2015-06-05 13 views
5

Questo è il mio esempio di sistemazione dell'elenco dei posti. Devo mostrare alcune modifiche all'allineamento del sedile.Come organizzare PHP per il valore del loop?

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

Così sto ottenendo il risultato come

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

ma dovrebbe essere

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

Come posso ottenere in questo modo? Grazie

+0

Si prega di inviare il contenuto delle variabili rilevanti – treegarden

+2

Invertire il ciclo interno. –

+2

suggerimento per la prossima volta: prova a darci solo il codice pertinente. Non aver paura di cambiare una variabile qui o là per rendere più chiaro cosa intendi. Usa pseudocodice, .. In breve: fai tutto in modo che sia più facile per noi (lettori) capire meglio il tuo codice. :) – Jordumus

risposta

1

Si dovrebbe invertire il tuo posto di calcolo numero se si sta calcolando il numero di una colonna di strano.

Modificare la riga di calcolo per:

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

quello che sta succedendo qui è, stiamo controllando se la colonna è pari o dispari per ($j % 2) > 0; quindi calcolando il numero di conseguenza.

Quindi il codice dovrebbe essere simile a questo:

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

?> 
1

La linea in questione presumo sia

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

Al fine di ottenere un "effetto reverse", è sufficiente aggiungere una condizione if per ogni colonna dispari.

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); 
    } 
} 
Problemi correlati