Reputation: 2061
am having a problem with a bit of code.
I am trying to generate a unique name to insert into a database.
i have created the following function which checks to see if the name already exists:
function checkExists($database_reelfilm, $reelfilm, $mySlug, $locVal){
$mmid_rs_slugCheck = "-1";
if (isset($mySlug)) {
$mmid_rs_slugCheck = $mySlug;
}
$mmid2_rs_slugCheck = "-1";
if (isset($locVal)) {
$mmid2_rs_slugCheck = $locVal;
}
mysql_select_db($database_reelfilm, $reelfilm);
$query_rs_slugCheck = sprintf("SELECT * FROM locations_loc WHERE locations_loc.slug_loc = %s AND locations_loc.id_loc != %s", GetSQLValueString($mmid_rs_slugCheck, "text"),GetSQLValueString($mmid2_rs_slugCheck, "int"));
$rs_slugCheck = mysql_query($query_rs_slugCheck, $reelfilm) or die(mysql_error());
$row_rs_slugCheck = mysql_fetch_assoc($rs_slugCheck);
$totalRows_rs_slugCheck = mysql_num_rows($rs_slugCheck);
if($totalRows_rs_SlugCheck > 0){
return true;
}else{
return false;
}
};
i then create a loop to checking if the variable name exists, if it does i want it to add the value of the counter to the variable name then recheck to see if that exists until i have a unique name which i can then save to my db.
$updateVal = slugify($row_rs_locations['name_loc']);
$newSlug = slugify($row_rs_locations['name_loc']);
$locVal = $row_rs_locations['id_loc'];
//echo(slugify($row_rs_locations['name_loc']));
$checkCount = 1;
$isDupe = '<BR>';
while(checkExists($database_reelfilm, $reelfilm, $newSlug, $locVal)){
$isDupe = 'Duplicate Added ' . $checkCount . ' to slug...<BR>';
$newSlug = $newVal . $checkCount;
$checkCount ++;
}
if($updateVal != $newVal){
$updateVal = $newSlug;
}
I am obviously doing something wrong, I need the while loop on the next iteration to use the newSlug value set in the loop, from my various attempts i am not at all sure if this is possible.
Whats the best way to accomplish this?
Upvotes: 0
Views: 64
Reputation: 5511
i need the while loop on the next iteration to use the newSlug value set in the loop
In the while loop you do
$newSlug = $newVal . $checkCount;
But $newVal does not exist. Replace that line with the following:
$newSlug .= $checkCount;
Upvotes: 1
Reputation: 4614
$newVal
is not ever given a value, but it is used twice (second block of code). I think you need something like:
$newSlug = slugify($row_rs_locations['name_loc']);
$newVal = $newSlug;
Upvotes: 1