Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post

    Checking for duplicate values

    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!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    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.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    So how do you compare whole arrays to each other? The same as you would varaiables?

    So I could do something like this..

    PHP Code:
    $result array_unique($_SESSION['page4']);

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

    next page

    } else {

    error page



  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Well just tested it & it works great!

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    trying it out is usualy the easiest way to find out if it works

    glad you got it running
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    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.

    PHP Code:
    Array ( [page] => fourth [fmtextq1-pn] => [fmtextq2-pn] => [fmcheckboxq1-pn] => [fmcheckboxq2-pn] => [1] => 

    Array ( [
    page] => fourth [fmtextq1-pn] => [fmtextq2-pn] => [fmcheckboxq1-pn] => [fmcheckboxq2-pn] => 
    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..

    PHP Code:
    $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?

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    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:

    PHP Code:
    $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"..

    PHP Code:
    Array ( [page] => fourth [fmtextq1-pn] => [fmtextq2-pn] => [fmcheckboxq1-pn] => [fmcheckboxq2-pn] => [1] => // print_r($_SESSION['page4']);

    Array ( [page] => fourth [fmtextq1-pn] => [fmtextq2-pn] => [fmcheckboxq1-pn] => [fmcheckboxq2-pn] => // 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.

    PHP Code:
    $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!

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks for that... works good now!

    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".


  • #13
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see ...

    Glad you got it running
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •