SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot webreadyaudio's Avatar
    Join Date
    Sep 2003
    Location
    Wylie, TX
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help finding value of array

    I've built a simple shopping cart and need to generate the total shipping from the array. Here is code so far.
    PHP Code:
        <?
        
    while (list($key$val) = each($_SESSION['cart'])) {
            
    $this_item $key;
            
    $this_item_qty $val;
            echo (
    '<tr valign="top">');
                echo (
    '<td>' $items[$this_item].'</td>');
                echo (
    '<td align="right">$');
                echo (
    number_format($prices[$this_item],2));
                echo (
    '</td>');
                echo (
    '<td>');
                    if(!
    $this_item_qty) {
                        
    $this_item_qty 1;
                    }
                    echo 
    $this_item_qty;
                echo 
    '</td>';        
                echo (
    '<td>$'$shipping[$this_item] .'</td>');
                
    $this_line_total $this_item_qty * (number_format($prices[$this_item],2) + $shipping[$this_item]);
                echo (
    '<td>$'number_format($this_line_total,2) . '</td>');
            echo (
    '</tr>');
            
    $total $total $this_line_total;
                              
    $shippingtotal // BLAH, BLAH ??
        
    }?>
    So there may be 2 items listed in the cart, and I'm getting their individual totals with shipping, but want to create a seperate line and output the total charge for shipping alone.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didnt look too closely at your code, but i guess you have an individual shipping charge per item.

    here is the logic.
    PHP Code:
    $shipping_total 0;

    while(...) {
        
    $shipping_total =+ $shipping_charge_for_this_item;
    }


    echo 
    $shipping_total

  3. #3
    SitePoint Zealot webreadyaudio's Avatar
    Join Date
    Sep 2003
    Location
    Wylie, TX
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks clamcrusher, however I'm still not getting this.

    I am already inside a while loop. When I try another while loop it times out.

    I tried this
    PHP Code:
    if($items[$this_item] < ) {
                    
    $shippingtotal = + $shipping[$this_item];
                } 
    Basically I'm trying to say if their are more than one items in the cart then add the get the total shipping. Each product has a different shipping charge.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw i made a typo, it should be += not =+

    PHP Code:



     $shipping_total 
    0;

    while (list(
    $key$val) = each($_SESSION['cart'])) { 
        
    // your other code...
        
    $shipping_total += $shipping_charge_for_this_item;
        
    // your other code....
    }


    echo 
    $shipping_total

  5. #5
    SitePoint Zealot webreadyaudio's Avatar
    Join Date
    Sep 2003
    Location
    Wylie, TX
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, I got it to work. I was under the impression I needed to do some sort of count for that array.


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
  •