动态行跨度php而循环 [英] Dynamic row span php while loop

查看:67
本文介绍了动态行跨度php而循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have two tables one item table and customer table:

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

in the table you can see the second item item id 1002 have two entries.and i want to add colspan to that item for column 1 and 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>

但它以正常方式打印,我不知道如何在循环中添加rowspan ...请提示解决它的一些逻辑。

But it print in normal way, i have no idea how to add rowspan in loop..please suggest some logic to solve its appreciated.

推荐答案

您可以这样尝试:

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

First, add a counter to your query that will indicate how many entries has a given item.

$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");

然后,在循环浏览项目时,您会将rowspan设置为项目所具有的条目数。下面是调整后的整个代码。

Then, when looping through the items you will set the rowspan to the number of entries the item has. Below is the whole code adjusted.

<?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,而不是在每次迭代时都这样做。

Note that I added a buffer where I set which item was already displayed. That array is used so you only open one td with the wanted rowspan, instead of doing it on every iteration.

这篇关于动态行跨度php而循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