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

I want to show the associative array values one by one in a loop

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

我想在循环内一个一个地显示数量,因为循环内有一个计数函数,它在

内打印总数量

我的输出:

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

期望输出:

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

使用下面的代码:-

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

您还可以使用 while 循环,如下所示:-

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

警告:-

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.

希望对您有所帮助:)

您需要使用 for 循环来打印数量.. 只需使用下面的代码

  <?php echo $quantityId[$i]; ?>
"; 回声“”。++ $我。 ""; echo "".$row['name']. ""; echo "".$row['price']. ""; echo "".$quantityId[$counter]. ""; echo "".$orderArray['price'].""; 回声“”; } $counter = $counter + 1; // 或 $counter = $counter + 1; }//foreach结束 ?>