1

php forloopでmysqlデータベースから1行だけを表示していますか?では、mysqlデータベースから1行だけforloop取得rowfor loop repeat that size row again and again、いつでもnew product is entered?

ページ機能

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

 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 テーブルのスクリーンショット サイズ列に 2 つのサイズが表示されるようになりました

ページのスクリーンショット ページのスクリーンショット

この機能を使ったとき

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 />

配列と foreach ループを使用した後の結果

ここに画像の説明を入力

4

1 に答える 1

0

関数を次のように変更します。

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;

}

関数では最初の行のみを返し、return は while ループを終了するためです。

于 2013-03-12T09:25:17.553 に答える