Skip to content Skip to sidebar Skip to footer

Auto Detect Number Of Same Entry And Do A Rowspan According To The Number?

i wanted to create a forloop which will create a table from the data obtained from the database. the obtained result looks something like this. id/cart_id/title/price/quantity 1 /

Solution 1:

You can achieve this with an extra for loop:

<?php
    $lastcart = FALSE ;
    for($i = 0; $i < count($history); $i++) {
        $a = $history[$i]['cart_id'];
        echo '<tr >';
        $rowspan = 0;
        for(
            $j=$i;
            $history[$i]['cart_id'] !== $lastcart &&
            $j < count($history) && 
            $history[$j]['cart_id']==$history[$i]['cart_id'];
            $j++
        ){
            $rowspan++ ;
        }
        if($rowspan > 0){
            $lastcart = $history[$i]['cart_id'] ;
            echo '<td rowspan="{$rowspan}">'.$a.'</td>';
        }
        echo '<td>'.$history[$i]['title'].'</td>';
        echo '<td>'.$history[$i]['price'].'</td>';
        echo '<td>'.$history[$i]['quantity'].'</td>';
        echo '<td>'.$history[$i]['cart_id'].'</td>';
        echo '</tr>';
    }
?>

Post a Comment for "Auto Detect Number Of Same Entry And Do A Rowspan According To The Number?"