PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > with a foreach statement
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
with a foreach statement

Réponse
 
LinkBack Outils de la discussion
Vieux 11/05/2008, 20h54   #1
Ron Piggott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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";

  Réponse avec citation
Vieux 12/05/2008, 03h04   #2
Craige Leeder
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] 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 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
>
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07889 seconds with 10 queries