Jump to content


Photo

do while loop ?


  • Please log in to reply
6 replies to this topic

#1 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 01:44 PM

Hi, im a newbe to php. I want to create a six digit random number as an ID but would like to also check if its not been already used within the database. Here is the code I have so far..
     $chars_for_actnum = array ("1","2","3","4","5","6","7","8","9","0");

        for ($i = 1; $i <= 6; $i++) {
            $actnum = $actnum . $chars_for_actnum[mt_rand (0, count ($chars_for_actnum)-1)];
            

            //make a loop so we can check the database if ID has already been taken. if it has we will genarate another ID. 

$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query); 
if ($row = mysql_fetch_array($result)){ 
if  ($row["id"] == $actnum)        
}
    }

Any help will be much appreciated.
Thx

EDITED BY WILDTEEN88: PLEASE USE THE CODE TAGS WHEN POSTING CODE, THANK YOU

#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 01:52 PM

first for the random number can't you just do
$id = mt_rand (100000, 999999);
$query = mysql_query ("SELECT `id` FROM `table` WHERE `id` = '$id'") or die (mysql_error ());
if (mysql_num_rows ($query) != 0)
{
  $error = "blah";
}
else
{
  mysql_query ("INSERT...
}
In any case are you looking for unique ids? you can always use an autoincremental index field in the database which automatically gives the id, and if it's for security or something just use something like
md5 (time().$_SERVER['REMOTE_ADDR']);
z..z..z..z..z..z..z..z..

#3 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 14 June 2006 - 01:54 PM

$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query);
if ($row = mysql_fetch_array($result)){
if ($row["id"] == $actnum)
}
}
Personally, and I haven't tried this, you should be able to do this:
$query = "Select * from ".$DBprefix."client_info where id='$actnum'";
$result = mysql_query($query);
$row = mysql_num_rows($result); //number of rows returned as matching.

if($row) {
   //carry out a repeat id generation perhaps, and then call function again?
} else {
   //do whatever you like if the number isn't taken, as in insert into database etc.
}
$row = number of rows returned matching your query.
if(0) - or no matches, then expression if($row) will evaulate to false, as it's if(0) which is false!
if(1) - or if the number of matching rows is greater than 0, it will evaluate this expression to true.

HOwever, as mentioned above, if it's a unique id number, i would just go with making that field your primary key, and autoincrementing it. IE - you won't actually insert anything into that field, just insert the details instead and it will automatically fill in the field with a unique identifier upon insertion of the other fields information.

#4 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 14 June 2006 - 01:56 PM


function GenCode() {
     $num =  rand(0, 9);

     for ($i = 1; $i <= 6; $i++) {
     $actnum = $actnum . $num;
     return $actnum;
}

//make a loop so we can check the database if ID has already been taken. if it has we will genarate another ID. 

function ChkCode() {

     $actnum = GenCode();

     $query = "Select * from ".$DBprefix."client_info where id='$actnum'";
     $result = mysql_query($query); 
     
     if (mysql_num_rows($result) <> 0) {
          return $actnum;
     } else {
          // The number already exists in the database try again.
          ChkCode();
}

$newCode = ChkCode; // This will return the new code already checked.

You could also mod the script to add the new code into your database.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#5 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 02:27 PM

Thanks for your help everyone.. autoincrementing is an option I considered but wouldn’t it be less efficient as I wouldn’t be able to replace expired members? By using a checked random number it should save disk space and ID numbers.

#6 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 14 June 2006 - 02:40 PM

[!--quoteo(post=383815:date=Jun 14 2006, 03:27 PM:name=dharm11)--][div class=\'quotetop\']QUOTE(dharm11 @ Jun 14 2006, 03:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for your help everyone.. autoincrementing is an option I considered but wouldn’t it be less efficient as I wouldn’t be able to replace expired members? By using a checked random number it should save disk space and ID numbers.
[/quote]

I did a Call Logging script once and i had the same problem as i had to reserve a job number before i submitted the job which ment i had loads of job ids that had no job attached.

I just created a script to clear all these out which run once a night but like you say the i could have a job with id230 and only have 12 jobs.

Could have done something similar to what you have done but that would mean that anyone looking through the past jobs may find job id 20 dated 2006 and job id 1 dated 2001 which is a confused mess waiting to happen. It just depens what your wanting to attach to your id's.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#7 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 02:44 PM

[!--quoteo(post=383829:date=Jun 14 2006, 03:40 PM:name=scripts2go.co.uk)--][div class=\'quotetop\']QUOTE(scripts2go.co.uk @ Jun 14 2006, 03:40 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I did a Call Logging script once and i had the same problem as i had to reserve a job number before i submitted the job which ment i had loads of job ids that had no job attached.

I just created a script to clear all these out which run once a night but like you say the i could have a job with id230 and only have 12 jobs.

Could have done something similar to what you have done but that would mean that anyone looking through the past jobs may find job id 20 dated 2006 and job id 1 dated 2001 which is a confused mess waiting to happen. It just depens what your wanting to attach to your id's.
[/quote]
I'd have to agree, I think you'd be much better off using an autoincremental value instead of random numbers, and just check for old users when adding the new one. I assume this is what you would be doing in any case with the random numbers as it would also need to check which are in use or not
z..z..z..z..z..z..z..z..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users