1

I'm trying to display multiple arrays into a table so that each value is on a separate line within the table.

This is my current setup:

<?php

$id = $_GET['id'];

$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");

while($res = mysqli_fetch_array($result))
{
?>

 <tr>
   <td><? echo $res['partnumber']; ?></td>
   <td><? echo $res['partdescription']; ?></td>
   <td><? echo $res['partprice']; ?></td>
   <td><? echo $res['partquantity']; ?></td>

<?php
    }

?>

Which displays the following:

Screenshot

enter image description here

instead i need it to display each value on a separate line

I tried the following but it duplicates its value over and over again.

<? foreach ($res as $row) : ?>
 <tr>
   <td><? echo $row['partnumber']; ?></td>
   <td><? echo $row['partdescription']; ?></td>
   <td><? echo $row['partprice']; ?></td>
   <td><? echo $row['partquantity']; ?></td>
 </tr>
 <? endforeach; 
 }
 ?>
2
  • looks like your values are concatenated strings. Is "asdf, aasdfas, asdf" single database value? Commented Nov 8, 2016 at 5:40
  • $partnumber = $_POST['partnumber']; $partnumberarray = implode( ", ", $partnumber); Commented Nov 8, 2016 at 15:19

5 Answers 5

1

First of all, your code is vulnerable to SQL injection, make sure to secure $id (ex. $id = (int)$_GET['id'];) before putting it into your query string.

then try:

while($res = mysqli_fetch_array($result))
{
?>
 <tr>
   <td><? echo $res['partnumber']; ?></td>
 </tr><tr>
   <td><? echo $res['partdescription']; ?></td>
 </tr><tr>
   <td><? echo $res['partprice']; ?></td>
 </tr><tr>
   <td><? echo $res['partquantity']; ?></td>
 </tr>
<?php
    }

?>
Sign up to request clarification or add additional context in comments.

Comments

0

If you need new line for each column must simply add new tr for each td

Try this:

<? foreach ($res as $row) : ?>
    <tr>
      <td><? echo $row['partnumber']; ?></td>
    </tr>
    <tr>
      <td><? echo $row['partdescription']; ?></td>
    </tr>
    <tr>
      <td><? echo $row['partprice']; ?></td>
    </tr>
    <tr>
      <td><? echo $row['partquantity']; ?></td>
    </tr>
 <? endforeach; ?>

Comments

0

Notice the change in the starting curly braces, and that i removed the endforeach you used, and the table tags...

Now you can make use of this in your for each loop.

 <table>
 <? foreach ($res as $row) { ?>
    <tr>
    <td><? echo $row['partnumber']; ?></td>
    <td><? echo $row['partdescription']; ?></td>
    <td><? echo $row['partprice']; ?></td>
    <td><? echo $row['partquantity']; ?></td>
    </tr>
 <? 
     }
 ?>
 </table>

I think this should do the trick

Comments

0

This is what i'm getting with your code: (i'll fix the security issue after i get this working, thank you)

    <?php

    $id = $_GET['id'];


    $result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id");

    while($res = mysqli_fetch_array($result))
    {
    ?>
    <? foreach ($res as $row) { ?>
<tr>
<td><? echo $row['partnumber']; ?></td>
<td><? echo $row['partdescription']; ?></td>
<td><? echo $row['partprice']; ?></td>
<td><? echo $row['partquantity']; ?></td>
</tr>
    <? 
    }

    }

    ?>

enter image description here

Comments

0
<?php
$arr1 = ['name'=>'jon', 'age'=>30, 'address'=>'dilly'];
$arr2 = ['name'=>'gita', 'age'=>20, 'address'=>'goha'];
$arr3 = ['name'=>'sita', 'age'=>20, 'address'=>'pune'];

$agroup = array([$arr1, $arr2, $arr3]);
 
?>

<table>
  <?php foreach($agroup as list($a, $b, $c)){ ?>
  <tr>
    <td><?php echo $a['name']; ?></td>
    <td><?php echo $b['name']; ?></td>
    <td><?php echo $c['name']; ?></td>
  </tr>
  <tr>
    <td><?php echo $a['age']; ?></td>
    <td><?php echo $b['age']; ?></td>
    <td><?php echo $c['age']; ?></td>
  </tr>

  <tr>
    <td><?php echo $a['address']; ?></td>
    <td><?php echo $b['address']; ?></td>
    <td><?php echo $c['address']; ?></td>
  </tr>

<?php } ?>
</table>

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.