Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to arrange PHP for loop value?

Tags:

loops

php

This is my example of seat list arrangements. I have to show some seat alignment changes.

$rows=($data['seat_type']==1)?3:4;
$cols=round($numofseats /$rows) ;
$rowCssPrefix= 'row-';
$colCssPrefix= 'col-';
$seatWidth= 35;
$seatHeight= 35;
$seatCss= 'seat';
$selectedSeatCss= 'selectedSeat';
$selectingSeatCss= 'selectingSeat';
$window_rows = ($data['seat_type']==1) ? array(0,2) :array(0,3);

for ($i = 0; $i < $rows; $i++) {
    $seat_w=(in_array($i,$window_rows))?'W':'A';
    for ($j = 0; $j < $cols; $j++) {
        $seatNo = ($i + $j * $rows + 1);
        if($seatNo <= $numofseats)
        {
            $className = $seatCss . ' '.$rowCssPrefix .$i.' '.$colCssPrefix .$j;                                      if($j % $cols==0) echo '<br>';
            if(!in_array($seatNo,$booked_seats)) {
                echo'<li class="' . $className.'" style="top:'. ($i *$seatHeight).'px;left:'. ($j *$seatWidth).'px" title="'.$seatNo.$seat_w.'">'.$seatNo.'<input  type="checkbox" name="seat[]" value="'.$seatNo.'-'.$seat_w.'"/></li>';
                }else{
                $className .= ' '.$selectedSeatCss;
                echo'<li class="' . $className.'" style="top:'. ($i *$seatHeight).'px;left:'. ($j *$seatWidth).'px">'.$seatNo.'</li>';
            }
        }
    }
}

So I am getting the result like

1  4 7 10
2  5 8 11
3  6 9 12

but it should be

1  6 7 12
2  5 8 11
3  4 9 10

How can I get like this? Thanks

like image 771
MGM Avatar asked Oct 31 '22 02:10

MGM


1 Answers

You should reverse your seat number calculation if you're calculating a number for an odd column.

Change the calculation line to :

$seatNo = (($j % 2) > 0) ? (($rows - $i) + ($j * $rows)) : ($i + $j * $rows + 1); 

What's happening here is, we are controlling if the column is odd or even by ($j % 2) > 0; then calculating the number accordingly.

So your code should look like this :

<?php

$rows=($data['seat_type']==1)?3:4;
$cols=round($numofseats /$rows) ;
$rowCssPrefix= 'row-';
$colCssPrefix= 'col-';
$seatWidth= 35;
$seatHeight= 35;
$seatCss= 'seat';
$selectedSeatCss= 'selectedSeat';
$selectingSeatCss= 'selectingSeat';
$window_rows = ($data['seat_type']==1) ? array(0,2) :array(0,3);

for ($i = 0; $i < $rows; $i++) {
    $seat_w=(in_array($i,$window_rows))?'W':'A';
    for ($j = 0; $j < $cols; $j++) {
        // If we are on the first (or 3rd, 5th, odd numbers) column, normally continue numbering,
        // But if we are on an even column, reverse the numbering by (($rows - $i) + ($j * $rows)).
        $seatNo = (($j % 2) > 0) ? (($rows - $i) + ($j * $rows)) : ($i + $j * $rows + 1); 
        if($seatNo <= $numofseats)
        {
            $className = $seatCss . ' '.$rowCssPrefix .$i.' '.$colCssPrefix .$j;                                      if($j % $cols==0) echo '<br>';
            if(!in_array($seatNo,$booked_seats)) {
                echo'<li class="' . $className.'" style="top:'. ($i *$seatHeight).'px;left:'. ($j *$seatWidth).'px" title="'.$seatNo.$seat_w.'">'.$seatNo.'<input  type="checkbox" name="seat[]" value="'.$seatNo.'-'.$seat_w.'"/></li>';
                }
                else {
                $className .= ' '.$selectedSeatCss;
                echo'<li class="' . $className.'" style="top:'. ($i *$seatHeight).'px;left:'. ($j *$seatWidth).'px">'.$seatNo.'</li>';
            }
        }
    }
}

?>
like image 192
mehmetseckin Avatar answered Nov 15 '22 06:11

mehmetseckin