Jump to content

Recommended Posts

Can I use a While Loop on a session array?

 

What I mean is, can I do this, but for an array result instead of a mysql query result?

 

while ($row = mysql_fetch_array ($result)) {
echo 'output goes here';
}

 

How would I do this for a session array that looks like this? I need to get the $sessID for each instance of the shopping cart session.

$_SESSION['cart'][$sessID] = array ('quantity' => 1, 'price' => $prodPrice, 'prodName' => $prodName, 'handle' => $handle);

 

There is a good chance that I'm actually making this very difficult for myself. If any one has a better idea of how to do the following, I would love to hear it! Thanks...

 

I am building a php/mysql shopping cart for a cricket website. When the website user adds a cricket bat to the shopping cart they must select some options:

 

Bat size, Handle size, Bat Edges, Bat Weight, Etc...

 

There are between 6 and 10 options depending on the chosen cricket bat.

 

I add the details to the shopping cart session like this:

 

 

$sessID = uniqid();

$prodID = (int) $_POST['prodID'];
$prodName = $_POST['prodName'];
$prodPrice = $_POST['prodPrice'];
$size = $_POST['size'];
$handle = $_POST['handle'];
$weight = $_POST['weight'];
$edges = $_POST['edges'];

$_SESSION['cart'][$sessID] = array ('quantity' => 1, 'price' => $prodPrice, 'prodName' => $prodName, 'handle' => $handle);

 

On the viewcart.php page I intend on outputting the price and product name from the shopping cart arrays.

 

The problem is, I don;t know how to set a while loop for all the instances of $sessID?

 

Any thoughts on this would be greatly appreciated.

 

Thanks

do you mean something like this

$_SESSION['cart'][$sessID] = array(); // set as an array

//add to the array
$_SESSION['cart'][$sessID][] = array ('quantity' => 1, 'price' => $prodPrice, 'prodName' => $prodName, 'handle' => $handle);

echo "<pre>";
//loop thought the array
foreach($_SESSION['cart'][$sessID] as $Item)
{
    print_r($Item);
}
echo "</pre>";

MadTechie,

 

Thanks for your super quick reply - I appreciate your help.

 

I tried your code, but got the following error:

Warning:  Invalid argument supplied for foreach() in /home/timcarl/public_html/viewcartoption.php on line 9

 

I suspect that it's because $sessID no longer exists. After adding the items to the shopping cart, I go to a different page (viewcart.php) to view the contents of the cart and checkout.

 

I have the following code at the top of my viewcart page:

foreach ($_SESSION['cart'] as $sessID => $value) {
	$allsessids .= $sessID;
}

 

The problem is, I now have one massive number in the variable $allsessids, instead of a list of a list of the shopping cart session ID's.

 

When I come to output the shopping cart sessions, how would you do it?

 

I thought a While Loop was the best way, but I'm not sure how to code it. I want something like this:

 

echo '<td>'.$_SESSION['cart'][$sessID]['quantity'].'</td><td>'.$_SESSION['cart'][$sessID]['price'].'</td>';

 

Which would give me a table of results of everything in my shopping cart:

 

Item 1 - £100 - quantity:1

Item 2 - £120 - quantity: 1

 

I hope this makes sense. Thanks again for your quick response.

 

 

 

 

I don't believe your even need $sessID in the array, unless theirs a purpose for it ?

 

personally i would do this

 

$_SESSION['cart'] = array(); // set as an array

//add to the array
$_SESSION['cart'][] = array ('quantity' => 1, 'price' => $prodPrice, 'prodName' => $prodName, 'handle' => $handle);

 

So in my example (below) $item is the array

 

foreach ($_SESSION['cart'] => $item) {
      echo '<td>'. $item['quantity'].'</td><td>'. $item['price'].'</td>';
   }

 

personally I use foreach on arrays and while with SQL queries, I find it easier that way.. as I don't know when the query will end but with an array i don't want to check if I'm on the last item!

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.