0

カート内の各アイテムが表示されず、正しい数のアイテムが表示されますが、毎回カートに追加された最初のアイテムが繰り返されるという点で、セットアップしたショッピング カートに問題があります。

何が問題なのかわかりましたが、それを修正する方法がわかりません...基本的に現在、レコードセットに対して1つのクエリのみを実行しており、それが表示されています。私のPHPの基本的な知識では、カート内の各アイテムに対してクエリを実行する必要があると言えます(最大6つしかないため、大きくなることはありません)。

カート内の数字は、データベースの ship_id 番号と同じです.... したがって、次のようなものを入れることができると考えていました:

   $sql = "SELECT $ship_id, $ship_name, image
    FROM $ship_infomation
    WHERE $ship_id =  $cartId;";

... foreach ループの内部ですが、これにより問題が発生します

私の小さな問題を解決するために私を導くための最良の方向を誰かが指摘できますか.

どうもありがとう

ショッピングカートの表示:

 <?php 
    $cart = $_SESSION['cart'];
  if (!$cart) {echo "<div class='dialogue_box_inside_info_request'>
 <div class='dialogue_box_image'>
    <img src='images/basket_empty_dialogue.png' width='618' height='264' />
    </div>
</div>";}
  else
  {

      $array = explode(',', $_SESSION['cart']);

  echo "<table width='835' border = '0'>";
  foreach($array as $cartId) {
  $ship_name = $row_ships['ship_name'];
  $ship_image = $row_ships['image'];
  echo "<tr>";
  echo "<td width='110' height='58' class='table_text'><img src='images/ships/$ship_image'width='83' height='53' /> </td>";
  echo "<td width='620' height='58' class='table_text'>$ship_name</td>";
  echo "<td width='35' height='58' class='table_text'><a href='cart.php?action=delete&ship_id=$cartId'><img src='images/trash.png' width='31' height='42' /></a></td>";
  echo "<td height='58'>$cartId</td>";
  echo "</tr>";
  } 
  echo "</table>";

  }
   ?> 

コードの先頭に配置されたレコードセット

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_ships, $ships);
$query_ships = "SELECT ship_id, ship_name, image FROM ship_infomation";
$ships = mysql_query($query_ships, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
?>
4

2 に答える 2

0
$row_ships = mysql_fetch_assoc($ships);

この行は、ループ内に配置する必要があります。

すべてを整理する方法の例。すべて独自の変数名を使用しているため、少し置換する必要があります。セッションでは、すべてのアイテムの配列をカートに入れます。現在のように、これをコンマなどで区切られた文字列として保存する必要はありません。やるだけ$_SESSION['cart'] = array()

製品を追加するには、プッシュ機能を使用します。 $_SESSION['cart'].push($itemid);

カートから製品を削除するには、配列を繰り返し処理して正しいエントリを見つけ、それを設定解除する必要があります。今はそのコードを実行しません。あなたを正しい方向に向けているだけです。

製品/アイテムの ID をセッションに保存する場合、次のようなクエリを作成できます。

<table>
<?php
$where = implode(', ', $_SESSION['cart']);
$rec = mysql_query("SELECT * FROM product WHERE id IN ($where)");
while($row = mysql_fetch_assoc($rec)) {
    // Output whatever you need.
    echo "<tr>";
    echo "<td><img src='images/ships/$row['image']</td>";
    echo "<td>$row['name']</td>";
    echo "</tr>"
}
?>
</table>

また、mysql 関数は廃止されているため、代わりに mysqli ライブラリへの切り替えを検討する必要があります。http://www.php.net/manual/en/book.mysqli.php

于 2013-08-03T19:54:08.957 に答える