Help with a foreach statement

This is a discussion on Help with a foreach statement within the PHP General forums, part of the PHP Programming Forums category; I am writing a shopping cart. Products assigned in the following fashion: $_SESSION['selection'][$product]=$quantity; I am wanting to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2008
Ron Piggott
 
Posts: n/a
Default Help with a foreach statement

I am writing a shopping cart.

Products assigned in the following fashion:
$_SESSION['selection'][$product]=$quantity;

I am wanting to display the list of products in the shopping cart in a
list for the user to see as they continue shopping.

I put the SESSION variable into $cart

$cart = $_SESSION['selection'];

then I start the foreach and this is where I get messed up. I am trying
to ECHO the following syntax to the screen with each selected product:

The part I need help with is to write the foreach loop to give me
$product (so I may query the database to find out the product name) and
the $quantity (so I may show the user what they are purchasing). The
part that is messing me up is that this is an array.

My ECHO statement should look like this:

echo "<li><a href=\"" . $path_to_shopping_cart . "product/" .
$cart[$product_id] . "/\">" . $product_name . "</a> - " . $quantity .
"</li>\r\n";

Reply With Quote
  #2 (permalink)  
Old 05-12-2008
Craige Leeder
 
Posts: n/a
Default Re: [PHP] Help with a foreach statement

Hi Ron,

This code should work:

<?php

$path_to_shopping_cart = './';
$iLength = count($cart);

foreach ($cart as $product_name => $quantity)
{
echo "<li><a href=\"" . $path_to_shopping_cart . "product/" .
$cart[$product_id] . "/\">" . $product_name . "</a> - " . $quantity .
"</li>\r\n";
}

?>

Though I'm not sure about $cart[$product_id]. Where is that coming
from?. It makes no sense here. Perhaps you want to do a
tri-demensional array in $_session, so you will have a
dual-demesnional array in $cart.

Let me know,
- Craige

On Sun, May 11, 2008 at 2:54 PM, Ron Piggott <ron.php@actsministries.org> wrote:
> I am writing a shopping cart.
>
> Products assigned in the following fashion:
> $_SESSION['selection'][$product]=$quantity;
>
> I am wanting to display the list of products in the shopping cart in a
> list for the user to see as they continue shopping.
>
> I put the SESSION variable into $cart
>
> $cart = $_SESSION['selection'];
>
> then I start the foreach and this is where I get messed up. I am trying
> to ECHO the following syntax to the screen with each selected product:
>
> The part I need help with is to write the foreach loop to give me
> $product (so I may query the database to find out the product name) and
> the $quantity (so I may show the user what they are purchasing). The
> part that is messing me up is that this is an array.
>
> My ECHO statement should look like this:
>
> echo "<li><a href=\"" . $path_to_shopping_cart . "product/" .
> $cart[$product_id] . "/\">" . $product_name . "</a> - " . $quantity .
> "</li>\r\n";
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:40 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0