在我有for循环函数的页面中?在for($i=0;$i<=$max;$i++){ Size Query中,Size Query只从mysql数据库检索一个row,在任何时候从new product is entered?检索for loop repeat that size row again and again。
页面函数
<?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;">Item Code</font></td><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;">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;
?>
</table>
<diiv style="float:left; margin-left:42px;"><font style="font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;">
<?php echo $itemcode; ?>
<input type="hidden" name="itemcode[]" value="<?php echo $itemcode?>" /></font></div><br />
<div style="float: left;margin-left: 122px; margin-top: -14px;"><font style="font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;">
<?php echo $product?>
<input type="hidden" name="product[]" value="<?php echo $product?>" /></font></div><br />
<div style="float: left;margin-left: 387px;margin-top: -24px;"><font style="font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;">
<img name="image" id="image" src="admin/uploads/small0_<?php echo $image?>" width="150" height="150">
<input type="hidden" name="image[]" id="image" value="<?php echo $image?>" /> </font></div><br />
<div style="float: left;margin-left: 548px;margin-top: -147px;"><font style="font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;">
<?php echo get_price($id) ?>
<input type="hidden" name="price[]" id="price" value="<?php echo get_price($id)?>" /></font></div><br />
<?php //foreach ($size as $sizes) { ?>
<div style="float: left;margin-left: 625px;margin-top: -149px;"><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; ?>" /></font></div><?php //}?><br />
<div style="float: left;margin-left:686px;margin-top: -150px;"><a href="javascript:del(<?php echo $id?>)">
<input type="button" class="button5" value="Remove" /></a></div><br />
<hr style="width:800px" />
<?php
}
?>
<?php
}
else{
echo "There are no items in your shopping cart!";
}
?>大小查询
function get_size($id){
$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'];
}}Mywishlist表截图

页面截图

发布于 2013-03-11 15:28:02
因为您的get_size()函数每次调用该函数时都会重新运行查询,因此您将立即从第一行的循环中返回。
简而言之,您需要学习基本编程。
发布于 2013-03-11 15:35:07
您的问题在于qet_size()函数的构造。
首先,您的查询不是根据您的目标构建的。你的功能是
function get_size($id){但是您没有将$id考虑在内。正确的查询如下
SELECT size FROM mywishlist WHERE id = $id您的问题是,您正在获取表中所有行的,并且只按id对它们进行排序。然后循环遍历每个行,只返回所命中的第一行的大小。我提供的查询将只返回一行,然后可以返回给定的值。
注意到:PHP5.5中已不再推荐MySQL_*。使用MySQLi_*或PDO
https://stackoverflow.com/questions/15342334
复制相似问题