Jump to content

Recommended Posts

I was wondering if someone could help me with my loop logic...

 

php 5.2.5

 

What I'm trying to do is figure shipping costs for a shopping cart... Here is my code so far...

 

$query = mysql_query("SELECT * FROM $local_database.shopping_cart_contents WHERE shopping_cart_id = '$_SESSION[shopping_cart_id]'", $b_link);                               
    while ($row = mysql_fetch_assoc($query)) {    
        foreach ($row['product_vendor'] as $vendor) {  
        #    Determine if it has preset shipping or not                                                                        
        if ($row['preset_shipping'] == "yes") {
        #   If it does have preset shipping, determine if it is set or free                                                    
            if ($row['preset_shipping_set_or_free'] == "set") {
        #    If the preset shipping is set, then use the preset shipping fee for the shipping rate * the quantity of that item 
                $shipping_rate = $row['preset_shipping_fee'] * $row['shopping_cart_contents_qty'];
            }
            if ($row['preset_shipping_set_or_free'] == "free") { 
        #    If the preset shipping is free, then the shipping rate is free
                $shipping_rate = 0;
            }    
        }                                                                   
        if ($row['preset_shipping'] == "no") {
        #   If it does not have preset shipping, calculate the shipping based on the to and from zipcodes and the total                    combined weight of how many of that item there are 
            foreach ($row['product_id'] as $product) { 
            $sub_total_weight = $row['product_weight'] * $row['shopping_cart_contents_qty'];
            }
            $total_weight += $sub_total_weight;
            if (isset($_POST['service'])) {$service = $_POST['service'];}else{$service = '03';} 
            $length = '5';
            $width = '5';
            $height = '5';
            $shipping_rate = ups($row['vendor_zipcode'],$shipping_zipcode,$service,$total_weight,$length,$width,$height);    
        }    
        }
        $sub_total_shipping_price += $shipping_rate;            
    }

 

The problem lies in the foreach statements, the first foreach statement is meant to separate each set of products that have the same vendor in the while loop... the second foreach statement, inside the first, is meant to separate out each product's combined weight.

 

I'm getting this error however, "Warning: Invalid argument supplied for foreach()" Which I don't understand, because the first argument is an array, which seems to be the only real stipulation to the foreach loop.

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/
Share on other sites

foreach ($row['product_vendor'] as $vendor)

 

is NOT an array it is a single entity, $row is an associative array, but in a while loop it is only acting on one row at a time and the $row[product_vendor] is one specific key=>value not an array, I hope that makes sense

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603707
Share on other sites

you could try and remove the while loop altogether and see what that does for you

 

just use

 

$row = mysql_fetch_assoc($query); // making $row the associative array it is and use the two foreach loops on it 

 

remeberbering to also remove the while closing brace

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603722
Share on other sites

you could try and remove the while loop altogether and see what that does for you

 

just use

 

$row = mysql_fetch_assoc($query); // making $row the associative array it is and use the two foreach loops on it 

 

remeberbering to also remove the while closing brace

 

That'll give $row just one row of data.  He needs the while loop.

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603729
Share on other sites

i thought the while loop was to allow you to iterate over the associative array one row at a time, if you make $row = to the full associative array using fetch_assoc then the rest would work.

 

Read up on mysql_fetch_* functions then.  Honestly, that's not how it works.  The array keys would overwrite each other anyway if that was the case.

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603735
Share on other sites

Oh.  Move the foreach outside of the while loop and rewrite it as:

 

