【问题标题】:Dynamic row span php while loop动态行跨度php while循环
【发布时间】:2016-08-07 16:52:42
【问题描述】:

我有两个表一个项目表和客户表:

在表格中,您可以看到第二个项目 id 1002 有两个条目。我想将 colspan 添加到该项目的第 1 列和第 3 列。

 <table>
  <tr>
    <th>Item ID</th>
    <th>Item Color</th>
    <th>Customer</th>
  </tr>
<?php
$sql = mysqi_query($con,"select * from item_table");
while($row = mysqli_fetch_array($sql)){
  ?>

<tr>
    <td><?=$row['item_id'];?></td>
    <td><?=$row['item_color'];?></td>
    <td>
        <select>
        <?php
        $sql_cust = mysqli_query($con,"select * from customer_tbl");
        while($row_cust = mysqli_fetch_array()){
        if($row['customer_id'] == $row_cust['customer_id']){
            echo "<option selected='selected' >".$row['customer_name']."</option>";
        }else{
            echo "<option>".$row['customer_name']."</option>";
        }

        <?php
        }
        ?>
        </select>
    </td>
  </tr>


<?php
}
?>
</table>

但它以正常方式打印,我不知道如何在循环中添加行跨度..请提出一些逻辑来解决它的赞赏。

【问题讨论】:

    标签: php html-table


    【解决方案1】:

    你可以这样试试:

    首先,在您的查询中添加一个计数器,该计数器将指示给定项目的条目数。

    $sql_cust = mysqli_query($con,
    "SELECT *, (SELECT COUNT(*) FROM item_table as it 
     WHERE it.item_id = item_table.item_id) as c
     FROM item_table");
    

    然后,在遍历项目时,您会将行跨度设置为项目的条目数。下面是调整后的整个代码。

    <?php
    $sql = mysqi_query($con,
        "SELECT *, (SELECT COUNT(*) FROM item_table as it 
         WHERE it.item_id = item_table.item_id) as entry_count
         FROM item_table");
    $buffer = [];
    while($row = mysqli_fetch_array($sql)){
        if(!isset($buffer[$row[$item_id]])) {
            $buffer[$row[$item_id]] = 1;
        }
    ?>
    <tr>
        <?php if(!isset($buffer[$row[$item_id]])) {?>
        <td rowspan="<?=$row['entry_count']?>"><?=$row['item_id'];?></td>
        <?php }?>
        <td><?=$row['item_color'];?></td>
        <?php if(!isset($buffer[$row[$item_id]])) {?>
        <td rowspan="<?=$row['entry_count']?>">
            <select>
            <?php
            $sql_cust = mysqli_query($con,"select * from customer_tbl");
            while($row_cust = mysqli_fetch_array()){
            if($row['customer_id'] == $row_cust['customer_id']){
                echo "<option selected='selected' >".$row['customer_name']."</option>";
            }else{
                echo "<option>".$row['customer_name']."</option>";
            }
    
            <?php
            }
            ?>
            </select>
        </td>
        <?php }?>
      </tr>
    
    
    <?php
    }
    ?>
    

    请注意,我添加了一个缓冲区,用于设置已显示的项目。使用该数组,因此您只打开一个具有所需行跨度的 td,而不是在每次迭代时都这样做。

    【讨论】:

      【解决方案2】:

      我有一个简单的想法

      给 TD 一个类似的 ID

      <td id="dynamically-Generate"> (you need to verify that TD id need to be equal in .rowSpan ="here" inside script  )
      

      如果动态生成大于 1 则设置此 TD 则不显示

      如果动态生成大于 1 则再次使用此脚本

      <script>
      document.getElementById("dynamically-Generate").rowSpan = "dynamically-Generate";
      </script>
      

      在循环内使用脚本并且两者都动态生成需要在每个循环内相同并在每个循环后更改

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-31
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 2011-06-02
        相关资源
        最近更新 更多