5

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

3
  • Please post the content of the relevant variables Commented Jun 5, 2015 at 7:58
  • 2
    Reverse the inner loop. Commented Jun 5, 2015 at 8:02
  • 2
    tip for next time: try to only give us the relevant code. Don't be afraid to change a variable here or there to make it more clear what you mean. Use pseudocode, .. In short: do everything so it's easier for us (readers) to understand your code better. :) Commented Jun 5, 2015 at 8:31

2 Answers 2

1

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>';
            }
        }
    }
}

?>
Sign up to request clarification or add additional context in comments.

Comments

1

The relevant line I assume is

for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j < $cols; $j++) {
        $seatNo = ($i + $j * $rows + 1);
    }
}

In order to get an "reverse effect", simply add an if condition to each odd column.

for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j < $cols; $j++) {
        if ($j % 2 == 1) $seatNo = (($rows - $i) + $j * $rows );
        else $seatNo = ($i + $j * $rows + 1);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.