1

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

4
  • Do all the fields have an equal amount of comma separated values? A hint: $one[0] returns the first value, $two[0] and $three[0] as well... Commented Jan 13, 2022 at 8:23
  • @MartyMcVry yes, all columns have equal amount of values Commented Jan 13, 2022 at 8:25
  • @mplungjan it works for first column, i want to display all three columns Commented Jan 13, 2022 at 8:26
  • 1
    Try fetching the number of values there are, and then looping through 0 --> numvalues - 1 using a regular for-loop. (Or a foreach with $key and $value pairing, so you can use $key as an index for $two and $three.) Commented Jan 13, 2022 at 8:33

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.