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
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
Link to comment
Share on other sites

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