Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Updating elements in an array
Hi,

I am quite new to PHP, so appologies is this sounds too simple.

I am developing a shopping cart to be included in my website.  When an item
is added to the basket, I use the following code to store the product id
number and quantity in an associated array:

$product_details = array("qty" => $quantity, "productID" => $product_id);
 if(isset($_SESSION['cart'][$product_id])
)
{
//Increase quantity
 $_SESSION['cart'][$product_id]['qty']+=$
product_details['qty'];
}
else
{
//Add a new element to the array
 $_SESSION['cart'][$product_id]=$product_
details;
}


When the user views the basket, the following code displays the contents:

foreach($_SESSION['cart'] as $value)
{
// Lookup product information from the database - i.e. name, price etc,
//Ouptut HTML source to display this intormation
}

When I output the product information, there is a text box corrasponding to
each item that is initially populated with the quantity value which is
recalled from the associated array.

The user is able to adjust the figures in each of these text boxes to change
the quantity required of each item.

My question is, when the user clicks the 'Update Basket' command button, how
do I update the Qty field for each element in the array to reflect the new
quantity values from the text boxes on the form?

Many Thanks.



Report this thread to moderator Post Follow-up to this message
Old Post
Rob
10-13-04 08:56 PM


Re: Updating elements in an array
Rob wrote:
> I am quite new to PHP, so appologies is this sounds too simple.
>
> I am developing a shopping cart to be included in my website.

It's funny how many new users are building shopping carts...

> My question is, when the user clicks the 'Update Basket' command
> button, how do I update the Qty field for each element in the array
> to reflect the new quantity values from the text boxes on the form?
>

Assuming that the form method you are using is POST and that the input field
names equal the stored session keys and that only positive integers are
allowed, you could do something like the following:

foreach($_SESSION['cart'] as $k => $v) {
if (isset($_POST[$k])) {
if (preg_match("/^\d+$/", $qty = $_POST[$k])) {
$_SESSION['cart'][$k] = $v;
} else {
// Quantity is not a positive integer, handle the error
}
}
}


HTH;
JW




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
10-14-04 01:55 AM


Re: Updating elements in an array
Janwillem Borleffs wrote:
> foreach($_SESSION['cart'] as $k => $v) {
>    if (isset($_POST[$k])) {
>        if (preg_match("/^\d+$/", $qty = $_POST[$k])) {
>            $_SESSION['cart'][$k] = $v;
>        } else {
>            // Quantity is not a positive integer, handle the error
>        }
>    }
> }
>
>

Sorry, this should read:

foreach($_SESSION['cart'] as $k => $v) {
if (isset($_POST[$k])) {
if (preg_match("/^\d+$/", $qty = $_POST[$k])) {
$_SESSION['cart'][$k] = $qty;
} else {
// Quantity is not a positive integer, handle the error
}
}
}


JW




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
10-14-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:54 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.