|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
PHP: shopping cart vuota cliccando aggiungi al carrello
Come da titolo, ho il problema che quando cerco di aggiungere un prodotto al carrello, mi appare una finestra con messaggio che e' stato aggiunto, ma in realta' nella tabella del cart non c'e'.
Mi da anche il seguente errore: Fatal error: Cannot use object of type stdClass as array . La linea di codice e': <td><?php echo $value['item_name']; ?></td> Vi posto tutto il codice che si trova nel file reserve.php : Codice:
<?php session_start(); ini_set('display_errors', 1); $connect = mysqli_connect('127.0.0.1', 'root', '***********', 'Community Garden List'); if (isset($_POST['add'])) { if (isset($_SESSION['cart'])) { $item_array_id = array_column($_SESSION['cart'], 'product_id'); if (!in_array($_GET['id'], $item_array_id)) { $count = count($_SESSION['cart']); $item_array = array( 'product_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'product_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['cart'][$count] = $item_array; echo '<script>window.location="reserve.php"</script>'; } else { echo '<script>alert("Product is already Added to Cart")</script>'; echo '<script>window.location="reserve.php"</script>'; } } else { $item_array = array( 'product_id' => $_GET['id'], 'item_name' => $_POST['hidden_name'], 'product_price' => $_POST['hidden_price'], 'item_quantity' => $_POST['quantity'], ); $_SESSION['cart'][0] = $item_array; } } if (isset($_GET['action'])) { if ($_GET['action'] == 'delete') { foreach ($_SESSION['cart'] as $keys => $value) { if ($value['product_id'] == $_GET['id']) { unset($_SESSION['cart'][$keys]); echo '<script>alert("Product has been Removed...!")</script>'; echo '<script>window.location="reserve.php"</script>'; } } } } ?> ?> Codice:
<?php $query = 'SELECT * FROM product ORDER BY serial ASC'; $result = mysqli_query($connect, $query); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result)) { ?> <div class="col-md-4"> <form method="post" action="reserve.php?action=add&id='.$row['id']."> <div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center"> <img src="<?php echo $row['image']; ?>" class="img-responsive" style="width:100%;> <h5 class="text-info"><?php echo $row['pname']; ?></h5> <h5 class="text-danger">€ <?php echo $row['price']; ?></h5> <h5 class="text-info"><?php echo $row['pdescription']; ?></h5> <input type="text" name="quantity" class="form-control" value="1"> <input type="hidden" name="hidden_name" value="<?php echo $row['pname']; ?>"> <input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"> <input type="hidden" name="hidden_pdescription" value="<?php echo $row['pdescription']; ?>"> <input type="submit" name="add" style="margin-top:5px;" class="btn btn-success" value="Add to Bag"> </div> </form> </div> <?php } } ?> <div style="clear: both"></div> <h3 class="title2">Shopping Cart Details</h3> <div class="table-responsive"> <table class="table table-bordered"> <tr> <th width="30%">Product Name</th> <th width="10%">Quantity</th> <th width="13%">Price Details</th> <th width="10%">Total Price</th> <th width="17%">Remove Item</th> </tr> <?php if (!empty($_SESSION['cart'])) { $total = 0; foreach ($_SESSION['cart'] as $key => $value) { ?> <tr> <td><?php echo $value['item_name']; ?></td> <td><?php echo $value['item_quantity']; ?></td> <td>€ <?php echo $value['product_price']; ?></td> <td> € <?php echo number_format($value['item_quantity'] * $value['product_price'], 2); ?></td> <td><a href="reserve.php?action=delete&id=<?php echo $value['product_id']; ?>"><span class="text-danger">Remove Item</span></a></td> </tr> <?php $total = $total + ($value['item_quantity'] * $value['product_price']); } ?> <tr> <td colspan="3" align="right">Total</td> <th align="right">€ <?php echo number_format($total, 2); ?></th> <td></td> </tr> <?php } ?> </table> </div> </div>
__________________
Ti Amo amore mio...per sempre! ![]() ![]() |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: May 2003
Messaggi: 57
|
Il carrello non lo stai salvando nel database ma nella sessione, questo spiega perchè la tabella del carrello rimane vuota.
Per l'errore che ti da, il codice sembra corretto. A questo punto proverei a stampare la variabile $_SESSION['cart'] con var_dump o print_r in modo da capire come sono organizzati gli oggetti. Nel dettaglio php ti sta dicendo che non puoi usare la notazione $value['item_name'] perchè $value è un oggetto e non un array. Se così fosse allora ti basterebbe fare $value->item_name (e così con tutti gli altri valori) per risolvere. |
![]() |
![]() |
![]() |
#3 | ||
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
Quote:
Notice: Undefined property: stdClass::$item_name in
__________________
Ti Amo amore mio...per sempre! ![]() ![]() Ultima modifica di mariade : 06-01-2018 alle 22:25. |
||
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: Jul 2009
Messaggi: 275
|
Ti stai referenziando ad un object trattandolo come fosse un array. Al posto di
Codice PHP:
Codice PHP:
|
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
Quote:
Notice: Undefined property: stdClass::$item_name Dopo che aggiungo il prodotto al carrello, devo far inserire nome e email al cliente e far storing dell'ordine nel database, che poi rilascera' i dati dell'ordine al cliente da salvare. come risolvo? Ne sto uscendo pazza.
__________________
Ti Amo amore mio...per sempre! ![]() ![]() Ultima modifica di mariade : 07-01-2018 alle 16:46. |
|
![]() |
![]() |
![]() |
#6 |
Member
Iscritto dal: Jul 2009
Messaggi: 275
|
Non avevo visto la risposta. Dunque ora il problema è che ti stai referenziando a qualcosa che non esiste e da qui warning. Printa $value ed accertati che al suo interno ci sia realmente quello che stai cercando.
|
![]() |
![]() |
![]() |
#7 |
Member
Iscritto dal: May 2003
Messaggi: 57
|
Fai un var_dump($_SESSION['cart']) così capisci cosa c'è dentro...
EDIT: non conosco l'architettura dell'intero progetto però il problema potrebbe risiedere nel fatto che nella pagina in cui mostri il carrello, non chiami la funzione session_start(). Ultima modifica di Pbdz : 08-01-2018 alle 19:36. |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
grazie degli aiuti,
son riuscita a correggere gli errori. adesso ho il problema che aggiunge solo un prodotto al cart e non di piu' e non mi cancella il prodotto dal carrello. Qual e' il problema secondo voi?
__________________
Ti Amo amore mio...per sempre! ![]() ![]() Ultima modifica di mariade : 09-01-2018 alle 09:41. |
![]() |
![]() |
![]() |
#9 |
Member
Iscritto dal: May 2003
Messaggi: 57
|
Posta il codice aggiornato così vediamo se ci sono degli errori...ho già qualche sospetto...
![]() |
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Jun 2005
Città: Swords, Dublino
Messaggi: 642
|
grazie, ho risolto anche questo problema. grazie tante.
__________________
Ti Amo amore mio...per sempre! ![]() ![]() |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Aug 2017
Messaggi: 469
|
Capisco la situazione ma devo dire che seguire il problema su più forum rende alla fine inutile e confusa la discussione ad altri.
Un rimedio sarebbe mettere (in entrambi i forum) la soluzione funzionante... Ultima modifica di Mursey : 10-01-2018 alle 12:49. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 20:46.