doz22551 2016-02-19 06:45
浏览 45
已采纳

我想在循环中逐个显示关联数组值

$order=mysql_query("SELECT * FROM `order_details` WHERE id='".$_GET['id']."'") or die(mysql_error());
$orderArray=mysql_fetch_array($order);

$productId=explode(',',$orderArray['product_id']);
$productId=(array_values($productId));
$quantityId=explode(',',$orderArray['quantity']);
$quantityId=(array_values($quantityId));
print_r($quantityId);

$i=0;
foreach($productId as $id)  
{   
    $i++;
    //echo "SELECT * FROM products WHERE id=$id";
    $productId=mysql_query("SELECT * FROM products WHERE id=$id");  
    $productArray=mysql_fetch_array($productId);
?>
       <tr>
          <td><?php echo $i;?></td>
          <td>
              <?php echo $productArray['name'];?>
          </td>
          <td>
              <?php echo $productArray['price'];?>
          </td>
          <td>
              <?php 
                  for($j=0;$j<count($quantityId);$j++){
                      echo $quantityId[$j];
                  }   

              ?>
          </td>
          <td>
              <?php 

                      echo $orderArray['price'];
              ?>
          </td>
      </tr>   
<?php
}
?>  

I want to show the quantity one by one the inside the loop and as there is a count function inside the loop it prints the total quantity inside

My output:

Sl No.  Name             Price      Quantity    
1   watches2              100            12                                 
2   pens1                 100            12 

desired output:

Sl No.  Name             Price      Quantity    
1   watches2            100              1                                  
2   pens1               100              2  
  • 写回答

3条回答 默认 最新

  • doulin9679 2016-02-19 07:00
    关注

    Use below code:-

    <?php
    
    $order=mysql_query("SELECT * FROM `order_details` WHERE id='{$_GET['id']}'") or die(mysql_error());
    // Use mysql_fetch_assoc here
    $orderArray=mysql_fetch_assoc($order);
    // No need to do explode.  assign directly to query
    $productId=$orderArray['product_id'];
    // no need of below line explode set array index from 0 same as array_values does
    //$productId=(array_values($productId));
    $quantityId=explode(',',$orderArray['quantity']);
    // no need of below line explode set array index from 0 same as array_values does
    //$quantityId=(array_values($quantityId));
    
    //foreach($productId as $id){   // No need for this loop
    
    // Use IN in mysql query
    $sql = "SELECT * FROM products WHERE id IN ($productId)";
    $result = mysql_query($sql);
    
    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }   
    
    // Use mysql_fetch_assoc here
    for ($i = 0; $row = mysql_fetch_assoc($result); ++$i){   
        echo "<tr>";
        echo "<td>".$row['name']. "</td>";
        echo "<td>".$row['price']. "</td>";
        echo "<td>".$quantityId[$i]. "</td>";
        echo "<td>".$orderArray['price']."</td>";
        echo "</tr>";    
    }
    //}
    

    You can also use while loop as below:-

    $counter = 0;
    while($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>".$row['name']. "</td>";
        echo "<td>".$row['price']. "</td>";
        echo "<td>".$quantityId[$counter]. "</td>";
        echo "<td>".$orderArray['price']."</td>";
        echo "</tr>"; 
        $counter++; // or $counter = $counter + 1;
    }
    

    Warning:-

    mysql_* was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    Hope it will help you :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口