Need help with While();

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!

Moderator: General Moderators

Post Reply
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Need help with While();

Post 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
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post 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.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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="">';

?>
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Thanks, trukfixer works almost perfect, but some times i still get the same image.......

-NSF
Post Reply