PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 3, 2022

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

 August 03, 2022     html, html-table, mysql, php, sql     No comments   

Issue

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


Solution

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>


Answered By - mplungjan
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing