我的代码中没有显示表格,知道为什么吗?

时间:2018-11-08 02:05:50

标签: php

<?php
require_once 'core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
include 'includes/headerpartial.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="col-md-12">
      <h2 class="text-center">My Shopping Cart</h2><hr>
  <div class="">
    <?php if($cart_id == ''): ?>
      <div class="" >
        <p class="text-center text-danger">
          Your shoppping 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>Size</th><th>Sub Total</th></thead>
            <tbody>
              <?php
                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><?=$item['quantity'];?></td>
                    <td></td>
                    <td></td>
                  </tr>
                  <?php } ?>
            </tbody>
          </table>
        <?php endif; ?>
  </div>
</div>

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

core / init.php

<?php
$db = mysqli_connect('127.0.0.1','root','','');
if(mysqli_connect_errno()) {
  echo 'Database connection failed with following errors: '. mysqli_connect_error();
  die();
}
session_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/tutorial/config.php';
require_once BASEURL.'helpers/helpers.php';

$cart_id = '';
if(isset($_COOKIE[CART_COOKIE])){
  $cart_id = sanitize($_COOKIE[CART_COOKIE]);
}

if(isset($_SESSION['SBUser'])){
  $user_id = $_SESSION['SBUser'];
  $query = $db->query("SELECT * FROM users WHERE id = '$user_id'");
  $user_data = mysqli_fetch_assoc($query);
  $fn = explode(' ', $user_data['full_name']);
  $user_data['first'] = $fn[0];
  $user_data['last'] = $fn[1];
}

if(isset($_SESSION['success_flash'])){
  echo '<div class="bg-success"><p class="text-success text-center">'.$_SESSION['success_flash'].'</p></div>';
  unset($_SESSION['success_flash']);
}

if(isset($_SESSION['error_flash'])){
  echo '<div class="bg-danger"><p class="text-danger text-center">'.$_SESSION['error_flash'].'</p></div>';
  unset($_SESSION['error_flash']);
}

 ?>

add_cart.php

<?php
 require_once $_SERVER['DOCUMENT_ROOT'].'/tutorial/core/init.php';
 $product_id = sanitize($_POST['product_id']);
 $size = sanitize($_POST['size']);
 $available = sanitize($_POST['available']);
 $quantity = sanitize($_POST['quantity']);
 $item = array();
 $item[] = array(
   'id' => $product_id,
   'size' => $size,
   'quantity' => $quantity,
 );

 $domain = ($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false;
 $query = $db->query("SELECT * FROM products WHERE id = '{$product_id}'");
 $product = mysqli_fetch_assoc($query);
 $_SESSION['success_flash'] = $product['title']. ' was added to your cart.';

 //check to see if the cart cookie exists.
 if($cart_id != ''){
   $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
   $cart = mysqli_fetch_assoc($cartQ);
   $previous_items = json_decode($cart['items'],true);
   $item_match = 0;
   $new_items = array();
   foreach ($previous_items as $pitems) {
     if($item[0]['id'] == $pitem['id'] && $item['size'] == $pitem['size']){
       $pitem['quantity'] = $pitem['quantity'] + $item[0]['quantity'];
       if($pitem['quantity'] > $available){
         $pitem['quantity'] = $available;
       }
       $item_match = 1;
     }
     $new_items[] = $pitem;
   }
   if($item_match != 1){
     $new_items = array_merge($item,$previous_items);
   }
  $items_json = json_encode($new_items);
  $cart_expire = date("Y-m-d H:i:s", strtotime("+30 days"));
  $db->query("UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = '{$cart_id}'");
  setcookie(CART_COOKIE,'',1,'/',$domain,false);
  setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
 }else{
   //add the cart to the database and set cookie
   $items_json = json_encode($item);
   $cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
   $db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}')");
   $cart_id = $db->insert_id;
   setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
 }
 ?>

config.php

<?php
define('BASEURL', $_SERVER['DOCUMENT_ROOT'].'/Tutorial/');
define('CART_COOKIE','SBwi72UckdsknEg2');
define('CART_COOKIE_EXPIRE',time() + (86400 * 30));
 ?>

我应该在页面中看到一个表格,但是我看到了这个https://i.imgur.com/UvAKY3p.png

经过数小时的代码弄乱后,我完全迷路了。

*当我在代码的中间删除“ else”语句时,该表就会显示出来,但是它必须存在。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

我认为您必须将此<?php if($cart_id == ''): ?>替换为<?php if($cart_id != ''): ?>,以使您的表引用的是上面查询的值。 if else条件不正确的逻辑。因为如果执行此if($cart_id != ''),则下面的内容也必须与此<?php if($cart_id != ''): ?>

相同