Ajax购物车;t显示项目


Ajax shopping cart doesn't show items

我正在尝试制作一个非常简单的ajax购物车。到目前为止,我已经有了购物车,基于ajax功能,我看到当我单击Add to Cart按钮时,购物车中的项目数量增加了。

现在的问题是显示购物车按钮。当我试着滑动推车时,它不会滑动。我认为问题出在PHP部分,但我在数据库中循环以获取所有ID并显示结果。。至少这个功能不起作用,这就是为什么我认为问题存在的原因。

这是PHP部分:

// counting items in cart and showing on the page - work
if(isset($_POST['total_cart_items']))
{
   echo count($_SESSION['itemid']);
   exit();
}
// post item into cart - work
if(isset($_POST['item_id']))
{
    $_SESSION['itemid'][]=$_POST['item_id'];
    echo count($_SESSION['itemid']);
    exit();
}
// this part is the problem as doesn't show the cart
if(isset($_POST['showcart']))
{
    for($i=0;$i<count($_SESSION['itemid']);$i++)
    {
        $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
        $result = $pdo->prepare($sql);
        $result->execute(array($_SESSION['itemid']));                 
        foreach ( $result as $row ){
            echo '<div class="cart_items" style="text-align:center;">
                    <a href=""><p>'.$row["itemid"][$i].'</p></a>
                  </div>';
        }
    }
    exit(); 
} 

这是ajax/js部分

$(document).ready(function(){
  $.ajax({
    type:'post',
    url:'includes/store_items.php',
    data:{
      total_cart_items:"totalitems"
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });
});
function cart(itemid)
{
    var ele=document.getElementById(itemid);    
    $.ajax({
    type:'post',
    url:'includes/store_items.php',
    data:{
      item_id:itemid        
    },
    success:function(response) {
      document.getElementById("total_items").value=response;
    }
  });
}
function show_cart()
{
  $.ajax({
  type:'post',
  url:'includes/store_items.php',
  data:{
    showcart:"cart"
  },
  success:function(response) {
    document.getElementById("mycart").innerHTML=response;
    $("#mycart").slideToggle();
  }
 });
}

最后一个函数function show_cart()就是问题所在。如果需要的话,这里还有HTML,但这也可以。

<p id="cart_button" style="text-align:center;" onclick="show_cart();">
    <img src="img/cart_icon.png">
    <input type="button" id="total_items" value="">
</p>
<div id="mycart" style="text-align:center;"></div>  

更新:

    for($i=0;$i<count($_SESSION['itemid']);$i++)
    {
        $sql = "SELECT * FROM document_upload where upload_id = ?"; 
        $result = $pdo->prepare($sql);
        $result->execute(array($_SESSION['itemid'])); 
        foreach ( $result as $row ):?>
          <div class="cart_items" style="text-align:center;">
               <a href=""><p><?=$row["itemid"][$i]?></p></a>
          </div>
       <?php endforeach; ?>
    }

$result是包含查询、参数。。。用于准备查询、绑定参数、执行。。。但是他不包含结果。。。

你有不同的方法来获得结果:

$row = $result->fetch(PDO::FETCH_ASSOC); //load ONE row as array(colName => colValue)... First call: returns the first row. Second call, the second one... When there is no more rows, returns FALSE
$rows = $result->fetchAll(); //the one we use: a big array with all rows returned . We 'll do like this one...

此外,我有一个更好的方法来设置请求参数(对我来说更好,因为通过这种方式你可以命名你的参数):

$result->bindParam(':id', $id, PDO::PARAM_INT);

你需要做:

<?php
//...
// counting items in cart and showing on the page - work
if(isset($_POST['total_cart_items']))
{
   echo count($_SESSION['itemid']);
   exit();
}
// post item into cart - work
if(isset($_POST['item_id']))
{    
    $_SESSION['itemid'][]=$_POST['item_id'];
    echo count($_SESSION['itemid']);
    exit();
}
// this part is the problem as doesn't show the cart
if(! isset($_POST['showcart'])) exit; //problem
foreach ($_SESSION['itemid'] as $i):
        $sql = "SELECT * FROM document_upload where upload_id = :id"; 
        $result = $pdo->prepare($sql);
        $result->bindParam(":id", $i, PDO::PARAM_INT);
        $result->execute();                 

        $resArray = $result->fetchAll(); //return the array of results
         foreach ( $resArray as $row ):?>
             <div class="cart_items" style="text-align:center;">
                  <a href=""><p><?=$row["upload_title"]?> - <?=$row["upload_description"]?></p></a>
             </div>
        <?php endforeach;
 endforeach; ?>

更好的是,使用这种结构,您可以直接操作HTML。。。

但是从SQL部分:你想只选择一些项目吗?不要用那种"for"循环!使用以下请求:

SELECT * FROM document_upload where upload_id IN (id1, id2, id3)...

没有更多,更好的表演。。。

试试这个;)

问题在于您将id传递给查询的方式:

$itemsInCart = count($_SESSION['itemid']);
for($i = 0; $i < $itemsInCart; $i++){
    $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
    $result = $pdo->prepare($sql);
    /* you forgot to get current item from array using index $i */
    $result->execute(array($_SESSION['itemid'][$i]));       
    /* fetch all records */
    $records = $result->fetchAll();
    foreach ( $records as $row ){
        /* one more thing you have not selected itemid column in query so you can't get it right now you passed 3 columns;  */
        echo '<div class="cart_items" style="text-align:center;">
                <a href=""><p>' . $row["upload_lesson_plan"] . '</p></a>
              </div>';
    }
}

带有foreach循环:

foreach($_SESSION['itemid'] as $currentItemId){
    $sql = "SELECT upload_lesson_plan, upload_worksheet, upload_materials FROM document_upload where upload_id = ?"; 
    $result = $pdo->prepare($sql);
    /* you forgot to get current item from array using index $i */
    $result->execute(array($currentItemId));       
    /* fetch all records */
    $records = $result->fetchAll();
    foreach ( $records as $row ){
        /* one more thing you have not selected itemid column in query so you can't get it right now you passed 3 columns;  */
        echo '<div class="cart_items" style="text-align:center;">
                <a href=""><p>' . $row["upload_lesson_plan"] . '</p></a>
              </div>';
    }
}