Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
fala galera estou com um script de carrinho de compras, mas é o seguinte qdo tento atualizar a qtd de itens ele zera todo o carrinho podem me dar uma força:
PHP [/tr][tr]<?
session_start();
if($new)
{
if(!session_is_registered("cart"))
{
$cart = array();
session_register("cart");
$items = 0;
session_register("items");
}
if($cart[$new])
$cart[$new]++;
else
$cart[$new] = 1;
$items = calculate_items($cart);
}
if($save)
{
foreach ($cart as $isbn => $qty)
{
if($$isbn=="0")
unset($cart[$isbn]);
else
$cart[$isbn] = $$isbn;
}
$items = calculate_items($cart);
}
function calculate_items($cart)
{
$items = 0;
if(is_array($cart)){
foreach($cart as $isbn => $qty)
{
$items += $qty;
}
}
return $items;
}
function display_cart($cart, $change = true, $images = 1)
{
global $items;
global $total_price;
?>
<table border = 0 width = 100% cellspacing = 0>
<form action ='carrinho.php' method='post'>
<tr>
<th colspan = <?=(1+$images)?> bgcolor='#cccccc'>Item</th>
<th bgcolor='#cccccc'>Preco</th><th bgcolor='#cccccc'>Quant</th>
<th bgcolor='#cccccc'>Total</th></tr>
<?
foreach ($cart as $isbn => $qty)
{
?>
<tr>
<?
if($images ==true)
{
?>
<td align = left>
<?
if (file_exists("images/$isbn.jpg"))
{
$size = GetImageSize("images/".$isbn.".jpg");
if($size[0]>0 && $size[1]>0)
{
?>
<img src='images/<?=$isbn?>.jpg' border=0 width= "<?=$size[0]/3?>" height="<?=$size[1]/3?>" >
<?
}
}
else
?>
</td>
<?
}
?>
<td align = left>
<a href = 'show_book.php?isbn=<?=$isbn?>'><?=$book["title"]?></a> de <?=$book["author"]?>
</td><td align = center>$ <?=number_format($book["price"], 2)?>
</td>
<td align = center>
<?
if ($change == true)
{
?>
<input type = text name='<?=$isbn?>' value ='<?=$qty?>' size=3>
<?
}
else
{
print $qty;
}
?>
</td>
<td align = center>
R$ <?=number_format($book["price"]*$qty,2)?>
</td></tr>
<?
}
?>
<tr>
<th colspan = <?=(2+$images)?> bgcolor='#cccccc'> </td>
<th align = center bgcolor='#cccccc'>
<?=$items?>
</th>
<th align = center bgcolor='#cccccc'>
$ <?=number_format($total_price, 2)?>
</th>
</tr>
<?
if($change == true)
{
?>
<tr>
<td colspan = <?=(2+$images)?> </td>
<td align = center>
<input type = hidden name = save value = true> <input type ='submit' alt = 'Save Changes'>
</td>
<td> </td>
</tr>
<?
}
?>
</form>
</table>
<?
}
if($cart && @array_count_values($cart)):
display_cart($cart);
else:
{
?>
Seu carrinho esta vazio!!!
<hr size="1">
<? }
endif
?>
[/tr]
para testar o script execute-o :
http://localhost/carrinho.php?new=1
http://localhost/carrinho.php?new=2
http://localhost/carrinho.php?new=3
valew
Carregando comentários...