php - 如何安排PHP for循环值?

标签 php loops

这是我的座位表安排示例。我必须展示一些座位排列变化。

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

所以我得到这样的结果

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

但应该是

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

我怎么会变成这样? 谢谢

最佳答案

如果您正在计算一个奇数列的数字,您应该反转您的座位号计算。

将计算行更改为:

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

这里发生的事情是,我们通过 ($j % 2) > 0 控制列是奇数还是偶数;然后相应地计算数量。

所以你的代码应该是这样的:

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

?>

关于php - 如何安排PHP for循环值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30661277/

相关文章:

php - 如何在php中动态检查函数的参数数量

c++ - 查找两个字符串之间的最长公共(public)后缀 C++

javascript - 如何遍历列表并添加图像

c - 输入用户的密码并检查它是否包含字符、字母和数字

C# float 无限循环

php foreach 静态函数

php - [ubuntu]重新安装apache2 无法访问我的wordpress

php - Woocommerce 移动更新购物车按钮

php - 远程访问WAMP服务器

php - Nginx 位置配置(子文件夹)