Jump to content

sessions


Ruth

Recommended Posts

I'm attempting a simple shopping cart to try and learn sessions, so far it's been horrible. I have several links that will display a selection of books. If the user clicks on one book it will show the description of that book. They then can click add to chart at this point I want to store the book information aka order into a session, but have been unsuccessful. I find these simple examples of code online that do not help. Can anyone help me figure this out?

 

Here is what is in my session page

session_start ();

$_SESSION ['cart'] = array();

$_SESSION ['image'] = $image;

$_SESSION ['bktitle'] = $bktitle;

$_SESSION ['price'] = $price;

$_SESSION ['qty'] = $qty;

$_SESSION ['totalqty'] = $totalqty;

$_SESSION ['totalprice'] = $totalprice;

$_SESSION ['total'] = $total;

 

show cart page

session_start ();

 

if (isset ($_SESSION['cart']))

{

$_SESSION['cart'] ++;

}

else

{

$_SESSION['cart'] = 1;

}

 

$isbn = $_GET['isbn'];

 

if (!$_GET['cart'])

{

if (!$isbn)

{

echo 'You have no books in your cart. Please add a book.';

}

}

require_once ('dbconnection.php');

 

if ($isbn or $_GET['cart'])

{

$query = "SELECT image_path, title, book_price

from books

where isbn = '$isbn'";

$result = mysqli_query ($db, $query) or die (mysqli_error ($db));

 

while ($row = mysqli_fetch_array ($result))

{

$image = $row ['image_path'];

$bktitle = $row ['title'];

$price = $row ['book_price'];

$count = count ($row['title']);

}

$qty = 1;

$totalqty = $qty + $_POST ['qty'];

$totalprice = $price * $qty;

$total = $totalprice * $count ;

require_once ('session.php');

 

echo<<<stop

<form method = "post" action = "show_cart.php">

<table>

<tr bgcolor = "red">

<th colspan = "2" align = "center"><font color = "white">Item</font></th>

<th><font color = "white">Price</font></th>

<th><font color = "white">Qty</font></th>

<th><font color = "white">Total</font></th>

</tr>

<tr>

<tfoot bgcolor = "red">

<td></td>

<td></td>

<td></td>

<td><font color = "white">$totalqty </font></td>

<td><font color = "white">$total</font></td>

 

</tfoot>

</tr>

<tr>

<td><img src = "$image"></td>

<td valign = "top">$bktitle</td>

<td valign = "top">$price</td>

<td valign = "top"><input type = "text" value = "$qty" name = "qty" size ="2"></td>

<td valign = "top">$totalprice</td>

</tr>

</table>

</form>

stop;

}

 

 

Link to comment
Share on other sites

I really don't know if I'm doing this right. I turned on error_reporting(E_ALL); and I get Undefined index: cart, Undefined index: qty, and A session had already been started - ignoring session_start() . However I also did print_r($_SESSION); and I get Array ( [cart] => Array ( ) [image] => /images/java_book.gif [bktitle] => Java 2 for professional Developers [price] => 34.99 [qty] => 1 [totalqty] => 1 [totalprice] => 34.99 [total] => 34.99 ). I'm not real sure if the session is being set at all.

Link to comment
Share on other sites

I show on the screen

 

Array ( [cart] => Array ( ) [image] => /images/java_book.gif [bktitle] => Java 2 for professional Developers [price] => 34.99 [qty] => 1 [totalqty] => 1 [totalprice] => 34.99 [total] => 34.99 [test] => test )

Link to comment
Share on other sites

If they order one book and decided to order more I'm trying to store that 2end order along with the first in the session. So I'm trying to get it to increment. If they don't have any order then set their selection as a session and keep adding to it as they order. Does that make sense?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.