dongxiai3003 2013-03-12 09:22
浏览 104

在php for循环中只显示mysql数据库中的一行?

in php forloop showing only one row from mysql database?In The forloop retrieve only one row from mysql database and for loop repeat that size row again and again when ever new product is entered?

Page Function

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

Size Query

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'];
}}

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

Mywishlist Table Screenshot now see there are two sizes in the size column

Page Screenshot page screenshot

When I Used This Function

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)){
 $size[] = $row['size'];
}

return $size;

}

 <?php foreach ($size as $sizes) { ?>
<div style="float: left;margin-left: 625px;margin-top: -131px;">
<font style="font-family:Arial,Helvetica,sans-serif;font-size:15px; color:#000;">
<?php echo $sizes; ?> 
<font><input type="hidden" name="size" value="<?php echo $size; ?>" />
</font></div><?php }?><br />

Result After Using Array & Foreach Loop

enter image description here

  • 写回答

1条回答 默认 最新

  • dongxi0605 2013-03-12 09:25
    关注

    change your function to:

    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)){
     $size[] = $row['size'];
    }
    
    return $size;
    
    }
    

    Because in your function you only returned first row and return terminates the while loop.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图