buonasera,
con l'ausilio di uno script esterno sto creando una sorta di carrello e-commerce, ho trovato questo codice alla pagina:
http://jameshamilton.eu/content/simp...-cart-tutorial
io lo sto adattando alle mie esigenze, però riscontro un problema, aggiungo un prodotto in sessione, e me lo risulta con il prezzo esatto, ne aggiungo un altro differente, e mi risulta la tabella con due volte lo stesso prodotto (il primo che ho aggiunto) aggiungo un terzo prodotto e mi risultano 3 prodotti (con il nome del primo aggiunto e il prezzo del primo aggiunto) come mai?
io faccio così per aggiungere il prodotto
Codice PHP:
<a href="agg_carrello.php?nome=$_POST[nome]&prezzo=$_POST[prezzo]
nella pagina agg_carrello.php c'è:
Codice PHP:
<?php
session_start();
$nomeprodotto=$_GET['nome'];
$prezzoprodotto=$_GET['prezzo'];
$_SESSION['carrello'][$nomeprodotto]++;
header("Location:carrello.php");
?>
nella pagina carrello.php c'è
Codice PHP:
<?php
session_start();
$con=mysql_connect(.................);
if(!$con){
die('Impossibile connettersi a: ' . mysql_error());
}
mysql_select_db(.............., $con);
if($_SESSION['carrello']) {
echo "<table border=\"1\" padding=\"3\" width=\"40%\">";
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['carrello'] as $nomeprodotto) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT nome, prezzo, descrizione FROM prodotto WHERE nome = %d;", $nome);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($nome, $prezzo, $descrizione) = mysql_fetch_row($result);
$line_cost = $prezzo; //work out the line cost
$total = $total + $line_cost; //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$nome</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$prezzo <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
echo "<td align=\"center\">$line_cost</td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";
}
?>
ho provato a fare il passaggio di id anzichè del nome del prodotto nel tag <a> iniziale ma non capisco per quale oscuro motivo l'id non me lo passa, me lo registra nel database ma se richiamo l'id con un post non me lo visualizza per questo ho scelto di passare il nome.
comunque il risultato lo potrete verificare qui:
www.laserenissima.eu/prodotti.php
per capire bene cosa mi succede provate ad aprire un prodotto, aggiungerlo al carrello, e poi aggiungerne un altro differente.
spero che qualcuno mi sappia aiutare
grazie e buonanotte!