Echo Array成新线

时间:2016-11-15 02:41:36

标签: php mysql arrays

我有一个编辑页面,用户可以在其中编辑发票表单。我需要将数组回显到表中,但是将每个数组分成表中的新行。

当我回应这个价值时,它聚集成一行。当我将值回显到输入字段时,我只从数组中获取一条记录。

这是我桌子上的样子:

enter image description here

<?php

$id = $_GET['id'];

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

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

$partnumber = $res['partnumber'];

$partdescription = $res['partdescription'];

$partprice = $res['partprice'];

$partquantity = $res['partquantity'];


}
>

然后是表格:

<tr>

    <td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $partnumber;?>></td>
    <td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $partdescription;?>></td>
    <td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $partprice;?>></td>
    <td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $partquantity;?>></td>


</tr>

我明白了: enter image description here

 <tr>

    <td><?php echo $partnumber;?></td>
    <td><?php echo $partdescription;?></td>
    <td><?php echo $partprice;?></td>
    <td><?php echo $partquantity;?></td>


</tr>

我明白了: enter image description here

我尝试了以下操作,但它们都会产生空白回声:

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

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

请帮忙

EDIT ***************

一旦我让桌子工作,我将解决安全问题。

我试过了,输出仍在一行。

<?php

$id = $_GET['id'];


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


while($res = mysqli_fetch_array($result))
{
?>
 <tr>
  <td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $res['partnumber'];?>></td>
  <td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $res['partdescription'];?>></td>
  <td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $res['partprice'];?>></td>
  <td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $res['partquantity'];?>></td>

<?php
}
?>

这有帮助吗?

enter image description here

enter image description here

请注意,它只显示1条记录,因为我在一条记录中有多个值,而我们用逗号分隔它们。请检查上面的截图。这是代码:

$partnumber = $_POST['partnumber'];
$partnumberarray = implode( ", ", $partnumber);


$partdescription = $_POST['partdescription'];
$partdescriptionarray = implode( ", ", $partdescription);


$partprice = $_POST['partprice'];
$partpricearray = implode( ", ", $partprice);


$partquantity = $_POST['partquantity'];
$partquantityarray = implode( ", ", $partquantity);



$result = mysqli_query($mysqli, "INSERT INTO invoicespartnumber, partdescription, partprice, partquantity, login_id) VALUES('$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')");

无论如何,我可以将1条记录中的值回显到桌面上的多行中吗?

2 个答案:

答案 0 :(得分:0)

你必须把你的html放在你对结果数组做的循环中。

看起来像这样。

 <?php

 $id = $_GET['id'];

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

 while($res = mysqli_fetch_array($result)){ //close php here to easier write html. ?> 
 <tr>
      <td><input type="text" class="input-small" name="partnumber[]" value="<?php echo $res['partnumber'];?>"></td>
      <td><input type="text" class="input-small" name="partdescription[]" value="<?php echo $res['partdescription'];?>"></td>
      <td><input type="text" class="input-small" name="partprice[]" size="4" value="<?php echo $res['partprice'];?>"></td>
      <td><input type="text" class="input-small" name="partquantity[]" size="4" value="<?php echo $res['partquantity'];?>"></td>
 </tr>

 <?php //open php tag again to add end scope to while loop
 }
 ?>

答案 1 :(得分:0)

<table width = "100%">
<thead>
<tr>
// here will be all columns names in th
</tr>
</thead>
<tbody>
<?php

$id = $_GET['id'];

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

while($res = mysqli_fetch_array($result))
{ ?>
<tr>
<td><?php echo $res['partnumber']; ?></td> <!-- Also use input box here -->

<td><?php echo $res['partdescription'];?><td>

<td><?php echo  $res['partprice']; ?></td>
<td><?php echo  $res['partquantity'];?></td>
</tr>
<?php
}
?>
相关问题