php-如何从一个页面获取所选项目并在另一个页面上显示所有这些特定项目?

时间:2016-11-09 05:48:18

标签: php jquery

我有两页。 menu.php是显示数据库中所有项目的地方,并且有一个" ADD"按钮添加这些项目。这些项应该被添加到另一个页面中的表中(addtocart.php)。问题是它只添加了我点击"添加"的第一个项目。按钮。当我为其他项目做同样的事情时,它什么都不做。

Here is a picture of menu.php

这里只添加了一个项目,当我返回并再次点击另一个项目的添加按钮时,表格中没有任何内容。它只保留一个项目,没有更新。 Addtocart.php

以下是menu.php

的代码
       <div id = "breakfast2">
        <h2>Breakfast</h2>
        <?php
        $conn1 = mysqli_connect("localhost", "root", "", "cart_mexican") or    

      die();
        $dbsql1 = "SELECT * FROM breakfast ORDER BY id ";

        $query1 = mysqli_query($conn1, $dbsql1);
        if(mysqli_num_rows($query1) > 0)  
            {  
        while($row1= mysqli_fetch_assoc($query1) or die)
            { 
            ?>
            <div class = "col-md-4">
    <form method = "POST" action ="addtocart.php?action = add &id=<?phpecho   
    $row1 ['id']; ?>">

    <img src = "<?php echo $row1["image_path"]; ?>" class = "img-responsive"   
  /><br>
    <h4 class ="text-primary"><?php echo $row1["name"]; ?></h4>
    <center><h4 class = "text-primary">Rs <?php echo $row1["price"]; ?></h4>  
   </center>
    <input type = "text" name ="quantity" class = "form-control" 
   style="width: 260px" />
    <input type = "hidden" name ="hidden_name" value = "<?php echo 
   $row1["name"]; ?>" />
    <input type = "hidden" name ="hidden_price" value = "<?php echo 
   $row1["price"]; ?>" />
    <center><input type = "submit" name ="add_to_cart" style = "margin-
 left:0px; margin-top: 5px;" class ="btn btn-small btn-success" value=   
 "Add"/>
        </div>
        </form>
        </div>

        <?php
    }
            }
        ?>

以下是addtocart.php

的代码
    <?php   
        session_start();  
        $connect = mysqli_connect("localhost", "root", "", "cart_mexican");  
        if(isset($_POST["add_to_cart"]))  
        {  
        if(isset($_SESSION["shopping_cart"]))  
            {  
       $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");  
       if(!in_array($_GET["id"], $item_array_id))  
       {  
            $count = count($_SESSION["shopping_cart"]);  
            $item_array = array(  
                 'item_id'               =>     $_GET["id"],  
                 'item_name'               =>     $_POST["hidden_name"],  
                 'item_price'          =>     $_POST["hidden_price"],  
                 'item_quantity'          =>     $_POST["quantity"]  
            );  
            $_SESSION["shopping_cart"][$count] = $item_array;  
       }  
       else  
       {  
            echo '<script>alert("Item Already Added")</script>';  
            echo '<script>window.location="addtocart.php"</script>';  
       }  
        }  
        else  
            {  
       $item_array = array(  
            'item_id'               =>     $_GET["id"],  
            'item_name'               =>     $_POST["hidden_name"],  
            'item_price'          =>     $_POST["hidden_price"],  
            'item_quantity'          =>     $_POST["quantity"]  
       );  
       $_SESSION["shopping_cart"][0] = $item_array;  
        }  
        }  
        if(isset($_GET["action"]))  
        {  
        if($_GET["action"] == "delete")  
        {  
            foreach($_SESSION["shopping_cart"] as $keys => $values)  
        {  
            if($values["item_id"] == $_GET["id"])  
            {  
                 unset($_SESSION["shopping_cart"][$keys]);  
                 echo '<script>alert("Item Removed")</script>';  
                 echo '<script>window.location="addtocart.php"</script>';  
            }  
        }  
        }  
        }  
        ?>

表格的HTML代码

           <div class="container" style="width:700px;">  

            <div style="clear:both"></div>  
            <br />  
            <h3>Order Details</h3>  
            <div class="table-responsive">  
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="40%">Item Name</th>  
                           <th width="10%">Quantity</th>  
                           <th width="20%">Price</th>  
                           <th width="15%">Total</th>  
                           <th width="5%">Action</th>  
                      </tr>  
                      <?php   
                      if(!empty($_SESSION["shopping_cart"]))  
                      {  
                           $total = 0;  
                 foreach($_SESSION["shopping_cart"] as $keys => $values)  
                           {  
                      ?>  
                      <tr>  
                           <td><?php echo $values["item_name"]; ?></td>  
                           <td><?php echo $values["item_quantity"]; ?></td>  
                           <td>Rs <?php echo $values["item_price"]; ?></td>  
                           <td>Rs <?php echo 
   number_format($values["item_quantity"] * $values["item_price"], 2); ?>   
  </td>  
                           <td><a href="addtocart.php?action=delete&id=<?php 
    echo $values["item_id"]; ?>"><span class="text-danger">Remove</span></a>   
   </td>  
                      </tr>  
                      <?php  
     $total =  $total +  ($values["item_quantity"]*$values["item_price"]);  
                           }  
                      ?>  
                      <tr>  
                           <td colspan="3" align="right">Total</td>  
                           <td align="right">Rs <?php echo   

     number_format($total, 2); ?></td>  
                           <td></td>  
                      </tr>  
                      <?php  
                      }  
                      ?>  
                 </table>  
            </div>  
       </div>  

0 个答案:

没有答案