Home > Blockchain >  How to insert separator in table built from array in php?
How to insert separator in table built from array in php?

Time:05-30

Hi I have this table built from an array but I want to insert a row under each description group and add the quantity but I don't know how to do it.

link to img the table was built with a foreach using this code.

<?php
foreach ($resulta as $res) {
?>
    <tr>
        <th><?php echo $res['des'] ?></th>
        <th><?php echo $res['name'] ?></th>
        <th><?php echo $res['slug'] ?></th>
        <th><?php echo $res['details'] ?></th>
        <th><?php echo $res['price'] ?></th>
    </tr><?php } ?>

CodePudding user response:

One of the ways:

Use a temp variable , say $total with initial value =0 , then increment it by $res["price"]

then if the des changes (or is at the end of the foreach loop), displays this variable and reset it to 0

Sample Data

<?php
$resulta[] = array("des"=>"samsung led tv", "name"=>'', "slug"=>'', $details=>'', "price"=>"8.99");
$resulta[] = array("des"=>"samsung led tv", "name"=>'', "slug"=>'', $details=>'', "price"=>"41.99");
$resulta[] = array("des"=>"samsung led tv", "name"=>'', "slug"=>'', $details=>'', "price"=>"144.99");
$resulta[] = array("des"=>"macbook pro", "name"=>'', "slug"=>'', $details=>'', "price"=>"2499.99");
$resulta[] = array("des"=>"macbook pro", "name"=>'', "slug"=>'', $details=>'', "price"=>"649.99");
$resulta[] = array("des"=>"macbook pro", "name"=>'', "slug"=>'', $details=>'', "price"=>"148.99");
$resulta[] = array("des"=>"dell vostro 3557", "name"=>'', "slug"=>'', $details=>'', "price"=>"1499.99");
?>

PHP Code

<table border=1>
<?php
$initdesc="undefined"; $count=0; $total=0;
foreach ($resulta as $res) {

  if ($initdesc !=$res["des"]) {
       if ($count > 0) { ?>
<tr><td colspan=4 style="text-align:right;">Total<td><?php echo $total; ?>
<?php  }
    $total=0;
    $initdesc=$res["des"];
  } 
?>
    <tr>
        <td><?php echo $res['des'] ?></td>
        <td><?php echo $res['name'] ?></td>
        <td><?php echo $res['slug'] ?></td>
        <td><?php echo $res['details'] ?></td>
        <td><?php echo $res['price'] ?></td>
    </tr>

<?php 
$count  ;
$total=$total $res['price'];        
}  

if ($count!=0) { ?>
<tr><td colspan=4 style="text-align:right;">Total<td><?php echo $total; ?>
<?php } ?>

</table>

CodePudding user response:

I'd do something like this:

    // Group first your array into a multilevel array, I'm using the description as main key.
    $newArr = [];
    foreach($resulta as $val) {
        $newArr[$val['des']] = $val;
    }

    // parse the array accordingly
    foreach ($newArr as $res) {
        foreach($res as $r) { ?>
        <tr>
            <th><?php echo $r['des'] ?></th>
            <th><?php echo $r['name'] ?></th>
            <th><?php echo $r['slug'] ?></th>
            <th><?php echo $r['details'] ?></th>
            <th><?php echo $r['price'] ?></th>
        </tr>
    <?php } ?>
    <tr>
        <th colspan="5"><?php echo "This is your separator"; ?></th>
    </tr>
    }
  • Related