Eu já tinha imaginado isso... porem tenta acompanhar meu raciocínio... Um pedido tem mais de um item certo? como vou fazer para inserir todos os itens no pedido(isso estou dizendo no banco de dados) E você pediu os codigos: Data base CREATE TABLE IF NOT EXISTS `produtos` (
`id` int(10) NOT NULL DEFAULT '0',
`nome` varchar(50) DEFAULT NULL,
`preço` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
conexão.php:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("alunos_carrinho");
?>
index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Video Aula sobre Carrinho de Compras</title>
</head>
<body>
<?php
require("conexao.php");
$sql = "SELECT * FROM produtos";
$qr = mysql_query($sql) or die(mysql_error());
while($ln = mysql_fetch_assoc($qr)){
echo '<h2>'.$ln['nome'].'</h2> <br />';
echo 'Preço : R$ '.number_format($ln['preço'], 2, ',', '.').'<br />';
echo '<a href="carrinho.php?acao=add&id='.$ln['id'].'">Comprar</a>';
echo '<br /><hr />';
}
?>
</body>
</html>
carrinho.php:
<?php
session_start();
if(!isset($_SESSION['carrinho'])){
$_SESSION['carrinho'] = array();
}
//adiciona produto
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($_GET['id']);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
}else{
$_SESSION['carrinho'][$id] += 1;
}
}
//REMOVER CARRINHO
if($_GET['acao'] == 'del'){
$id = intval($_GET['id']);
if(isset($_SESSION['carrinho'][$id])){
unset($_SESSION['carrinho'][$id]);
}
}
//ALTERAR QUANTIDADE
if($_GET['acao'] == 'up'){
if(is_array($_POST['prod'])){
foreach($_POST['prod'] as $id => $qtd){
$id = intval($id);
$qtd = intval($qtd);
if(!empty($qtd) || $qtd <> 0){
$_SESSION['carrinho'][$id] = $qtd;
}else{
unset($_SESSION['carrinho'][$id]);
}
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Video Aula sobre Carrinho de Compras</title>
</head>
<body>
<table>
<caption>Carrinho de Compras</caption>
<thead>
<tr>
<th width="244">Produto</th>
<th width="79">Quantidade</th>
<th width="89">Preço</th>
<th width="100">SubTotal</th>
<th width="64">Remover</th>
</tr>
</thead>
<form action="carrinho.php?acao=up" method="post">
<tfoot>
<tr>
<td colspan="5"><input type="submit" value="Atualizar Carrinho" /></td>
<tr>
<td colspan="5"><a href="index.php">Continuar Comprando</a></td>
</tfoot>
<tbody>
<?php
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
}else{
require("conexao.php");
$total = 0;
foreach($_SESSION['carrinho'] as $id => $qtd){
$sql = "SELECT * FROM produtos WHERE id= '$id'";
$qr = mysql_query($sql) or die(mysql_error());
$ln = mysql_fetch_assoc($qr);
$nome = $ln['nome'];
$preço = number_format($ln['preço'], 2, ',', '.');
$sub = number_format($ln['preço'] * $qtd, 2, ',', '.');
$total += $ln['preço'] * $qtd;
echo '<tr>
<td>'.$nome.'</td>
<td><input type="text" size="3" name="prod['.$id.']" value="'.$qtd.'" /></td>
<td>R$ '.$preço.'</td>
<td>R$ '.$sub.'</td>
<td><a href="?acao=del&id='.$id.'">Remove</a></td>
</tr>';
}
$total = number_format($total, 2, ',', '.');
echo '<tr>
<td colspan="4">Total</td>
<td>R$ '.$total.'</td>
</tr>';
}
$_SESSION['total'] = $total;
?>
</tbody>
</form>
</table>
<form name="finalisa" action="finalisa.php?acao=''" method="post">
<input type="submit" value="Finalizar compra" />
</form>
</body>
</html>
finalisa.php:
<?php
session_start();
//REMOVER CARRINHO
if($_GET['acao'] == 'del'){
$id = intval($_GET['id']);
if(isset($_SESSION['carrinho'][$id])){
unset($_SESSION['carrinho'][$id]);
}
}
?>
<h2>Finalizar pedido</h2>
<?php
echo"Itens comprados";
?>
<table>
<tr>
<th width="244">Produto</th>
<th width="79">Quantidade</th>
<th width="89">Preço</th>
<th width="100">SubTotal</th>
<th width="64">Remover</th>
</tr>
<?php
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
}else{
require("conexao.php");
$total = 0;
foreach($_SESSION['carrinho'] as $id => $qtd){
$sql = "SELECT * FROM produtos WHERE id= '$id'";
$qr = mysql_query($sql) or die(mysql_error());
$ln = mysql_fetch_assoc($qr);
$nome = $ln['nome'];
$preço = number_format($ln['preço'], 2, ',', '.');
$sub = number_format($ln['preço'] * $qtd, 2, ',', '.');
$total += $ln['preço'] * $qtd;
echo '<tr>
<td>'.$nome.'</td>
<td>'.$qtd.'</td>
<td>R$ '.$preço.'</td>
<td>R$ '.$sub.'</td>
<td><a href="?acao=del&id='.$id.'">Remove</a></td>
</tr>';
}
$total = number_format($total, 2, ',', '.');
echo '<tr>
<td colspan="4">Total</td>
<td>R$ '.$total.'</td>
</tr>';
}
?></table>
<form action="">
Comentarios sobre a compra:<br />
<textarea name="Comentario" rows="10" cols="40" size="60" />
</textarea><br>
<input type="submit" value="Prossegir" />
</form>