Page 1 of 1

Need help with While();

Posted: Fri Nov 11, 2005 3:52 pm
by Zoxive
I think theres something wrong with my while..... i've tryed many things, but it ends up crashing my Xammp, and it takes a couple minutes to get it back, and its just a pain, so i came here..

Code: Select all

<?php
	include('data.php');

	$folder="/NTS/images/tours/";

	
	$query = "SELECT `ID` FROM `invoice line item master`";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while($line = mysql_fetch_array($result, MYSQL_ASSOC)){
		$names[]=$line['ID'] . 'small.gif';
	}
		
	print_r ($line);

	
	$name = array_rand($names, 1);
	
	
	while($_SESSION['image']==$name){
		$name = array_rand($names, 1);
	}
		$_SESSION['image']=$name;
		
		$imagething = '<img src="' . $folder . $names[$name] . '" width="180" height="51" alt="">';

?>
Its baically just showing a random image each time the page is loaded, and i have it check to make sure its not he same one it showed last time too..

-NSF

Posted: Sat Nov 12, 2005 9:44 am
by ambivalent

Code: Select all

$name = array_rand($names, 1);

while($_SESSION['image']==$name)
Seems to me that the "while" always returns "true" and loops indefinitely, crashing XAMMP in the process.

Posted: Sat Nov 12, 2005 9:57 am
by trukfixer
try this:

Code: Select all

<?php
    include('data.php');
    $folder="/NTS/images/tours/";
$names = array();
    $query = "SELECT `ID` FROM `invoice line item master`";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    while($line = mysql_fetch_array($result))//no need for MYSQL_ASSOC- 
{
        array_push($names,$line['ID'] . 'small.gif');
    }
       print_r ($line);

 while($name = array_rand($names,1))
{
   if($_SESSION['image'] != $name)
   {
       $_SESSION['image']=$name;
        break;
    }
}     
        
        $imagething = '<img src="' . $folder . $names[$name] . '" width="180" height="51" alt="">';

?>

Posted: Sat Nov 12, 2005 10:03 am
by Zoxive
Thanks, trukfixer works almost perfect, but some times i still get the same image.......

-NSF