SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Explain this PHP code please

    Hi, I don't fully understand this PHP code... I'm getting this from the oscommerce script, so hopefully someone can explain it to me.

    PHP Code:
        $sql_data_array = array('orders_id' => $insert_id,
                                
    'products_id' => tep_get_prid($order->products[$i]['id']),
                                
    'products_model' => $order->products[$i]['model'],
                                
    'products_name' => $order->products[$i]['name'],
                                
    'products_price' => $order->products[$i]['price'],
                                
    'final_price' => $order->products[$i]['final_price'],
                                
    'products_tax' => $order->products[$i]['tax'],
                                
    'products_quantity' => $order->products[$i]['qty']); 
    What I'm trying to do is get the total order of the cart (without any shipping/handling/taxes added to it). I'm guessing it has something to do with the products_price, but I'm not too sure.

    Thanks in advance for your help

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    It's simply creating an array and assigning it to the variable $sql_data_array. Have a look at the output from this PHP code
    PHP Code:
    $sql_data_array = array('orders_id' => $insert_id
                                
    'products_id' => tep_get_prid($order->products[$i]['id']), 
                                
    'products_model' => $order->products[$i]['model'], 
                                
    'products_name' => $order->products[$i]['name'], 
                                
    'products_price' => $order->products[$i]['price'], 
                                
    'final_price' => $order->products[$i]['final_price'], 
                                
    'products_tax' => $order->products[$i]['tax'], 
                                
    'products_quantity' => $order->products[$i]['qty']); 

    print_r($sql_data_array); 
    It just means that now $sql_data_array['orders_id'] will equal the value of $insert_id.

    Hope that helped [img]images/smilies/smile.gif[/img]

  3. #3
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oscommerce has a custom function that makes the creation of UPDATE sql code somewhat simpler, you just pass an array such as the one above and it builds the query from that.

  4. #4
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how is it possible to work out the total value of the shopping cart from that code (assuming all the prices are in the products_price)?

  5. #5
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It isn't possible, that code is just an array.

  6. #6
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like a 3d array to me and I'm not too great at getting around 3d arrays. From my understanding, it is storing values into $sql_data_array['products_price']. I think that that the products_price could be an array of all the items that have been purchased.

    So basically what I need is to somehow extract all the prices from that array and sum it all up. Hope that makes sense?

  7. #7
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its not a 3d array. Its just a standard associative array containing a bunch of values destined for insertion into the database.

  8. #8
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Phil, do you have experience with this specific code? If not, how do you know whether or not it is a multi-dimensional array or not?

    In any case, if it is, I'll assume the array contained within $sql_data_array['product_price'] is just a 2-dimensional array containing prices, in which case this would do what you want:

    PHP Code:
    $total_price 0;
    foreach (
    $sql_data_array['product_price'] as $price)
    {
        
    $total_price += $price;

    Hope that helps
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  9. #9
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks cyborg, I'll try that out

  10. #10
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh well, that didn't work... I figured out a work around though and that's to retrieve the values from the database and storing them in my variables pretty much straight after it's been inserted in. I guess that's easier...

  11. #11
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    A bit more server usage than necessary though.


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
  •