jwk811 Posted January 5, 2007 Share Posted January 5, 2007 ive been trying to do this for the last hour and i cant seem to figure it out... im trying to make an id that has letters in it.. i was to check if the id is already being used and if so make that id end with a number higher than that one like this: id1.. if thats used make that id2 unless thats being used then make it id3 and so on.. i need to get a variable from it that will have the id and number at the end.. any help? Link to comment https://forums.phpfreaks.com/topic/32932-increment-when-dbrows-are-more-than-zero/ Share on other sites More sharing options...
simcoweb Posted January 5, 2007 Share Posted January 5, 2007 Can you post the code, pleasae. Link to comment https://forums.phpfreaks.com/topic/32932-increment-when-dbrows-are-more-than-zero/#findComment-153341 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 Here's is one solution to your problem:[code]<?php$id_str = "thisisastring";$id_num = 0;while ($id_used = checkid($id_str, $id_num)) $id_num++;$use_this_id = $id_str . $id_num;function checkid($id_str,$id_num){ $ret = false; $q = "select * from table name where myid = '" . $id_str . $id_num . "'"; // based on your previous questions, I'm doing a database query $rs = mysql_query($q) or die("Problem with the query:<pre>$q</pre><br>" . mysql_error()); if (mysql_num_rows($rs) > 0) $ret = true; return($ret);}?>[/code]Note: this code has not been checked for syntax or logic errors.Ken Link to comment https://forums.phpfreaks.com/topic/32932-increment-when-dbrows-are-more-than-zero/#findComment-153343 Share on other sites More sharing options...
fooDigi Posted January 5, 2007 Share Posted January 5, 2007 forgive me if i do not understand this correctly, but if the preceding characters are always the same ('id')... then set a variable = str_replace('id', '', $id_string); ... which will leave you with just the numeric chars, which you can then increment. then combine the string back together with the next highest number. Link to comment https://forums.phpfreaks.com/topic/32932-increment-when-dbrows-are-more-than-zero/#findComment-153347 Share on other sites More sharing options...
jwk811 Posted January 5, 2007 Author Share Posted January 5, 2007 awesome! thanks ken it worked! Link to comment https://forums.phpfreaks.com/topic/32932-increment-when-dbrows-are-more-than-zero/#findComment-153350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.