简体   繁体   中英

how to display comma separated values from multiple columns in php inside html table

i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:

<table class="table custom-table m-0">
  <thead>
    <tr>
      <th>Description</th>
      <th>Make</th>
      <th>UOM</th>
    </tr>
  </thead>
  <tbody>

    <?php 
    $one=explode(',', $row['description']);
    $two=explode(',', $row['make']);
    $three=explode(',', $row['uom']);
    foreach($one as $ones) {
     ?>
    <tr>
      <td>
        <?php echo $ones?>
      </td>
      <td></td>
      <td></td>

    </tr>
    <?php }?>
  </tbody>
</table>

here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance

Use a counter - assuming exact same number of entries per row

http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e

<? 
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);    
$i=0;
?>

<table class="table custom-table m-0">
  <thead>
    <tr>
      <th>Description</th>
      <th>Make</th>
      <th>UOM</th>
    </tr>
  </thead>
  <tbody>

    <?php 
    foreach($one as $ones) {
     ?>
    <tr>
      <td><?php echo $ones; ?></td>
      <td><?php echo $two[$i]?></td>
      <td><?php echo $three[$i]?></td>

    </tr>
    <?php $i++;}?>
  </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM