Answer the question
In order to leave comments, you need to log in
How to generate a cart from MySql via PHP (Telegram shop-bot)?
I am writing a shop bot in a cart, trying to form a shopping cart.
How
to make this out of this (in simple words)
Table from the database.
The code itself:
case 'Корзина': {
action($chat_id,'typing');
$cart_info=getCart($user_id);
$cart_products=json_decode($cart_info['product_id'],true);
$num=count($cart_products);
if($num>0 and $cart_products!=null){
$product_array=array();
$all_price=0;
foreach ($cart_products as $productID){
$info=getProduct($productID);
$product_array[]=$info;
$all_price+=$info['price'];
}
$result=" В корзине: ".PHP_EOL.PHP_EOL;
$cnt=($num>=$limit_musics)?$limit_musics:$num;
for ($i=1;$i<=$cnt;$i++){
$product_id=$product_array[$i-1]['id'];
$product_name=$product_array[$i-1]['name'];
$product_price=$product_array[$i-1]['price'];
$result.=$i.". ".$product_name." - ".$product_price." тг.".PHP_EOL."------------------------".PHP_EOL;
}
if($num>$limit_musics){
$result.=PHP_EOL."Итого: ".$all_price." тг.";
message($chat_id, urlencode($result).inline_btn(array('Следующая страница','/nextCart_'.$limit_musics,' Завершить покупку','Оформить заказ','❌ Очистить корзину','/delCart')));
}else{
$result.=PHP_EOL."Итого: ".$all_price." тг.";
message($chat_id, urlencode($result).inline_btn(array('Оформить заказ ','/nextReq_','Очистить корзину ❌','/delCart')));
}
}else{
$msg="♻️Ваша корзина пуста".PHP_EOL.PHP_EOL."Вы можете использовать кнопку продукта или кнопку поиска, чтобы найти нужные продукты и добавить их в корзину!";
message($chat_id, urlencode($msg));
}
}break;
function getCart($user_id){
global $db;
$query="select * from cart WHERE user_id=".$user_id." or id=".$user_id;
$res=mysqli_query($db, $query);
$res=mysqli_fetch_assoc($res);
return $res;
}
function getProduct($product_id){
global $db;
//SELECT MAX(num) AS Name, COUNT(num) AS Count FROM dbo.TestTable GROUP BY (num)
$query="select count * from product WHERE id=".$product_id;
$res=mysqli_query($db, $query);
$res=mysqli_fetch_assoc($res);
return $res;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question