millsy007 Posted February 27, 2009 Share Posted February 27, 2009 I am writing a piece of code to insert names, part of the program needs to check for a duplicate name, upon there being a duplicate name I want to add a numerical suffix, and then increment this each time another duplicate is found/ or if a duplicate value has already been assigned. So far I have: $suffix = '0'; while ($check!= 'no duplicates') { $suffix ++; $query = " SELECT passenger_name FROM passengers, journey WHERE journey.shuttle_id = '$id' AND journey.id = passengers.journey_id AND passengers.passenger_name = '$name' "; $qry_result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($qry_result); if ($num_rows > 0) { while($row = mysql_fetch_array($qry_result)){ $name = $row[passenger_name];} $name .= "$suffix"; } else { $check = 'no duplicates'; } } It works, but is setting the names as bob|bob1|bob12|bob123|bob1234|bob12345|bob123456|bob1234567 as opposed to just adding the one number suffix, how could I remedy this? Quote Link to comment Share on other sites More sharing options...
asmith Posted February 27, 2009 Share Posted February 27, 2009 when the first time the query is being executed, what is the value of $name ? bob? to have an example I assume it is. your query will find 'bob' in the database. then it will make bob1 at the first attempt. Now $name is 'bob1'. Your main while loop now is searching the database for bob1? if it finds it, then the $name now will become " 'bob1' + 2 => 'bob12' I didn't get what you are trying to do with duplicate names. IF you wanna check to see if bob1 exists, then record bob2, then the only thing you need to do is removing $name .= "$suffix"; and put it when you want to insert the new query. and change your query to this : AND passengers.passenger_name = '$name.$suffix' Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted February 27, 2009 Share Posted February 27, 2009 $nameArray = array('bob','melany','bob','sue','rita','sue','bob'); $newNameArray = array(); $wrkArray = array_count_values($nameArray); foreach($wrkArray as $name => $count) { $newNameArray[] = $name; if ($count > 1) { for ($i = 2; $i <= $count; $i++) { $newNameArray[] = $name.$i; } } } print_r($newNameArray); Quote Link to comment Share on other sites More sharing options...
millsy007 Posted February 27, 2009 Author Share Posted February 27, 2009 What I want to do is where a name already exists, create a new name to insert that is basically the name they have with a number at the end. So at first it will find a bob, as bob exists I want to Create bob1 if bob1 exists then I would try bob2 and so on... I basically want to create a unique name that I can then use for the next part of my program Quote Link to comment Share on other sites More sharing options...
asmith Posted February 27, 2009 Share Posted February 27, 2009 ok, you can use this : <?php // this query gets ALL records in passenger_name that they start with $name (bob for example) $query = " SELECT passenger_name FROM passengers, journey WHERE journey.shuttle_id = '$id' AND journey.id = passengers.journey_id AND passengers.passenger_name LIKE '$name%' "; $qry_result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($qry_result); if ($num_rows > 0) { // if it exists, then put all similar names into an array while($row = mysql_fetch_array($qry_result)){ $similar_names[] = $row[passenger_name];} // check in the similar names array if your name exists, if so, adds 1 to suffix, then check again until you find your final suffix. $suffix = 1; while (in_array($name.$suffix, $similar_names)) $suffix++; } else { //$name is already unique. no suffix needed then. } now your desired name will be $name.$suffix, so you can record it into your table. Quote Link to comment Share on other sites More sharing options...
millsy007 Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks, but when I run this I just get the names input all without a suffix, eg bob | bob | bob ? Quote Link to comment Share on other sites More sharing options...
asmith Posted February 27, 2009 Share Posted February 27, 2009 how you "get" those ? I can't edit my post. Here's the new version : <?php // this query gets ALL records in passenger_name that they start with $name (bob for example) $query = " SELECT passenger_name FROM passengers, journey WHERE journey.shuttle_id = '$id' AND journey.id = passengers.journey_id AND passengers.passenger_name LIKE '$name%' "; $qry_result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($qry_result); if ($num_rows > 0) { // if it exists, then put all similar names into an array while($row = mysql_fetch_array($qry_result)){ $similar_names[] = $row[passenger_name];} // check in the similar names array if your name exists, if so, adds 1 to suffix, then check again until you find your final suffix. $suffix = 1; while (in_array($name.$suffix, $similar_names)) $suffix++; $final_name = $name.$suffix; } else { //$name is already unique. no suffix needed then. $final_name = $name; } echo $final_name; echo $final_name; will print the target name. use this, and tell me what does it print on the screen. Quote Link to comment Share on other sites More sharing options...
millsy007 Posted February 27, 2009 Author Share Posted February 27, 2009 Yeah thats perfect thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.