Jump to content

increment when dbrows are more than zero


jwk811

Recommended Posts

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

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
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.