PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
i am having a small problem with random numbers. I have a script which does exactly what i want it to do - generate a random number which is an id for a database table, gets the name and prints. this all works fine. however i want to do this over again and am having problems:
$foo = true;
while ($foo = true)
{
//get a random number
srand(time());
$random = (rand(1, 21));
include 'connect.php';
//select all dishid values from the dishes table
$idexists = @mysql_query("SELECT dishid FROM dishes");
if(!idexists)
{
echo"fail";
}
//add all id's to an array
$idarray;
$iterationnumber = 0;
while ($row = mysql_fetch_array($idexists))
{
$idarray[$iterationnumber] = $row['dishid'];
$iterationnumber++;
//all valid id numbers are now stored in an array
}
//if the random number generated is in the array it is a valid id. if not, keep generating until valid id found
while (!in_array($random, $idarray))
{
$random = (rand(1, 21));
}
$getrandomname = @mysql_query("SELECT name FROM dishes WHERE dishid = '$random'");
if(!$getrandomname)
{
echo"fail";
}
while ($row = mysql_fetch_array($getrandomname))
{
$dish1name = $row['name'];
echo "$dish1name <br />";
}
}
obviously this will loop forever but that condition is only there to demonstrate my problem. what this does is it outputs "$dish1name" over and over. Why is there not a new value in $random when the loop re-iterates?
thanks
Your example code has all sorts of issues. What exactly is the problem? Are you getting values output, or is the variable name actually outputting. What is it exactly that is happening?
$foo = 1;
while ($foo <8)
{
//get a random number
srand(time());
$random = rand(1, 21);
include 'connect.php';
//select all dishid values from the dishes table
$idexists = @mysql_query("SELECT dishid FROM dishes");
if(!idexists)
{
echo"fail";
}
//add all id's to an array
$idarray;
$iterationnumber = 0;
while ($row = mysql_fetch_array($idexists))
{
$idarray[$iterationnumber] = $row['dishid'];
$iterationnumber++;
//all valid id numbers are now stored in an array
}
//if the random number generated is in the array it is a valid id. if not, keep generating until valid id found
while (!in_array($random, $idarray))
{
$random = rand(1, 21);
}
$getrandomname = @mysql_query("SELECT name FROM dishes WHERE dishid = '$random'");
if(!$getrandomname)
{
echo"fail";
}
while ($row = mysql_fetch_array($getrandomname))
{
$dish1name = $row['name'];
echo "$dish1name <br />";
}
$foo++;
}
<?php
$foo = 0; // PHP is zero based, so it makes sense to stay in the theme
while ($foo < 7)
{
//get a random number
$random = rand(1, 21);
include 'connect.php';
//select all dishid values from the dishes table
/**
* You should have some sort of error handling here
* And you should not use error suppression @
*/
$idexists = mysql_query('SELECT dishid FROM dishes') or die(mysql_error());
if (!$idexists)
{
echo"fail";
}
//add all id's to an array
/**
* WHAT DOES THIS DO?
*
* Should it maybe be $idarray = array();
*/
//$idarray;
$idarray = array();
$iterationnumber = 0;
while ($row = mysql_fetch_array($idexists))
{
$idarray[$iterationnumber] = $row['dishid'];
$iterationnumber++;
}
//if the random number generated is in the array it is a valid id. if not, keep generating until valid id found
while (!in_array($random, $idarray))
{
$random = rand(1, 21);
}
/**
* This should have some sort of error handling also
* As well as not using error suppression
*/
$getrandomname = mysql_query("SELECT name FROM dishes WHERE dishid = '$random'") or die(mysql_error());
if (!$getrandomname)
{
echo 'fail';
}
while ($row = mysql_fetch_array($getrandomname))
{
echo $row['name'] . '<br />';
}
$foo++;
}
?>
Yes, do something other than echoing 'Fail'. This will not prevent execution of the remainder of the script. If what is coming out of the query is needed by the rest of the script, and the query fails, and the scripts continues execution... get my drift?
//select all dishid values from the dishes table that are vegetarian
$idexists = @mysql_query("SELECT dishid FROM dishes WHERE vegetarian = 'Yes'");
i have modified this line so that only numbers from vegetarian recipes are selected. i need to do the whole thing again but without vegetarian. so there are two sets of random values .obviously i would change the code above from yes to no. is there a way which doesnt include copying all the code i wrote before to do this?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.