【问题标题】:Shopping cart output repeating first entry购物车输出重复第一个条目
【发布时间】:2013-07-28 10:11:25
【问题描述】:

我在我正在构建的网站上设置了一个购物车。我可以轻松地添加商品、删除商品和清除购物车。我遇到的问题实际上是显示购物车中的项目,或者更多地显示相关数据库条目。

PHP 会话变量保存与用于识别数据库中项目的 ID 号相关的数字。然后我需要购物车显示图像、名称、删除选项和 ID 号。当显示购物车时,如果有多个项目,图像和名称始终是购物车中的第一个项目,每个条目重复,而 ID 号确实会改变。

这让我觉得我只是在执行一个查询,而实际上我需要在下面我的第一个代码段的 foreach 循环中运行查询。作为 PHP 的新手,我不太确定解决此问题的最佳方法。我刚刚通过 Dreamweaver(第二个代码)添加了一个记录集。

有人能指出指导我解决小问题的最佳方向吗?

非常感谢

购物车展示:

    <?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);
?>

【问题讨论】:

    标签: php duplicates cart recordset


    【解决方案1】:

    您绝对不需要在循环中运行查询;这违背了 SQL 数据库的性质/意图(简而言之 - 除非数据库设计不佳 - 一个查询 = 快,许多迭代查询 = 慢)。

    问题似乎与您的 foreach 循环有关。在第二个代码块中获取的数据数组是 $row_ships,您可以在 foreach 循环中使用它来分配 $ship_name 和 $ship_image。

    但是,我没有看到您循环访问 $row_ships 数组。您的循环通过 $array 前进(通过 $_SESSION['cart'] 字符串在循环外部填充),为每次迭代提供不同的 $cartId。但是,$ship_name = $row_ships['ship_name'] 对于每次迭代都是相同的分配(这也适用于 $image)。

    如果 $cartId 与表中的 ship_id 对应,您需要在循环中建立该连接,以便为 $cartId 的每个值使用正确的 ship_name 和图像。

    【讨论】:

    • 感谢您抽出宝贵的时间做出很好的回应。这就是我的猜测,我想你可能会理解我将查询移动到循环中的糟糕逻辑。你能解释一下我应该如何链接 $cartId 和 ship_name,我不知道如何开始。
    • 如果它们需要链接,就必须有一个关系:即知道哪个cartId 与哪个表行对应的方法。我无法推断出太多可能是什么,因为我不知道您的架构等。您的会话购物车变量是一个逗号分隔的购物车 ID 字符串,是吗?当产品添加到购物车时,您是否添加到它?该 id 究竟代表什么?如果它可以与数据库中的产品 id 匹配,该产品 id 可以与 ship_id 匹配,那么匹配起来就没有问题。如果没有,您可以考虑在会话数据中添加更多信息,而不仅仅是 cartId。
    • 嗨 Centuren,你说的很对,购物车是一组 CSV... 每个 cartId 值都是数字。这些项目确实添加在每个产品页面上。产品都有自己的 ID (ship_id)。 cartId 和 ship_id 的实际数字是相同的,因此我认为这是一个更简单的过程!你认为这很容易解决吗?谢谢你的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2022-07-05
    相关资源
    最近更新 更多