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>"; 	
}
?>

Link to comment
Share on other sites

<?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>";    
}
?>

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.