Jump to content

[SOLVED] Adding Suffix to Variable in a Loop


millsy007

Recommended Posts

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?

 

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

$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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.