显示PHP会话购物车中的完整项目列表

时间:2013-08-03 19:42:48

标签: php cart shopping

我购买的购物车存在一些问题,因为它没有显示购物车中的每件商品,而是显示正确数量的商品,但每次重复添加到购物车的第一件商品。

我得到了什么错,但只是不确定如何修复它...基本上我目前只在记录集上执行一个查询,这就是显示的内容。在我对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);
?>

2 个答案:

答案 0 :(得分: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>

此外,您应该考虑切换到mysqli库,因为mysql函数已被删除。 http://www.php.net/manual/en/book.mysqli.php

答案 1 :(得分:0)

您只能看到第一项的原因是您只使用mysql_fetch_assoc()函数获取第一行。并且如其他答案所述,您不限制从数据库中提取的行数。

您的假设是正确的,应该可行,但避免在循环中使用查询。

您可以将explode(',', $_SESSION['cart']);向上移动到查询所在的位置,并在WHERE子句中给出最终的数字数组,因此您将只获得购物车中的产品。

$cartIdArray = explode(',', $_SESSION['cart']);
/* here You should check that all are integers, or apply mysql_real_escape_string() */
$query_ships = "SELECT ship_id, ship_name, image
  FROM ship_infomation
  WHERE ship_id IN(" .  implode(", ", $cartIdArray) . ")";

您可以删除$row_ships = mysql_fetch_assoc($ships);行。在输出部分而不是foreach中,使用

while($row_ships = mysql_fetch_assoc($ships))
{
  $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={$row_ships['ship_id']}'><img src='images/trash.png' width='31' height='42' /></a></td>";
  echo "</tr>";
}
相关问题