-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart_fetch.php
73 lines (66 loc) · 2.23 KB
/
cart_fetch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
include 'includes/session.php';
$conn = $pdo->open();
$output = array('list'=>'','count'=>0);
if(isset($_SESSION['user'])){
try{
$stmt = $conn->prepare("SELECT *, products.name AS prodname, category.name AS catname FROM cart LEFT JOIN products ON products.id=cart.product_id LEFT JOIN category ON category.id=products.category_id WHERE user_id=:user_id");
$stmt->execute(['user_id'=>$user['id']]);
foreach($stmt as $row){
$output['count']++;
$image = (!empty($row['photo'])) ? 'images/'.$row['photo'] : 'images/noimage.jpg';
$productname = (strlen($row['prodname']) > 30) ? substr_replace($row['prodname'], '...', 27) : $row['prodname'];
$output['list'] .= "
<li>
<a href='product.php?product=".$row['slug']."'>
<div class='pull-left'>
<img src='".$image."' class='thumbnail' alt='User Image'>
</div>
<h4>
<b>".$row['catname']."</b>
<small>× ".$row['quantity']."</small>
</h4>
<p>".$productname."</p>
</a>
</li>
";
}
}
catch(PDOException $e){
$output['message'] = $e->getMessage();
}
}
else{
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
if(empty($_SESSION['cart'])){
$output['count'] = 0;
}
else{
foreach($_SESSION['cart'] as $row){
$output['count']++;
$stmt = $conn->prepare("SELECT *, products.name AS prodname, category.name AS catname FROM products LEFT JOIN category ON category.id=products.category_id WHERE products.id=:id");
$stmt->execute(['id'=>$row['productid']]);
$product = $stmt->fetch();
$image = (!empty($product['photo'])) ? 'images/'.$product['photo'] : 'images/noimage.jpg';
$output['list'] .= "
<li>
<a href='product.php?product=".$product['slug']."'>
<div class='pull-left'>
<img src='".$image."' class='img-circle' alt='User Image'>
</div>
<h4>
<b>".$product['catname']."</b>
<small>× ".$row['quantity']."</small>
</h4>
<p>".$product['prodname']."</p>
</a>
</li>
";
}
}
}
$pdo->close();
echo json_encode($output);
?>