php function myBasket global link basket goods array_keys basket array

 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
<?php
function myBasket(){
global $link, $basket;
$goods = array_keys($basket);
array_shift($goods);
$ids = implode(",", $goods);
$sql = "SELECT id, author, title, pubyear, price FROM catalog WHERE id IN ($ids)";
if(!$result = mysqli_query($link, $sql)) return false;
$items = result2Array($result);
mysqli_free_result($result);
return $items;
}
function result2Array($data){
global $basket;
$arr = array();
while($row = mysqli_fetch_assoc($data)){
$row['quantity'] = $basket[$row['id']];
$arr[] = $row;
}
return $arr;
}
if($basket){
echo 'Вернуться в <a href="catalog.php">каталог</a>';
}else{
echo 'Корзина пуста! Вернитесь в <a href="catalog.php">каталог</a>';
}
$i = 1;
$sum = 0;
$items = myBasket();
foreach ($items as $item) {
echo '<tr>
<td>'.$i++.'</td>
<td>'.$item['title'].'</td>
<td>'.$item['author'].'</td>
<td>'.$item['pubyear'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['quantity'].'</td>
<td><a href="delete_from_basket.php?id='.$item['id'].'">Удалить</a></td>
</tr>';
$sum += $item['price'] * $item['quantity'];
}
?>
<p>Всего товаров в корзине на сумму: <?= $sum; ?> руб.</p>