Jump to content

[SOLVED] Problem with looping through an array


Kalland

Recommended Posts

Hi,

 

I'm having some trouble looping through an array. The inner loop loops through the array and checks if a seat is reserved, but it only returns true for the last row. Seats 1 and 5 are supposed to have the class reserved, but it's only applied to seat 5.

 

If I try to echo out the $tmp variable it shows 1 and 5 between every row.

 

The outer loop creates the seats.

 

What could be the problem here?

 

<?php
error_reporting(E_ALL);

$list[] = array('1', 'test');
$list[] = array('5', 'test2'); 
$rows = count($list);

/** Create seats */
for ($i = 1; $i <= 80; $i++) { 			
	for ($j = 0; $j < $rows; $j++) { 		
		$tmp = $list[$j][0];
		echo $tmp;
		/** Check if seat is already reserved */
		$class = ($i == $tmp) ? 'reserved' : 'available';
	}	 	
	echo "<div class='$class'>$i</div>"; 	
}
?>

<?php
error_reporting(E_ALL);

$list[] = array('1', 'test');
$list[] = array('5', 'test2'); 
$rows = count($list);
//create array of reserved seats
for ($j = 0; $j < $rows; $j++) {       
    $tmp[] = $list[$j][0];
}     
/** Create seats */
for ($i = 1; $i <= 80; $i++) {          
    $class = (in_array($i;$tmp)) ? 'reserved' : 'available';      
    echo "<div class='$class'>$i</div>";    
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.