foreach ($vendors as $vendor) {

 

Then make the while loop something like:

 

while ($row = mysql_fetch_assoc($query)) {

            $vendors[$row['product_vendor'][] = $row;

}

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603747
Share on other sites

sorry for trying to mislead people there

 

mysql_fetch_assoc

 

(PHP 4 >= 4.0.3, PHP 5, PECL mysql:1.0)

 

mysql_fetch_assoc — Fetch a result row as an associative array

 

 

will only fetch one row of the array, thanks for pointing it out to me DarkWater I now stand corrected

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603750
Share on other sites

try

<?php
$query = mysql_query("SELECT * FROM $local_database.shopping_cart_contents WHERE shopping_cart_id = '$_SESSION[shopping_cart_id]'", $b_link);                               
    while ($row = mysql_fetch_assoc($query)) {    
        // foreach ($row['product_vendor'] as $vendor) {
        $vendor = $row['product_vendor'];  
        #    Determine if it has preset shipping or not                                                                        
        if ($row['preset_shipping'] == "yes") {
        #   If it does have preset shipping, determine if it is set or free                                                    
            if ($row['preset_shipping_set_or_free'] == "set") {
        #    If the preset shipping is set, then use the preset shipping fee for the shipping rate * the quantity of that item 
                $shipping_rate = $row['preset_shipping_fee'] * $row['shopping_cart_contents_qty'];
            }
            if ($row['preset_shipping_set_or_free'] == "free") { 
        #    If the preset shipping is free, then the shipping rate is free
                $shipping_rate = 0;
            }    
        }                                                                   
        if ($row['preset_shipping'] == "no") {
        #   If it does not have preset shipping, calculate the shipping based on the to and from zipcodes and the total                    combined weight of how many of that item there are 
         //   foreach ($row['product_id'] as $product) { 
            $sub_total_weight = $row['product_weight'] * $row['shopping_cart_contents_qty'];
         //   }
            $total_weight = $sub_total_weight;
            if (isset($_POST['service'])) {$service = $_POST['service'];}else{$service = '03';} 
            $length = '5';
            $width = '5';
            $height = '5';
            $shipping_rate = ups($row['vendor_zipcode'],$shipping_zipcode,$service,$total_weight,$length,$width,$height);    
        }    
        //}
        if (isset($sub_total_shipping_price[$vendor]))$sub_total_shipping_price[$vendor] += $shipping_rate; 
        else $sub_total_shipping_price[$vendor] = $shipping_rate;           
    }
print_r($sub_total_shipping_price);
echo "\n<br />", array_sum($sub_total_shipping_price);
?>

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603782
Share on other sites

That seems to work to separate out by vendors nicely DarkWater... thank you, but I can't figure out how to get my other variables...

 

Array
(
    [TestingVendor] => Array
        (
            [0] => Array
                (
                    [shopping_cart_contents_id] => 3
                    [shopping_cart_id] => 3
                    [product_id] => 31175
                    [product_vendor] => TestingVendor
                    [vendor_zipcode] => 90210
                    [shopping_cart_contents_qty] => 1
                    [product_name] => GPS, NUVI 350 ASIAN AMERICAS
                    [product_price] => 311.02
                    [product_weight] => 1.9
                    [preset_shipping] => no
                    [preset_shipping_set_or_free] => 
                    [preset_shipping_fee] => 0.00
                )

        )
)

 

I tried, $vendors['preset_shipping'], for example and that didn't do it, so I seen that its actually a layer deeper, so I tried, $vendors[]['preset_shipping'], and that errors out...

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603830
Share on other sites

It seems that its working alright, it'll take me some calculations on my own to make sure its coming up with the right amounts, here's the code, can you take a look at my next set of embedded foreach loops, the ones that are separating out the product_ids and tell me if I did that correctly?

 

$query = mysql_query("SELECT * FROM $local_database.shopping_cart_contents WHERE shopping_cart_id = '$_SESSION[shopping_cart_id]'", $b_link);
    while ($row = mysql_fetch_assoc($query)) {          
             $vendors[$row['product_vendor']][] = $row;   
    }                                    
        foreach ($vendors as $vendor) {
            foreach ($vendor as $row) {    
        #    Determine if it has preset shipping or not                                                                        
        if ($row['preset_shipping'] == "yes") {
        #   If it does have preset shipping, determine if it is set or free                                                    
            if ($row['preset_shipping_set_or_free'] == "set") {
        #    If the preset shipping is set, then use the preset shipping fee for the shipping rate * the quantity of that item 
                $shipping_rate = $row['preset_shipping_fee'] * $row['shopping_cart_contents_qty'];
            }
            if ($row['preset_shipping_set_or_free'] == "free") { 
        #    If the preset shipping is free, then the shipping rate is free
                $shipping_rate = 0;
            }    
        }                                                                   
        if ($row['preset_shipping'] == "no") {
        #   If it does not have preset shipping, calculate the shipping based on the to and from zipcodes and the total                    combined weight of how many of that item there are
            $products[$row['product_id']][] = $row; 
            foreach ($products as $product) {
                foreach ($product as $row) { 
                $sub_total_weight = $row['product_weight'] * $row['shopping_cart_contents_qty'];
                }
            }
            $total_weight += $sub_total_weight;
            if (isset($_POST['service'])) {$service = $_POST['service'];}else{$service = '03';} 
            $length = '5';
            $width = '5';
            $height = '5';
            $shipping_rate = ups($row['vendor_zipcode'],$shipping_zipcode,$service,$total_weight,$length,$width,$height);    
        }
            } 
        }

Link to comment
https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603848
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.