Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-31-2004, 01:14 AM simple array display
Experienced Talker

Posts: 34
Trades: 0
I created this array


$products = array(
"ibm" => array (
"id" => "ibm",
"qty" => 0,
"name" => "IBM Laptop",
"price" => "200.00"
),
"dell" => array (
"id" => "dell",
"qty" => 0,
"name" => "Dell Laptop",
"price" => "300.00"
),
"gateway" => array (
"id" => "gateway",
"qty" => 0,
"name" => "Gateway Laptop",
"price" => "400.00"




I want to use the foreach funtion to display name price and qty

How can I do this??
cedtech23 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-31-2004, 01:51 AM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
PHP Code:
foreach($products AS $name => $info)
{
    echo 
"<strong>$name</strong>: \${$info['price']} (Qty: {$info['qty']}<br />";

Should do the trick.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 09:10 AM missing first second part of name
Experienced Talker

Posts: 34
Trades: 0
Thanks for the input the relsults only show the first part of the name:


ibm: $200.00 Qty: 0
dell: $300.00 Qty: 0
gateway: $400.00 Qty: 0

it shoul say ibm laptop, dell laptop, why is it not showing the second part does it have to do with the white spaces??
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 09:27 AM Got it
Experienced Talker

Posts: 34
Trades: 0
Thanks Chroder,

I was able to look as the code you gave me any figure out why
but I still have some questions to I can better understand arrays.

$products is the name of the array and with that array their is arrays call ibm, dell, gateway. on the second part of the foreach statement $name =>$info means that $name stands for ibm,gateway, dell.

does $info stand for an array that contains id,qty,name,price??

why did I have to enclosed some items in {} ??

trying to fully understand array and output



foreach($products AS $name => $info)
{
echo "<strong>{$info['name']}</strong>: \${$info['price']} Qty: {$info['qty']}<br />";
}
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 01:53 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
A foreach array has two different syntax's:
foreach($array AS $value)
foreach($array AS $key => $value
(You name the variables whatever you want) In our case, we used the second one. Since you have a multidimensional array (meaning an array of an array) then the $value itself is an array, and that explains why $info was holding id,qty,name,price -- because that is the array that $products[anykey] is holding.

As to the {}'s, that's a simpler explanation Curly brackets help PHP distinguish between a variable in double quotes, and normal text in double qoutes. For example, if you wanted to say:
<animal>s are great
With 'animal' as a variable, you would need curly brackets to tell PHP that the 's' is not part of the variable name:
{$animal}s are great
If you try to put quotes around the keyname like $info['price'] then PHP get's confused. You can solve this by using simply $info[price] (no quotes) or by using the curly brackets. Habit just makes me choose the latter. You can read more in the manual, the part's on 'Simple' and 'Complex' syntax.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 03:24 PM passing array to sessions
Experienced Talker

Posts: 34
Trades: 0
thanks for the explanation

Okay, I created the foreach listed below

foreach($products AS $name => $info)
{

echo "<tr>";
echo "<td>{$info['name']}</td>";
echo "<td>\${$info['price']}</td>";


Then I added this line to pass the about of items that have been added to $_SESSION['cart']

echo "<td><a
href=$_SERVER[PHP_SELF]?buy=$products>BUY</a></td>";

am I passing the right array to the $_SESSSION['cart']

I need the display the items(dell laptop, etc) and the quantity (1,2, etc)of them in the cart
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 03:29 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
$products is the array itself, you cannot pass an entire array as a query string (other then to serialize it, which is not efficient in this situation).

You'd want to pass the 'id' in the query string

Code:
echo "<td><a href=$_SERVER[PHP_SELF]?buy=$info[id]">BUY</a></td>";
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 03:37 PM
Experienced Talker

Posts: 34
Trades: 0
how is passing buy=$info['id'] to the $_SESSION array going to help be create

name price qty


and have to qty change with each time I select it example

Dell Laptop $200.00 5

I am confused because I have never worked with session and arrays
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 07-31-2004, 05:31 PM
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
There is a cart script that was written by Ying Zhang which I first saw in a tutorial (I think on PHP Builders, not positive) quite some months ago.

It just recently resurfaced on OpenSourceCMS as a demo and is more polished. It was written for educational and demonstration purposes only, but has a very good foundation laid into it for session tracking and the cart.

You might be interested in this and you can demo it at OpenSource, it is called MyMarket. The script is very small, well as scripts of this type are considered.

I am actually using this script to build a cart for an existing site that I am working with. The site currently is passing the purchasing off to a Yahoo Store, but we are losing sales with this method. I have adapted it to feed off of the inventory at the Ystore, now all I need to do is the complicated part -- the Payment Gateway with Authorize.NET.
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]

Last edited by RonnieTheDodger; 07-31-2004 at 05:34 PM..
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 07-31-2004, 05:42 PM
RonnieTheDodger's Avatar
Extreme Talker

Posts: 232
Location: Central USA
Trades: 0
The tutorial was at DevShed (not PHP Builders). I did a lot of searching for cart tutorials, and this one was about the best I found out there. There is another good one at Zend also which was written some time ago, but the principals still apply. That is in a series entitled A PHP Based Shopping Cart

Quote:
E-Commerce Tutorial [ Part 1 | Part 2 | Part 3 ]
A comprehensive three part tutorial covering each step of how MyMarket was built. It provides background information about user security, shopping carts, and more. These documents reside at DevShed.
__________________
Ronnie T. Dodger

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Reply     « Reply to simple array display
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.74992 seconds with 12 queries