在php for循环只显示一行从mysql数据库? [英] in php for loop showing only one row from mysql database?
问题描述
在php forloop中只显示来自mysql数据库的一行? 页面功能 forloop
从mysql数据库中只检索一个行
c> $ b $
b
<?php
if (is_array($ _ SESSION ['cart'])){
echo'< tr bgcolor =#FFFFFFstyle =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;>产品名称< / font>< / td>
< td align =center>< font style =font-family:Arial,Helvetica,sans-serif; font-size:14px; color:#000;&字体>< / 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 =hiddenname =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 =hiddenname =product []value =<?php echo $ product?> />< / font>< / div>< br /
< input type =hiddenname =image []id =imagevalue =<?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 =hiddenname =price []id =pricevalue =<?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; size:15px; color:#000;>
<?php echo $ size; ?>
< / font>< input type =hiddenname =sizevalue =<?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 =buttonclass =button5value =Remove/>< / a>< / div>< br /
< hr style =width:800px/>
<?php
}
?>
<?php
}
else {
echo您的购物车中没有商品!
}
?>
大小查询
function get_size($ id){
$ result = mysql_query(SELECT size FROM mywishlist order by id)或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')或die Size Problem。< br />< br />。mysql_error());
while($ row = mysql_fetch_array($ result)){
return $ row ['size'];
}}
Mywishlist表截图
< img src =https://i.stack.imgur.com/PUUxb.pngalt =现在看到大小列中有两个大小>
网页截图:
使用此函数时
function get_size($ id){
$ result = mysql_query(SELECT size FROM mywishlist order by id)或die(My Wish Size Problem。br />< br / );
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 =hiddenname =sizevalue =<?php echo $ size;?& />
< / font>< / div><?php}?>< br />
使用Array& Foreach循环
:
将您的函数更改为:
code> function get_size($ id){
$ result = mysql_query(SELECT size FROM mywishlist order by id)或die(My Wish Size Problem >< br />。mysql_error());
while($ row = mysql_fetch_array($ result)){
$ size [] = $ row ['size'];
}
return $ size;因为在你的函数中,你只返回第一行并返回结束符,因为它是一个字符串,因为它是一个字符串。 while循环。in php forloop showing only one row from mysql database?In The
forloop
retrieve only onerow
from mysql database andfor loop repeat that size row again and again
when evernew 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
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
解决方案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.
这篇关于在php for循环只显示一行从mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!