【问题标题】:Displaying full list of items in a PHP session shopping cart在 PHP 会话购物车中显示完整的商品列表
【发布时间】:2013-08-03 19:42:48
【问题描述】:

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

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

【问题讨论】:

  • session_start(); 无处可寻,还是您遗漏了它?由于您正在使用会话,因此顶部的所有页面都需要这样做。
  • 它不在购物车页面上,但是这能解决我的整个问题吗?我应该把它放在哪里?
  • 理论上我敢打赌。正如我所说,session_start(); 必须作为第一行包含在所有使用的页面中,并且位于最顶部,在您的打开 &lt;?php 标记下方。先试试看,让我知道。
  • 它根本没有任何区别 Fred - 我有两个版本的页面用于测试一个带有我的 bodged WHERE $ship_id = $cartId;"; 在 foreach 内,一个没有 -一个不显示任何东西,一个没有重复船:-(
  • 试试var_dump($ship_id)var_dump($cartId)

标签: php cart shopping


【解决方案1】:

您只能看到第一项的原因是您使用 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>";
}

【讨论】:

  • 感谢 Jan 的彻底回答,我可以检查一下....“移动爆炸”部分,我也不完全确定我应该把它移到哪里。谢谢
  • 我假设 $_SESSION['cart'] 是一串逗号分隔的值,您在 echo 部分之前爆炸。您应该在查询数据库之前执行此操作。因此,您必须将我答案中的第一部分代码放在$ships = mysql_query(... 行之前
  • 谢谢,现在就试试 - 是的,我认为你是对的,它们是 CSV 字符串
  • 我现在才得到这个...警告:mysql_fetch_assoc():提供的资源不是第 106 行 /home/elbowroo/public_html/ship_image/information_request_form_ship_info.php 中的有效 MySQL 结果资源我对代码的摆弄破坏了它:-(帮助!
  • 我刚刚编辑了答案,您在查询中使用了美元符号。这些是列名和表名,而不是 php 属性。我从你的帖子里复制过来的,没看到。请复制更新后的查询
【解决方案2】:
$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

【讨论】:

  • 感谢您花时间看这个 Anders,这正是我苦苦挣扎的地方。我已将您的代码放入循环中,我收到“警告:mysql_fetch_assoc():提供的资源不是第 106 行 /home/elbowroo/public_html/ship_image/information_request_form_ship_info.php 中的有效 MySQL 结果资源”-对不起但这实际上是我的第一个 sql 项目,我是自学的,所以不知道该使用什么,不使用什么
  • 好吧。做到这一点尽可能简单。您的会话是否包含购物车 ID,或者是否包含产品 ID 列表?本质上,会话和数据库之间的联系是什么?
  • 会话包含cartId,它只是一个数字列表(我认为是一个数组。这绝不会链接到数据库。两者之间的链接正是我想要实现的. (cartId 号有一个等效的 ship_id),这就是我想我想要做的......你会在我的购物车代码中注意到我目前正在回显 $cartId,但这只是为了测试,所以我知道商品正在添加到购物车中
  • @user2406993 如果您将表单与您的代码结合使用,请考虑一下。如果您有一组radio 按钮或checkmarks,那么您可能缺少数组括号,如[]。目前我能想到的就这些了。
猜你喜欢
  • 2015-07-03
  • 1970-01-01
  • 2021-11-27
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多