Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic row span php while loop

Tags:

php

html-table

i have two tables one item table and customer table: enter image description here

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.

enter image description here

 <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>

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

enter image description here

like image 482
Jasir alwafaa Avatar asked Jan 29 '26 15:01

Jasir alwafaa


1 Answers

You can try it this way:

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

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

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.

like image 66
trajchevska Avatar answered Jan 31 '26 05:01

trajchevska