PHP,我如何在10行10列中渲染100个项目

时间:2018-10-22 06:36:26

标签: php html css

所以我有一个包含一百个席位,有时包含150个席位的数组,我想在其中渲染项目,但要包含10行,而不是1行和100行,我该怎么做?

  <div class="container">
    <ol>
<-- ROW 1 START--> 
        <li class="row">
            <ol class="seats" type="A">
                <li class="seat">
                    <input type="checkbox" id="A1" />
                    <label for="A1">A1</label>
                </li>
            </ol>
        </li>
<-- ROW 1 End--> 
<-- ROW 2 START--> 
        <li class="row">
            <ol class="seats" type="A">
                <li class="seat">
                    <input type="checkbox" id="A1" />
                    <label for="A1">B1</label>
                </li>
            </ol>
        </li>
<-- ROW 2 END--> 
    </ol>
</div>  

这是php代码

<?php
require "includes\header.php";
require "includes\database.inc.php";

if (!isset($_SESSION['id'])) {
    header("Location: login.php?error=not_login");
    exit();
}
// SELECT * FROM seats where branch_id = 1
$seats = $mysqli->query("SELECT * FROM seats where branch_id = 1") or die($mysqli->error);
?>

  <head>
    <link href="./css/seats.css" rel="stylesheet">
  </head>


  <div class="container">
    <ol>
        <li class="row row--1">
            <ol class="seats" type="A">
            <?php 
                $i=1;
                while ($row = mysqli_fetch_array($seats)) {
            ?>
                <li class="seat">
                    <input type="checkbox" id="A1" />
                    <label for="A1"><?php echo $row['row'].$row['number'] ?></label>
                </li>
                <?php } ?>
        <li>
    </ol>
</div>  

但是它给了我这个用户界面,这是错误的,不是我想要的,并且当我检查A3时,它改高了A1 ...

Wrong UI and wrong checked ui

1 个答案:

答案 0 :(得分:1)

每10个项目结束后,您可以结束订购列表并重新创建一个新列表。尝试下面的代码,这可能会帮助您实现相同的目标:

<div class="container">
    <ol>
        <li class="row row--1">
            <ol class="seats" type="A">
            <?php 
                $i=1;
                while ($row = mysqli_fetch_array($seats)) {
            ?>
                    <li class="seat">
                        <input type="checkbox" id="A1" />
                        <label for="A1">A1</label>
                    </li>
            <?php if ($i%10 == 0) { ?>
                    </ol>
                    <ol class="seats" type="A">
            <?php }
                  $i++;
                }
            ?>
            </ol>
        </li>
    </ol>
</div>

只需将您的静态数据替换为您从$ row的db中获取的动态数据即可。