Jump to content

Recommended Posts

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>";
?>
Link to comment
https://forums.phpfreaks.com/topic/300578-execute-a-line-of-code-for-x-times/
Share on other sites

  • Solution

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

 

  • Like 1
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.