Olá, estou criando um mini projeto que consiste em um sistema de pedido para auto-atendimento para lanchonetes e restaurante, mas estou com dificuldades em inserir a quantidade para fazer o calculo e gerar o total da compra, alguém poderia me ajudar uma forma de gerar isto, não consegui e estou sem ideia, até em sites gringos já fui, mas não encontrei nada alguém me dá uma luz por favor.
Banco:
CREATE TABLE IF NOT EXISTS `produto` (
`id` int(11) NOT NULL,
`descricao` varchar(50) NOT NULL,
`preco` double(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `itensvenda` (
`id` int(11) NOT NULL,
`idVend` int(11) NOT NULL,
`idProd` int(11) NOT NULL,
`qtd` varchar(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `venda` (
`id` int(11) NOT NULL,
`valor` double(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
conexão:
<?php
$conect = mysqli_connect("localhost","root","root","teste");
?>
index:
<?php
session_start()?>
<?php
if (isset($_SESSION['venda'])){
}else{
$_SESSION['venda'] = array();
}
if(isset($_GET['del'])){
$del = $_GET['del'];
unset($_SESSION['venda'][$del]);
}
?>
<html>
<title> Produto</title>
<body>
<?php
include_once("config.php");
?>
<h1>produtos</h1>
<ul>
<?php
$sql= mysqli_query($conect,"select * from produto");
while($res = mysqli_fetch_array($sql)){
?>
<li>
<span><?php echo $res['descricao']?></span>
<strong><a href="index.php?par=<?php echo $res['id']?>"><?php echo number_format ($res['preco'],2,".",".");?></a></strong>
</li>
<?php
}
?>
</ul>
<table width="700" border="1">
<tr>
<td>produto:</td>
<td>valor:</td>
<td>quantidade:</td>
<td>açoes:</td>
</tr>
<form action="" enctype="multipart/form-data" method="post">
<?php
foreach($_SESSION['venda'] as $id => $quantidade):
$sqlCarrinho = mysqli_query($conect,"select * from produto where id = '$id'");
$resAssoc = mysqli_fetch_assoc($sqlCarrinho);
echo '<tr>
<td>'.$resAssoc['descricao'].'</td>
<td>' .$resAssoc['preco'].'</td>
<td>'<input type="text" name="qtd" value="1"/>'</td>
<td><a href="index.php?del='.$resAssoc['id'].'">x</a></td>
echo <tr>';
$totalProduto += $resAssoc['preco'] * $quantidade;
echo '<tr>';
endforeach;
echo '<tr>';
echo '<td colspan="4" align="right">R$ '.number_format($totalProduto,2,".",".").'</td>';
echo '</tr>';
?>
<?php
if(isset($_POST['enviar'])){
$sqlInseriVenda= mysqli_query($conect,"insert into venda(valor)values('$totalProduto')");
$idVenda = mysqli_insert_id($conect);
foreach ($_SESSION['venda'] as $proInsert => $quantidade) :
$sqlInserItens = mysqli_query($conect,"insert into itensvenda(idVend,idProd,qtd) values('$idVenda','$proInsert','$quantidade')");
endforeach;
echo "<script> alert('venda concluida com sucesso')</script>";
# code...
}
?>
</table>
<input align="right" type="submit" name="enviar" value="finalizar pedido">
</form>
</body>
</html>