1

for loop repeating single row如果来自mysql数据库,我该怎么办?是否有购物车页面和for($i=0;$i<=$max;$i++){循环从 mysql 数据库重复单行?以及如何在 for 循环中从数据库中检索多行?

查询功能

function get_size(){
$result=mysql_query("SELECT size FROM mywishlist order by id") 
or die("My Wish Size Problem"."<br/><br/>".mysql_error());
while($row=mysql_fetch_array($result)){
return $row['size'];
}}

购物车页面

<?php
if(is_array($_SESSION['cart'])){
echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td align="center"><font   style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Name</font></td>
<td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Image</font></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Price</font></td>
<td><div><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Size</font></div></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Amount</font></td>
<td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Options</font></td></tr>';

$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$id=$_SESSION['cart'][$i]['id'];
$q=$_SESSION['cart'][$i]['qty'];
$product=get_product_name($id);
$image=get_product_image($id);
$ids=get_id($id);
$itemcode=get_itemcode($id);
$size=get_size($id);
if($q==0) continue;
?>

<tr bgcolor="#FFFFFF"><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="itemcode[]" value="<?php echo $itemcode?>" /><input type="hidden" name="ids[]" value="<?php echo $ids?>" /><input type="hidden" name="product[]" value="<?php echo $product?>" /><?php echo $product?></font></td><td align="center"><input type="hidden" name="image[]" id="image"  value="<?php echo $image?>"  /><img name="image" id="image" src="admin/uploads/small0_<?php echo $image?>" width="150" height="150"></td>

 <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;">  <input type="hidden" name="price[]" id="price" value="<?php echo get_price($id)?>"/>Rs.<?php echo get_price($id)?></font></td>
 <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><?php echo $size ?></font><input type="hidden" name="size" value="<?php echo $size?>" /></td>                    
 <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="amt[]"  value="<?php echo get_price($id)*$q?>"/> Rs.<?php echo get_price($id)*$q?></font></td>
 <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><a href="javascript:del(<?php echo $id?>)"><input type="button" class="button5" value="Remove" /></a></font></td></tr>
 <?php                 
 }
 ?>
<tr><td colspan="6" align="right"><b><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><input type="hidden" name="total" id="total" value="<?php echo get_order_total()?>"/>Order Total:Rs.<?php echo get_order_total()?></font></b></td></tr>        
<tr><td colspan="6" align="right"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000; !important">Cash On Delivery & Free Shipping</font></td></tr>  
<tr><td colspan="6" align="right">

<?php  $query1=mysql_query("SELECT * FROM category ORDER BY id ") or die ('Product Query Problem');
$row4=mysql_fetch_array($query1);$ids=$row4['id'];$cat_name=$row4['categories'];?>   <input type="button" class="button1" value="Continue Shopping"  onclick="window.location='product.php'" /><input type="button" class="button2"  value="Clear Cart" onclick="clear_cart()"><input type="button" class="button3" value="Update Cart" onclick="update_cart()"><input type="submit" class="button4" name="order" id="order" value="Place Order"></td></tr>

 <?php
 }
 else{
 echo "<tr bgColor='#FFFFFF'><td><font style='font-family:Arial,Helvetica,sans-serif;    font-size:15px; color:#000;'>There are no items in your shopping cart!</font></td>";
}
?>
4

3 回答 3

0

要遍历结果中的所有行,您可以尝试

foreach($row=mysql_fetch_assoc($result))
{
print_r($row);
}

注意: mysql_* 函数不适用于任何新代码。

于 2013-03-11T08:20:39.920 回答
0

调用return. 您需要先获取所有行,然后返回结果数组:

function get_size(){

    $result=mysql_query("SELECT size FROM mywishlist order by id") 
    or die("My Wish Size Problem"."<br/><br/>".mysql_error());

    $results = array();
    while($row=mysql_fetch_array($result)){
        $results[] = $row;
    }
    return $results;
}
于 2013-03-11T08:22:04.917 回答
0

在while循环中,您需要设置数组来收集数据库信息...

 function get_size(){
   $result=mysql_query("SELECT size FROM mywishlist order by id")or die("My Wish Size Problem"."<br/><br/>".mysql_error());

   while($row=mysql_fetch_array($result)){

      $rows[]  = $row;

   } 

  return $rows;
}
于 2013-03-11T08:54:00.527 回答