jiros1 Posted January 20, 2016 Share Posted January 20, 2016 There are seats that need to be assigned to visitors, for each visitor I check if the seat is available, if it is, give the value 'x' for 1 $visitor until there are no visitors left. I want to execute a line of code for x times, x = the value of visitors. As the code is now, I want to suggest 2 visitors a seat by giving the value 'x'. I will use the values as follows: 1 = available 0 = is not available x = was available, and now it's a suggested seat The problem: I want to have a output of two X's, but as the script is now, it generates three X's, and I don't know why. The question: How do make my script so that it will output two X's (the value of $visitors)? The output: Visitors: 2 Array ( [0] => Array ( [seatAvailable] => 0 [seatNumber] => 1 ) [1] => Array ( [seatAvailable] => 0 [seatNumber] => 2 ) [2] => Array ( [seatAvailable] => x [seatNumber] => 3 ) [3] => Array ( [seatAvailable] => x [seatNumber] => 4 ) [4] => Array ( [seatAvailable] => x [seatNumber] => 5 ) ) My code <?php $visitors = 2; $seats = array( array( 'seatAvailable' => '0', 'seatNumber' => '1' ), array( 'seatAvailable' => '0', 'seatNumber' => '2' ), array( 'seatAvailable' => '1', 'seatNumber' => '3' ), array( 'seatAvailable' => '1', 'seatNumber' => '4' ), array( 'seatAvailable' => '1', 'seatNumber' => '5' ), ); echo "Visitors: ".$visitors; echo "<ol>"; foreach($seats as &$val){ //If not available if($val['seatAvailable'] == '0'){ //echo "<li>not available</li>"; } //If available elseif($val['seatAvailable'] == '1'){ //Give available spots the value X, for as many as there are $visitors for($i=0;$i<$visitors;$i++){ $val['seatAvailable'] = 'x'; } } } echo "</ol>"; echo "<pre>"; print_r($seats); echo "</pre>"; ?> Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted January 20, 2016 Solution Share Posted January 20, 2016 If you output the value of $i in your for loop, you'll notice that the loop is being executed for every seat available. for($i=0;$i<$visitors;$i++){ echo "<div>here ($i)</div>"; $val['seatAvailable'] = 'x'; } To only suggest seats based on the number of visitors you, could replace the for loop with something like this: <?php //... //If available elseif($val['seatAvailable'] == '1'){ //IF THERE ARE STILL VISITORS TO PROCESS, MARK THE SUGGESTED SEAT if($visitors > 0) { $val['seatAvailable'] = 'x'; $visitors--; } } //... ?> Then once all the $visitors have been processed, you could break out of the loop. Here is all the code in context: <?php $visitors = 2; $seats = array( array( 'seatAvailable' => '0', 'seatNumber' => '1' ), array( 'seatAvailable' => '0', 'seatNumber' => '2' ), array( 'seatAvailable' => '1', 'seatNumber' => '3' ), array( 'seatAvailable' => '1', 'seatNumber' => '4' ), array( 'seatAvailable' => '1', 'seatNumber' => '5' ), ); echo "Visitors: ".$visitors; echo "<ol>"; foreach($seats as &$val){ //If not available if($val['seatAvailable'] == '0'){ //echo "<li>not available</li>"; } //If available elseif($val['seatAvailable'] == '1'){ //IF THERE ARE STILL VISITORS TO PROCESS, MARK THE SUGGESTED SEAT if($visitors > 0) { $val['seatAvailable'] = 'x'; $visitors--; } } //IF THERE ARE NO MORE VISITORS, EXIT LOOP if($visitors == 0) { break; } } echo "</ol>"; echo "<pre>"; print_r($seats); echo "</pre>"; ?> 1 Quote Link to comment Share on other sites More sharing options...
jiros1 Posted January 22, 2016 Author Share Posted January 22, 2016 This is great, it's exactly what I needed. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.