webent Posted July 30, 2008 Share Posted July 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/ Share on other sites More sharing options...
paul2463 Posted July 30, 2008 Share Posted July 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603707 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 Yeah, I didn't even think of it that way, its not really an array until its outside the loop that's creating it... hmmm, that really poses a problem with my logic then. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603714 Share on other sites More sharing options...
paul2463 Posted July 30, 2008 Share Posted July 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603722 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603729 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 Exactly, somehow I have to keep the while loop and use some type of foreach logic but without using the foreach loop..., which is really not leaving me with alot of options... Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603731 Share on other sites More sharing options...
paul2463 Posted July 30, 2008 Share Posted July 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603733 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Did you try removing the foreach and just doing: $vendor = $row['product_vendor']; Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603734 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603735 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 Did you try removing the foreach and just doing: $vendor = $row['product_vendor']; But that wouldn't group all products for each instance of every unique product_vendor, you see what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603742 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603747 Share on other sites More sharing options...
paul2463 Posted July 30, 2008 Share Posted July 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603750 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Any time. =P Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603752 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 Sorry if I'm misunderstanding you, but if I move the foreach loop outside the while loop, won't I only get one instance of the variable? Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603756 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 No, because of the way I populated the $vendors variable. =P Make sure to change the foreach. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603763 Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603782 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 I get this error "Fatal error: Cannot use [] for reading" for this... $vendors[$row['product_vendor'][]] = $row; I've never seen code like that, so if its a simple mistake on my part, I apologize. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603786 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 You moved one of the ]. Try: $vendors[$row['product_vendor']][] = $row; You may need to rework your foreach loop though. Like, the inside of it. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603793 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603830 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 foreach ($vendors as $vendor) { foreach ($vendor as $row) { echo $row['shopping_cart_contents_id']; //echo's 3 first based on the array you just gave } } Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603832 Share on other sites More sharing options...
webent Posted July 30, 2008 Author Share Posted July 30, 2008 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); } } } Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603848 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 I think you have it, but you just should check the calculations and array structure to make sure it's what you need. Quote Link to comment https://forums.phpfreaks.com/topic/117378-foreach-loop-help/#findComment-603851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.