Page 2 of 2

Posted: Wed Aug 09, 2006 12:12 am
by RobertGonzalez
Don't forget to make the $_SESSION['qty'] var an array before assigning things to it...

Code: Select all

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

for ($i=1; $i<=$total-1; $i++) {       
    $_SESSION['qty']["idqty_{$i}"] =  $_POST["idqty_{$i}"];
}

Posted: Wed Aug 09, 2006 12:25 am
by fitchic77
Weird thing is, if I do, the code does NOT work.

Posted: Wed Aug 09, 2006 12:27 am
by RobertGonzalez
How is it not working?

Posted: Wed Aug 09, 2006 12:35 am
by fitchic77
It never ads to the session...it just replaces it....

If I put:
test1.php

Code: Select all

$session_start();
$_SESSION['products'] = array(); 
$id = $_REQUEST['id']; //id being dynamic in url
$_SESSION['products']["idProduct_{$id}"]  =  $id;
test2.php

Code: Select all

foreach ($_SESSION['products'] as $prod => $val) { 
    echo htmlentities("prod $prod => $val"); 
}
gives me...

Code: Select all

prod idProduct_14 => 14

or next time I add to the cart...

Code: Select all

prod idProduct_15=> 15

if I remove: $_SESSION['products'] = array();
the code lends me this to the same scenario above...adding two different items to the cart
prod idProduct_14 => 14prod idProduct_15 => 15

Posted: Wed Aug 09, 2006 12:44 am
by RobertGonzalez
Ok, so lets say that the querystring var id is a single value...

Code: Select all

<?php
// Start the session, remembering that 
// we do this for every page in the session scope
$session_start();

// Grab the id from the URI if there is one
// If there isn't one, default to 0
$id = ( isset($_GET['id']) ) ? $_GET['id'] : 0;

// Now, since the id is a single value, 
// we do not need to array the product ID session var
// But we can in case later on there is another
// Selected id
$_SESSION['products'] = array();
$_SESSION['products']['product_' . $id]  =  $id;
?>
So, if a user clicked three links with an id querystring var, and on page one clicked on page.php?id=45, then the user clicked on page.php?id=26 then page.php?id=922, your session product array would look like this...

Code: Select all

$_SESSION['products']['product_45'] = 45;
$_SESSION['products']['product_26'] = 26;
$_SESSION['products']['product_922'] = 922;

Posted: Wed Aug 09, 2006 7:08 am
by fitchic77
Yes. It should store all 3, but as soon as I uncomment out : $_SESSION['products'] = array(); it doesn't store more than one "idProduct"... I SWEAR....

Isn't it by default an ARRAY?

Could this be a php.ini setting difference?

Posted: Wed Aug 09, 2006 10:50 am
by RobertGonzalez
$_SESSION is an array, so I suppose technically you don't have to declare the $_SESSION['products'] an array because it will automatically fit into that category .Although good coding practises say you should declare your variables. I cannot see how telling your script that a particular var is an array is going to make it a string. That doesn't make sense to me.

Posted: Wed Aug 09, 2006 3:34 pm
by fitchic77
hey friend....

It isn't a string...it is just storing them all...the display I entered was a little decieving...sorry about that. It just shows all values in the loop idproduct_1 ==>1. I should have put a <br> in the loop.

Sorry about that...thanks again for your help...You saved my project!

Now I can successfully build my cart, remove items and update items... ;)

:wink:

Posted: Thu Aug 10, 2006 12:15 am
by RobertGonzalez
Sidebar: Do you come from an ASP background?