简体   繁体   English

html表中仅显示数组的最后结果(php,mysql)

[英]Only last result of an array showing in html table (php, mysql)

TEST SITE Look here to see code in action 测试站点查看此处查看运行中的代码

I have two arrays which when echo'd show exactly what i want it to (look at the test site). 我有两个数组,当回显时它们会准确显示我想要的内容(查看测试站点)。 I'm trying to get them both into a html table but it is only showing the last entry. 我正在尝试将它们都放入html表中,但仅显示最后一个条目。 I have shoved the entire array code into the table and it works fine (although there all in the same row and not separated) but as it is being used in a html email i'm not sure if this will be safe? 我已经将整个数组代码推到表中,并且工作正常(尽管所有代码都在同一行中并且没有分开),但是由于它正在html电子邮件中使用,所以我不确定这是否安全? I'm sorry if this is a really simple fix i'm new to php/mysql so the simple things seem impossible at the moment. 如果这是一个非常简单的修复程序,我很抱歉,我是php / mysql的新手,所以目前看来简单的事情似乎是不可能的。 I also know i could no doubt combine the two arrays but im on a KISS mantra at the moment and this is the easiest for me to understand. 我也知道我无疑可以将这两个数组结合起来,但是目前我是在接吻的咒语中,这对我来说是最容易理解的。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

<?php
    //Get Order Codes
    foreach ($ids as $id) {
    $sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
    $result = mysql_query($sqlcode);

    while($row = mysql_fetch_assoc($result)) {
    $codes['codes'] = $row['od_code'];
    } 
    echo "".$codes['codes']."<br />";
    }

    //Get Product Name
    foreach ($ids as $id) {
    $sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
    $result = mysql_query($sqlname);

    while($row = mysql_fetch_assoc($result)) {
    $names['names'] = $row['pd_name'];
    } 
    echo "".$names['names']."<br />";
    }   
?>

<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
</table>
$names['names'][] = $row['pd_name'];

<td> <?php foreach($names['names'] as $name){ echo "". $name.":"."<br>" } ?> </td>

Or as suggested in comments: 或在评论中建议:

while($row = mysql_fetch_assoc($result)) {
    $names['names'] = $row['pd_name'];
    $codes['codes'] = $row['pd_code'];
?>
<tr>
<td> <?php echo "". $names['names'].":"."<br>"?> </td>
<td> <?php echo "". $codes['codes']."<br>"?> </td>
</tr>
<?php } ?>

Please try to use PDO instead for interacting with mysql http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ 请尝试使用PDO代替与mysql进行交互http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

您必须遍历表中的数组,方法与在表标签上方相同。

you are using 您正在使用

foreach($ids as $id) make table to be populated inside foreach or else use 2d array $name and $codes and count the array size and loop your table structure for count no of times foreach($ids as $id)使表填充到foreach内,否则使用2d数组$ name和$ codes并计算数组大小并循环表结构进行无次数计数

A better solution 更好的解决方案

Change your logic so that you only use one SQL query and make use of a join. 更改逻辑,以便仅使用一个SQL查询并使用联接。

SELECT p.pd_name,
       oc.od_code
FROM tbl_product p
LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
WHERE p.pd_id = $id

So this should be the following using PDO: 因此,使用PDO应该如下所示:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("
    SELECT p.pd_name,
           oc.od_code
    FROM tbl_product p
    LEFT JOIN tbl_order_code oc ON oc.pd_id = p.pd_id
    WHERE p.pd_id = ?");

if ($stmt->execute(array($id))) {
  while ($row = $stmt->fetch()) {
      // print out table rows here
  }
}

Immediate fix (yuck) 立即修复(糟糕)

This should solve your immediate problem, but there is a much better way of doing this. 这应该可以解决您眼前的问题,但是有一种更好的方法。

<?php
foreach ($ids as $id) {
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT 1";
$result = mysql_query($sqlcode);
$codes = array();
while($row = mysql_fetch_assoc($result)) {
    $codes[] = $row['od_code'];
}    


//Get Product Name
foreach ($ids as $id) {
$sqlname = "SELECT pd_name FROM tbl_product WHERE pd_id = $id";
$result = mysql_query($sqlname);
$names = array();
while($row = mysql_fetch_assoc($result)) {
    $names[] = $row['pd_name'];
}
}
?>

<table width='550' border='1' align='center' cellpadding='5' cellspacing='1'>
<tr>
<td>Description</td>
<td>Code</td>
</tr>
<?php foreach($names as $key => $name): ?>
<tr>
<td> <?php echo $name .":"."<br>"?> </td>
<td> <?php echo $codes[$key]."<br>"?> </td>
</tr>
<?php endforeach; ?>
</table>

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

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