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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding Duplicate Numbers

    Hi,

    I'm looking for a solution that will determine if I have duplicate age_id's I'm pulling from a mysql database.

    $user1->age_id
    $user2->age_id
    $user3->age_id
    $user4->age_id

    I'm pulling the field 'age_id' from different user records in the mysql database as shown above and I would like to determine if any of the above age_id's are the same. I dont need to know how many duplicates, only if their are any duplicates and if so I will echo a duplicate id image.

    any help would be appreciated, Thanks, Jen.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Inline method, if you only have to do this once:
    PHP Code:
    $temp = array();
    // $max is the number of variables in the form $user#->age_id to check
    for ($ii 1$ii <= $max; ++$ii)  
    {
        if (!
    in_array(${'user'.$ii}->age_id$temp))
        {
            
    array_push($temp$ii);
        }
        else
        {
            echo 
    'duplicate';
            break;
        }

    function friendly method:
    PHP Code:
    function checkDupAgeId($ids)
    {
        return (
    array_unique($ids) != $ids);
    }
    checkDupAgeId(array($user1->age_id$user2->age_id$user3->age_id$user4->age_id));
    // true means duplicate 
    Neither seem really ideal, so I'm probably missing something.

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks ralph, your in-line method is working but I beleive I have other problems in my app. thanks for the help.
    Last edited by jen225; 01-08-2006 at 07:16 AM.

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    edit: nm then :]

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Ralph, why do you use '$ii' ? I can't get it. I mean we use '$i' because it stands for a integer variable. What is 'ii' for? Lol. And why are you using break?

    PHP Code:

    for($i=1,$used=array(); $i<=$max$i++) {
      if(
    in_array(${'user'.$i}->age_id$used)) {
        
    $duplicate_image true;
        break;
      } else { 
    $used[]=${'user'.$i}->age_id; }

    Thats a little smaller and compact, and wherever you need to do the image you just check if $duplicate_image is true, and if so, echo the image.
    Last edited by Element; 01-08-2006 at 10:18 PM.

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by Element
    Ralph, why do you use '$ii' ? I can't get it. I mean we use '$i' because it stands for a integer variable. What is 'ii' for? Lol. And why are you using break?
    i doesn't stand for integer at all, it's just the first in the series of iterator values (i, j, k) that's been around since forever. I use ii because in a lot of fonts i looks just like l and/or 1 and doubling it makes it easier to see what's happening. This is less a problem in PHP because of the $, but it's the way I do it in every language

    I use break because the number of duplicates isn't important here, only the existence of at least one. After one is found it's a waste of time and processor power to continue looping over results that don't matter.

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    That all pretty much makes sense, but according to me, and all my partners, we all use i because it makes more sense and in more logical standards for human comprehension, it does stand for integer, which is why when everyone uses numbers they usually just name it $i.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Iit's not any more logical or less if you use a variable which name is not $i as an iterator.
    anyway if you don't like to go into fuzzy logic there's nothing that is more or less logic.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Using the letter i as the variable name within a loop is not because it stands for integer. It is likely however passed on from the FORTRAN language in which the letters i-n designate integer variables. Since FORTRAN was one of the ancestors for modern day languages and programmers often have to rewrite older FORTRAN programs using modern day languages the habit of using i,j,k...n as integer variables in todays programming got carried over.

    So let's end this argument and get back on the original topic at hand.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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