简体   繁体   English

在一个html表中显示多个数组

[英]Display multiple arrays in a html table

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截图

在此处输入图片说明

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; 
 }
 ?>

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.首先,您的代码容易受到 SQL 注入的影响,在将其放入查询字符串之前,请确保保护$id (例如$id = (int)$_GET['id']; )。

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
    }

?>

If you need new line for each column must simply add new tr for each td如果您需要为每列换行,则必须为每个td添加新的tr

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; ?>

Notice the change in the starting curly braces, and that i removed the endforeach you used, and the table tags...注意起始花括号的变化,我删除了你使用的 endforeach 和表标签......

Now you can make use of this in your for each loop.现在您可以在 for each 循环中使用它。

 <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我认为这应该可以解决问题

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>
    <? 
    }

    }

    ?>

在此处输入图片说明

 $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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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