...

View Full Version : Checking for duplicate values



cyphix
05-23-2004, 07:06 PM
Is there an easy way that I can check all values pulled from the last page ($_POST values) to see if there are any duplicate values & then I can define an error message. (Like some html code or something). ?

I probably could work a way out how to do it with while loops & if statements but since the variables coming from the last page will be dynamic (could be 1, could be 10 & the names of the variables will also vary, sometimes a certain type might exist, sometimes they won't) it would take a but of work also considering that I can't compare the variable to itself as well.

Just wondering if there was any easier way to do it?

Thanks!

raf
05-23-2004, 09:40 PM
You could first dump the post-collection into an array, and then compaire that array against an array_unique() version of the array.

If they are the same, then there are no duplicate values.
If they are different, then you could do an array_count_values() to see which values have a count of > 1

cyphix
05-23-2004, 10:45 PM
Hmmmm don't think that will work as that removes the values if it finds dupliactes.

All I need to do is check to see if they accidently inout the same value more than once & then if they did I am gonna reload the page below the error message so they can fill them in correctly.

raf
05-24-2004, 01:54 AM
it removes the values from the resulting array, but not from the post-collection. And it's the post-collection that you need to insert the entered values when you reload the page.

so i don't quite see the problem with creating a unique array.
unless you also wan't to print which formfields contained duplicate values. Then you'll probably need to have a few nested loops or so.

cyphix
05-24-2004, 03:26 PM
So how do you compare whole arrays to each other? The same as you would varaiables?

So I could do something like this..



$result = array_unique($_SESSION['page4']);

if ($_SESSION['page4'] == $result) {

next page

} else {

error page

}


:confused:

cyphix
05-24-2004, 11:11 PM
Well just tested it & it works great! :cool:

raf
05-24-2004, 11:25 PM
trying it out is usualy the easiest way to find out if it works ;)

glad you got it running :thumbsup:

cyphix
05-25-2004, 06:15 PM
Got a problem..

After I added in my custom error code now it returns everything as false.. I know why however..

This is what I get with "print_r" outputs after what should have been returned as true..

First one is the session array & second is the array_unique() array.



Array ( [page] => fourth [fmtextq1-pn] => 1 [fmtextq2-pn] => 2 [fmcheckboxq1-pn] => 3 [fmcheckboxq2-pn] => 4 [1] => 1 )

Array ( [page] => fourth [fmtextq1-pn] => 1 [fmtextq2-pn] => 2 [fmcheckboxq1-pn] => 3 [fmcheckboxq2-pn] => 4 )


I'm not sure how it picks it up from the previous pages values cause I changed nothing, but in my error code I had to use this, where I think this is where it is getting the value from somehow..



$tquestionnum = 'fmtextq' . $textqc . '-pn';

$tquestionnum2 = $_SESSION['page4'][$tquestionnum];


I can't figure out anywhere else it got this extra session 4 var from so this must be it..

Unsure how to fix this problem? Any help?

raf
05-25-2004, 07:48 PM
euh. What do you mean? Do you mean that there is an alement inside the array and you don't know where it comes from?

cyphix
05-25-2004, 10:34 PM
Yes... as you can see the array returned by the session array has an extra element which is "[1] => 1" ... as soon as the page opens I set the session array to:



$result = array_unique($_SESSION['page4']);


Yet... when I fill in the form & make sure I put no duplicates, this is what I get outputted with "print_r"..



Array ( [page] => fourth [fmtextq1-pn] => 1 [fmtextq2-pn] => 2 [fmcheckboxq1-pn] => 3 [fmcheckboxq2-pn] => 4 [1] => 1 ) // print_r($_SESSION['page4']);

Array ( [page] => fourth [fmtextq1-pn] => 1 [fmtextq2-pn] => 2 [fmcheckboxq1-pn] => 3 [fmcheckboxq2-pn] => 4 ) // print_r($result);


They should be matching & as I stated above this used to work fine; so it seems the the session array is pulling another element from somewhere.

I found out that it is grabbing the new element from this code I inserted in the new error page coding part.. as when I edited it out & used "print_r" the script worked fine & the element went away in the session array output.



$tquestionnum = 'fmtextq' . $textqc . '-pn';

$tquestionnum2 = $_SESSION['page4'][$tquestionnum];


Thing is, I need this code in there & can't take it out... any ideas?

Thanks for your help! :)

raf
05-25-2004, 10:53 PM
Hmm. I've had some bad experiences with arrays stored in sessionvariables.
Maybe i don't fully get it, but when i perform some action on the arrays inside the sessionvariables, then i often get strange results.
So what i always do is

$arrayvar=$_SESSION['bleble'];
my code to proces $arrayvar
$_SESSION['bleble']=$arrayvar;

So i work on a copy of the actual array.
Try it like that.

I don't quite get this:

$tquestionnum = 'fmtextq' . $textqc . '-pn';
$tquestionnum2 = $_SESSION['page4'][$tquestionnum];

What are you trying to do here?

cyphix
05-25-2004, 11:34 PM
Thanks for that... works good now! :D



I don't quite get this:

$tquestionnum = 'fmtextq' . $textqc . '-pn';
$tquestionnum2 = $_SESSION['page4'][$tquestionnum];

What are you trying to do here?


Well basicaly.. I have to stick all the values together so I can make use of them elsewhere in the page & in the session variable.

The "$tquestionnum" is used to name form variables on the previous page & hence the error page it will produce if they do something wrong as well.

When you add 'fmtextq' + $textqc + '-pn' together you get the name of the variables on the last page.

Since I can only refer to them from the last page they will be no good if someone produces an error since the last page will change & now these variables won't exist... so then I use that value in a session variable to keep their values & assin that value to "$tquestionnum2".

:D

raf
05-25-2004, 11:41 PM
I see ...

Glad you got it running :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum