$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
答案 0 :(得分:0)
使用以下代码: -
<?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_ *在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 相反,应该使用MySQLi或PDO_MySQL扩展。
希望它会对你有所帮助:)。
答案 1 :(得分:0)
你需要使用for循环来打印数量.. 只需使用以下代码
<?php echo $quantityId[$i]; ?>
答案 2 :(得分:0)