无效的参数错误提供了foreach错误

时间:2017-02-18 10:24:59

标签: php

我的脚本出错了一切都很好,但显示错误。错误在第30行。警告:在第30行的C:\ xampp \ htdocs \ tutorial \ cart.php中为foreach()提供的参数无效。错误显示在最后的查看购物车页面上我还附加了查看购物车代码。

    <?php 
 require_once 'core/init.php';
 include 'includes/head.php';
 include 'includes/headerfull.php';
 include 'includes/navigation.php';
 if($cart_id != ''){
    $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
    $result = mysqli_fetch_assoc($cartQ);
    $items = json_decode($result['items'],true);
    $i= 1;
    $sub_total = 0;
    $item_count = 0;
 }
 ?>
<div class="container" style="margin-bottom: 100px;">
 <div class="col-md-12" style="background-color: white">
   <div class="row">
    <h2 class="text-center">My Shopping Cart</h2><hr>
        <?php if($cart_id == ''): ?>
        <div class="bg-danger">
         <p class="text-center text-danger">Your shopping cart is empty!</p>
         </div>
        <?php else: ?>
          <table class="table table-bordered table-condensed table-striped">
            <thead>
                <th></th><th>Item</th><th>Price</th><th>Quantity</th><th>Sub Total</th>
            </thead>
            <tbody>
                <?php 
                            // error is show in this script
                  foreach($items as $item){
                    $product_id = $item['id'];
                    $productQ = $db->query("SELECT * FROM products WHERE id = '{$product_id}'");
                    $product = mysqli_fetch_assoc($productQ);
                    $sArray = explode(',',$product['sizes']);
                    foreach($sArray as $sizeString){
                        $s = explode(':',$sizeString);
                        if($s[0] == $item['size']){
                            $available = $s[1];
                        }
                    }
                  ?>
                  <tr>
                    <td><?=$i;?></td>
                    <td><?=$product['title']; ?></td>
                    <td><?=money($product['price']);?></td>
                    <td>
                      <button class="btn btn-xs btn-default" onclick="update_cart('removeone','<?=$product['id'];?>','<?=$item['size'];?>');">-</button>
                      <?=$item['quantity'];?>
                      <?php if($item['quantity'] < $available): ?>
                      <button class="btn btn-xs btn-default" onclick="update_cart('addone','<?=$product['id'];?>','<?=$item['size'];?>');">+</button>
                      <?php else: ?>
                       <span class="text-danger">Max</span>
                    <?php endif; ?>

                    </td>
                   <td<?=$item['size'];?></td>
                    <td><?=money($item['quantity'] * $product['price']);?></td>
                  </tr>

             <?php 
               $i++;
               $item_count += $item['quantity'];
               $sub_total += ($product['price'] * $item['quantity']);

             }
             $tax = TEXRATE * $sub_total;
             $tax = number_format($tax,2);
             $grand_total = $tax + $sub_total;

              ?>
            </tbody>
          </table>
          <table class="table table-bordered table-condensed text-right" style="text-align: center">
          <legend>Total</legend>
            <thead class="totals-table-header"><th>Total Item</th><th>Sub Total</th><th>Tax</th><th>Grand Total</th></thead>
            <tbody>
              <tr>
                <td><?=$item_count;?></td>
                <td><?=money($sub_total);?></td>
                <td><?=money($tax);?></td>
                <td class="bg-success"><?=money($grand_total);?></td>
              </tr>
            </tbody>
          </table>
          <!-- checkout button -->
<button style="border: 2px solid grey;" type="button" class="btn btn-default btn-lg pull-right" data-toggle="modal" data-target="#checkoutModal">
 <span class="glyphicon glyphicon-shopping-cart"></span> Check Out >>
</button>

<!-- Modal -->
<div class="modal fade" id="checkoutModal" tabindex="-1" role="dialog" aria-labelledby="checkoutModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="checkoutModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>



        <?php endif; ?>
   </div>
</div>
</div>
      <?php  include 'includes/footer.php'; ?>



 // its view cart page down here
        <?php
    require_once $_SERVER['DOCUMENT_ROOT'].'/tutorial/core/init.php';
    $mode = sanitize($_POST['mode']);
    $edit_size = sanitize($_POST['edit_size']);
    $edit_id = sanitize($_POST['edit_id']);
    $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
    $result = mysqli_fetch_assoc($cartQ);
    $items = json_decode($result['items'],true);
    $ipdated_items = array();
    $domain = (($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false);
    if($mode == 'removeone'){
        foreach($items as $item){
        if($item['id'] == $edit_id && $item['size'] == $edit_size){
            $item['quantity'] = $item['quantity'] - 1;
        }
        if($item['quantity'] > 0){
            $updated_items[] = $item;
        }
       }
     }

     if($mode == 'addone'){
        foreach($items as $item){
        if($item['id'] == $edit_id && $item['size'] == $edit_size){
            $item['quantity'] = $item['quantity'] + 1;
        }

            $updated_items[] = $item;

       }
     }

     if(!empty($updated_items)){
        $json_updated = json_encode($updated_items);
        $db->query("UPDATE cart SET items = '{$json_updated}' WHERE id = '{$cart_id}'");
        $_SESSION['success_flash'] = 'Your shopping cart has been updated!';
     }

     if(empty($updated_items)){
        $db->query("DELETE FROM cart WHERE id = '{$cart_id}'");
        setcookie(CART_COOKIE,'',1,"/",$domain,false);
     }
     ?>

0 个答案:

没有答案