Re: duplicate entries in mysql column

From: Date: Sun, 04 Feb 2001 03:53:02 +0000
Subject: Re: duplicate entries in mysql column
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38065@lists.php.net to get a copy of this message
> My problem is that I need to find a way to check all the candidate_name > before printing them on the screen and add a flag to the ones which > occure more than once. If you can live with listing the names in alphabetical order, you can do something like this: $query = "select name, id from candidates order by name"; $candidates = mysql_query($query) or die(mysql_error()); $previous_name = 'An extremely unlikely name for a candidate to have'; $row = 0; while (list($name, $id) = mysql_fetch_row($candidates)){ echo $name; #Check if this candidate has same name as *next* candidate: if (strtolower($name) == mysql_result($candidates, $row + 1, 0)){ echo " ($id)"; } #Check if this candidate has same name as *last* candidate: elseif (strtolower($name) == $previous_name){ echo " ($id)"; } echo "<BR>\n"; $previous_name = $name; } This snippet will dump out the ID for any two (or more) candidates who have the same name, thus ensuring a unique but "humane" output. If you have two "Bob Jones" candidates, you'll have to remember which one is number 47 and which one is number 53, but otherwise you can just go by names. You may not need to go so far as to use a numeric ID: I use a similar technique on a back-end interface [you can't see it, sorry] to http://chatmusic.com/venues.htm where musicians can post their gigs to the calendar. When I have two venues named "Elbow Room", I dump out their city and state as well as their name. But, if I have two "Borders Books & Music" in the same city, I'll also dump out the street address as well as city/state. This provides the most "natural" interface for the musicians, yet "unique" enough for them to accurately pick the venue they are actually gigging at. Well, usually... Sometimes a musician *still* manages to pick the wrong venue. But having ID numbers spewed at the people who can't get it right from city/state and street address doesn't really seem like it's going to help :-) -- Visit the Zend Store at http://www.zend.com/store/ Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm

« previous php.general (#38065) next »