dongxun6690 2011-04-19 09:17
浏览 54
已采纳

只有在html表中显示的数组的最后结果(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. 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? 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. 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>
  • 写回答

4条回答 默认 最新

  • dongzi1209 2011-04-19 09:23
    关注

    A better solution

    Change your logic so that you only use one SQL query and make use of a join.

    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:

    $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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行