显示每个或jquery的项目数量

时间:2015-03-10 22:18:13

标签: php jquery html multidimensional-array foreach

我的网站上有一个篮子,用户可以将一个项目添加到购物篮中,将其删除并增加数量。该项目通过表单发布到basket.php。表单将项目的ID,数量等发布到basket.php,其中显示项目。使用输入类型='数字'值='数量'正确显示数量。更新basket.php中的数量也可以更新数据库中的数量。 我的问题是:这只适用于一个项目。如果我在购物篮中添加另一个商品,则购物篮中最后一个商品的数量将用于购物篮中的所有其他商品,当我更新数量时,同样的事情会发生。我如何单独处理篮子里的每件物品,以免被视为一件物品。

我的代码:

function.php

   <?php

  /****Function to display list of foods in the order page, 
  info button and the add to basket button********/

  function getFoo(){

   $conn = mysql_connect('localhost','root','') or trigger_error("SQL", 
   E_USER_ERROR);
   $db = mysql_select_db('1000_AD',$conn) or
   trigger_error("SQL", E_USER_ERROR); 

  $sql = "SELECT * FROM food LIMIT $offset, $rowsperpage";
  $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

  // while there are rows to be fetched...
  while ($list = mysql_fetch_assoc($result)) {


   $foo_id = $list['food_id'];
    $foo_cat = $list['food_cat'];
   $foo_type = $list['food_type'];
   $foo_title = $list['food_title'];
   $foo_price = $list['food_price'];
   $foo_image = $list['food_image'];

  // form below
  echo "<div class = 'single_food'>

  <form method='post' action='order.php'>

  <h4>$foo_title</h4>

  <div class = 'pic' ><img src='admin/food_images/$foo_image' width='180'  
  height= '160' /></div>

  <p><b>£$foo_price</b></p>

  <p><b>Qty</b> <input type='number' size='2' name='qty' style='width:30px'  
  min='1'  max = '99' value='1' /></p>

  <div class = 'btn'><a href='info.php?foo_id=$foo_id'  
  style='float:left'>INFO</a></div>

  <div id ='btn2'><input type='submit' name='add_basket'  id='submit'
  value='Add to Basket' /></div> 

         <input type='hidden' name='foo_id' value='$foo_id' />
        <input type='hidden' name='type' value='add' />
  <input type='hidden' name='return_url' value='$current_url' />
      </form>
  </div>";
  ?>

Order.php

 <?php 
  session_start();
  include ("functions/functions.php");
  ?>

 <?php
    //if add basket button is clicked post the food_id and quantity selected 
     to basket in database.

    if (isset($_POST['add_basket'])){

          global $con;

          $ip = getIp();

          $id = $_POST['foo_id'];
          $qty = $_POST['qty'];


             $check_foo = "select * from basket where ip_add= '$ip' AND f_id 

      = '$id'";
      $run_check = mysqli_query($con, $check_foo);

     if(mysqli_num_rows($run_check)>0){

     echo " ";
     }

    else {

     $insert_foo = "insert into basket (f_id,ip_add,qty) values ('$id',  
    '$ip',  '$qty')";
    $run_foo = mysqli_query($con, $insert_foo);

    $_SESSION['qty'] = $qty;

   echo "<script>window.open('order.php','_self')</script>";
     }

        }

      ?>

Basket.php

      <html>
      <head>
      <?php
       session_start();
       include ("functions/functions.php");
       </head>
      <body>
     <?php 
    //displays items in the basket and calculates sub total

      $total = 0;

      global $con;

      $ip = getIp();

      $sel_price = "select * from basket where ip_add='$ip'";

      $run_price = mysqli_query($con, $sel_price);

      while ($p_price = mysqli_fetch_array($run_price)) {

     $foo_id = $p_price['f_id'];

    $foo_price = "select * from food where food_id ='$foo_id'";

    $run_foo_price = mysqli_query($con, $foo_price);

    while ($ff_price = mysqli_fetch_array($run_foo_price)) {

    $food_price = array ($ff_price['food_price']);

    $food_title = $ff_price['food_title'];

    $food_image = $ff_price ['food_image'];

    $single_price = $ff_price['food_price'];

    $values = array_sum($food_price);

    $total += $values;
    ?>


     <?php 

     if (isset($_POST['update_quantity'])){

     $qty = $_POST['qty'];


    $update_qty = "update basket set qty='$qty' where f_id = '$foo_id' 
    and ip_add = ' $ip'";
    $run_qty = mysqli_query($con, $update_qty);

    $_SESSION['qty'] = $qty;

    $total = $values*$qty;

     }
      ?>
      <tr align="center">
     <td><input type="checkbox" name="remove[]" value="<?php echo $foo_id;?>" 
      /></td>
      <td><?php echo $food_title; ?><br>
      <img src="admin/food_images/<?php echo $food_image;?>"width="60" 
     height="60"/>
     </td>
       <td><input type="number" size="2" name="qty" style="width:30px"  
       min="1" max = "99" value ="<?php echo $_SESSION['qty'] ?>" /></td> 
      <td><?php echo "£".$single_price;?></td>

      </tr>

       <?php } } ?>
       <tr align="right">
      <td></td>
       <td></td>
       <td><b>Sub Total:</b></td>

       <td><?php 

       $_SESSION['total'] = $total;



       echo "£". number_format((float)$_SESSION['total'], 2, '.', ''); ?></td>
      </tr>
       <div id="up">
       <td><input type="submit" name="update_basket" value="Remove"></td>
       </div>
       <div id="up">
       <td>

      <input name="adjustBtn' . $foo_id . '" type="submit" 
     value="Update Quantity"/>
    <input name="update_quantity" type="hidden" value="<?php echo 
    $_SESSION['qty']  ?>" />
    </td>
    </div>
     <div id="con">
     <td><input type="submit" name="continue" value="Continue Shopping"></td>
     </div>
    <div id="chck">
     <td><a href="checkout.php"><button  type="hidden"           
       name="check">Checkout</button></a></td>
     <td>
    </div>

    </table>
    </form>
    </body>
    </html>

你能否为每个人展示如何做到这一点?用jquery可以吗?

1 个答案:

答案 0 :(得分:1)

您的项目将真正受益于使用AJAX - 它比听起来更容易。

AJAX允许您将购物篮存储为数据库行,并在不更改页面的情况下动态地将数据发送到购物篮。在AJAX块的success部分,您可以修改您所在的页面 - 从而一举两得:(1)更新表格中的篮子记录,以及(2)更改可见内容在屏幕上。

在设计方面,一种方法可以是使每个篮子成为一个表格行,并且有一个用于以JSON格式存储的篮子项目的列(单元格)(JSON =存储为文本的数组)。为方便起见,您还可以将相同的字符串存储在表单上的隐藏输入字段中 - see this jsFiddle example - 并且每次添加/删除时,只需要AJAX更改的&#34;数组&#34; (json string)到DB。

js代码:

$('input[type="checkbox"]').click(function(){
    qty = prompt('How many?');
    tmp = hidInput.val();
    basket = (tmp=='') ? {} : JSON.parse( tmp );
    item = $(this).closest('tr').attr('id');
    basket[item] = qty;
    hidInput.val( JSON.stringify(basket) );

    $.ajax({
        type: 'post',
        url: 'path_to_your/php_processor_file.php',
        data: myJson=tmp,
        success: function(recd){
            //here, you can update the screen, like this:
            $('#someDiv').html(recd);
        }
    });
});

<强> php_processor_file.php:

<?php

    $json = $_POST['myJson'];
    $arr = json_decode($json, true); //use true to turn into assoc array

    //now, do what you want with that PHP array: $arr

以下是获取AJAX基础知识的一些好帖子:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1