Jump to content

load takes time


robert_gsfame

Recommended Posts

back to previous loop problem, i have this code...but i am not sure why it takes time to load the page

 

<?php

require_once('config.php');

$username="James";

$checkfirstuser=mysql_query(sprintf("SELECT * FROM test WHERE username='%s'",

mysql_real_escape_string($username)));

$i=1;

$tot=0;

while($i==1){

$rand=rand(0,9);

while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){

$number=$checkfirstuserarray['id'];

if($rand==$number){

$tot++;}

}

if($tot==0){

$i++;}else{}

}

echo $rand;

?>

 

Link to comment
Share on other sites

You're loop is similar to the one I rewrote in your other post.

 

Instead of

<?php
$i=1;
$tot=0;
while($i==1){
$rand=rand(0,9);
while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){
$number=$checkfirstuserarray['id'];
if($rand==$number){
$tot++;}
}
if($tot==0){
$i++;}else{}
} 
?>

Try

<?php
$checkfirstuserarray=mysql_fetch_assoc($checkfirstuser);
$number=$checkfirstuserarray['id'];
$rand = rand(0,9);
while ($rand == $number) {
    $rand = rand(0,9);
}
echo $rand;
?>

 

BTW, get into the habit of properly indenting your code, it makes reading & debugging code much easier. Also you don't need an "else" clause if there is nothing in it.

 

Ken

Link to comment
Share on other sites

Ok, do this:

<?php
$tmp = array();
while($checkfirstuserarray=mysql_fetch_assoc($checkfirstuser)) {
   $tmp[] = $checkfirstuserarray['id'];
}
$rand = rand(0,9);
while (!in_array($rand,$tmp) {
    $rand = rand(0,9);
}
echo $rand;
?>

 

Ken

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.