0

私がループ機能を持っているページで?mysqlデータベースからfor($i=0;$i<=$max;$i++){ Size Query1つだけ取得し、いつでもrowfor loop repeat that size row again and againnew 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'];
}}

Mywishlist テーブルのスクリーンショット サイズ列に 2 つのサイズが表示されるようになりました

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

4

2 に答える 2

1

関数を呼び出すたびに関数がget_size()クエリを再実行するため、最初の行のループからすぐに戻ります。

つまり、基本的なプログラミングを学ぶ必要があります。

于 2013-03-11T15:28:02.903 に答える
0

あなたの問題は、qet_size()関数の構築にあります。

まず、目的に基づいてクエリが正しく構築されていません。あなたの機能は

function get_size($id){

$idしかし、あなたは考慮に入れません。適切なクエリは次のようになります

SELECT size FROM mywishlist WHERE id = $id

あなたの問題は、テーブル内のすべてidの行を取得していて、 . 次に、それぞれをループして、ヒットした最初の行のサイズのみを返します。私が提供したクエリは、指定された値を返すことができる 1 つの行のみを返します。

注意: MySQL_* は PHP 5.5 で非推奨になりました。MySQLi_* または PDO を使用

于 2013-03-11T15:35:07.287 に答える