Tuesday, April 28, 2009

RE: Check array for duplicate

Basically I am trying to do 2 things with it now.

Check for duplicates (would only happen if someone changes the values in the
form with Firebug or something)
And
Check the submitted values against the values in the database.

I added a query to get the values that are possible
$test = $this->User->$model_name->find('list', array('fields' => 'id'));
debug($test);
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
)

The data to be saved returns this (they selected options 1 and 5)
Array
(
[System] => Array
(
[System] => Array
(
[0] => 5
[1] => 1
)

)

)

So I want to compare the submitted values (1, 5) against the possible values
in the db (1 - 6). If OK save the data, else if someone tries to save value
27 it will not save anything.

Still new at this and trying to see how to get the [System][System][id] for
each of the values trying to be saved and compare them against the db
values.

-----Original Message-----
From: brian [mailto:bally.zijn@gmail.com]
Sent: April-28-09 4:35 PM
To: cake-php@googlegroups.com
Subject: Re: Check array for duplicate


I don't know why the save isn't happening but you shouldn't have the
array_unique() in the if test because that just returns the modified array.
If you're more interested in testing whether the array has dupes, rather
than simply removing them, I think you'd have to do something like:

if (sizeof(array_unique($this->data[$model_name][$model_name])) ==
sizeof($this->data[$model_name][$model_name]))
{
$this->User->save($this->data);
$this->skills($model);
}
else
{

I was going to suggest using array_diff() but that function is bollocks.

Anyway, why don't you just use SecurityComponent?

On Tue, Apr 28, 2009 at 1:59 PM, Dave Maharaj :: WidePixels.com
<dave@widepixels.com> wrote:
>
> Ok right on....seems to get me started.
>
> I have this in my controller but if there is a duplicate its not
> saving which is what I want) but  skipping past the }else{
>
> Ideas what i am missing?
>
>                  if (!empty($this->data)) {
>                      $san = new Sanitize();
>                      $this->data = $san->clean($this->data);
>                      if ($this->data[$model_name][$model_name] =
> array_unique($this->data[$model_name][$model_name])) {
>                                  $this->User->save($this->data);
>                          $this->skills($model);
>                      } else {
>                          $value = 'Messing around with values';
>                          $this->User->id = $this->Auth->user('id');
>                          //set confirm code to zero disableing login
>                          //$this->User->saveField('confirmed', 0);
>                          $this->set('value', $value);
>                          $this->layout = 'ajax';
>                          $this->render('/errors/hack_attempt');
>                      }
>                  }
> -----Original Message-----
> From: brian [mailto:bally.zijn@gmail.com]
> Sent: April-28-09 3:13 PM
> To: cake-php@googlegroups.com
> Subject: Re: Check array for duplicate
>
>
> $this->data['System']['System'] =
> array_unique($this->data['System']['System']);
>
> On Tue, Apr 28, 2009 at 1:33 PM, Dave Maharaj :: WidePixels.com
> <dave@widepixels.com> wrote:
>>
>> I have modified my form value using Firebug and I get this array
>> (which should never happen unless someone has edited the form and
>> tried submitting the altered values)
>>
>> How could I create a quick 'check the values' function for duplicates
>> to see if someone was trying to mess with the site?
>>
>>
>> Array
>> (
>>    [System] => Array
>>        (
>>            [System] => Array
>>                (
>>                    [0] => 1
>>                    [1] => 1
>>                    [2] => 1
>>                )
>>
>>        )
>>
>> )
>>
>> I have the db set so the fields have to be unique so it will not
>> save...but the view shows SQL Error: 1062: Duplicate entry '1-3' for
>> key 2
>> Query: INSERT INTO `users_systems` (`user_id`,`system_id`) VALUES
>> (3,'1'), (3,'1'), (3,'1')
>>
>> I would like to check the array for duplicte values before saving, if
>> found then destroy user session, display message and ban user...just
>> cant figure ot how to check the array for duplcates.
>>
>> Ideas?
>>
>> thanks
>>
>>
>> Dave
>>
>>
>> >
>>
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: