dynamic cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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}"];
}
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post by fitchic77 »

Weird thing is, if I do, the code does NOT work.
Last edited by fitchic77 on Sat Oct 16, 2010 11:28 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How is it not working?
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post 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
Last edited by fitchic77 on Sat Oct 16, 2010 11:29 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post 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?
Last edited by fitchic77 on Sat Oct 16, 2010 11:28 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
fitchic77
Forum Commoner
Posts: 51
Joined: Thu Jul 20, 2006 11:57 pm

Post 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:
Last edited by fitchic77 on Sat Oct 16, 2010 11:28 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sidebar: Do you come from an ASP background?
Post Reply