Professional Web Applications Themes

Updating elements in an array - PHP Development

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 - ...

  1. #1

    Default 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.


    Rob Guest

  2. #2

    Default 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



    Janwillem Borleffs Guest

  3. #3

    Default 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



    Janwillem Borleffs Guest

  4. #4

    Default Re: Updating elements in an array

    in foreach() loop it will check for evry data... if u r writing if else condition inside foreach , then it wil alwayz return a value for every element of array either from if condition or from else condition
    .
    usrname007 Guest

Similar Threads

  1. removing all elements in an array
    By Richard Markham in forum PERL Beginners
    Replies: 3
    Last Post: February 5th, 04:02 AM
  2. array of elements
    By Andrew Gaffney in forum PERL Beginners
    Replies: 3
    Last Post: October 28th, 09:19 PM
  3. Number of elements in an array
    By Graham in forum PERL Miscellaneous
    Replies: 6
    Last Post: September 11th, 09:33 AM
  4. printing elements of an array
    By Dan Muey in forum PERL Beginners
    Replies: 2
    Last Post: August 31st, 11:20 PM
  5. Accessing elements in array ref of array references
    By fatted in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 24th, 08:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139