levorion Posted October 24, 2012 Share Posted October 24, 2012 (edited) Hi thanks for reading my first post. I'm having trouble with a checkout form processor that is working but throwing WARNINGS that my sales team (and later direct customers) should not get. I am trying to grab customer cart information (one or more rows), grab it as an array, convert all to string, replace the double shipping (if it exists), and store it in final variable $prodfinal. Here is the code: // BTG Product Inserting Code $result_products = mysql_query("SELECT name, name2 FROM cart WHERE invoice_id=$invoice_id"); while($products[]= mysql_fetch_array($result_products,MYSQL_ASSOC)); $prod3=" "; foreach($products as $prod2) { $prod3.=" ".(implode(" ",$prod2)); } $prod2=(implode(" ",$products)); $prodfinal=str_replace('Shipping Shipping','& Shipping',$prod3); // BTG code end This code executes successfully but throws: Warning: implode() [function.implode]: Invalid arguments passed in /home1/beatth13/public_html/cms/customer_orders_list32.php on line 42 Warning: Cannot modify header information - headers already sent by (output started at /home1/beatth13/public_html/cms/customer_orders_list32.php:42) in /home1/beatth13/public_html/cms/customer_orders_list32.php on line 180 line 42 is the -- foreach($products as $prod2) { $prod3.=" ".(implode(" ",$prod2)); } I tested this code segment on its own php test page and it runs fine, no warnings... when I embed it in the larger form processing page the implode function...well...implodes. ALSO it throws the "output started" error from line 42 except I REALLY do not see where I am sending any output... It does not appear to me that I am sending any HTML output at this point but apparently I am wrong. Thank you in advance for your help! BTG Edited October 24, 2012 by levorion Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/ Share on other sites More sharing options...
requinix Posted October 24, 2012 Share Posted October 24, 2012 The problem is the while loop a few lines above. while($products[]= mysql_fetch_array($result_products,MYSQL_ASSOC)); mysql_fetch_array() will eventually return false. That'll be added into the $products array. When you foreach over it you'll get that false at the very end. So use a temporary variable. $products = array(); while($product = mysql_fetch_array($result_products,MYSQL_ASSOC)) $products[] = $product; Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387516 Share on other sites More sharing options...
kicken Posted October 24, 2012 Share Posted October 24, 2012 ALSO it throws the "output started" error from line 42 except I REALLY do not see where I am sending any output The output is the Warning message you're getting about invalid arguments. When you fix that, the headers already sent error will go away too. Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387518 Share on other sites More sharing options...
levorion Posted October 24, 2012 Author Share Posted October 24, 2012 Thank you both for the insight, the problem is fixed. Still not sure how using the temporary variable eliminates the "false" ending for the array, but it sure does work. Appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387532 Share on other sites More sharing options...
requinix Posted October 24, 2012 Share Posted October 24, 2012 Still not sure how using the temporary variable eliminates the "false" ending for the array, but it sure does work. Appreciate it! Without it the "are there more results" condition and "add this to the array" actions happen at the same time. There's no chance to insert some "only add it if it's a result and not mysql_fetch_array() saying it ran out of results" conditional logic. The actual fix is separating the two so you can add the extra step in between; the temporary variable is just a means to that end. Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387540 Share on other sites More sharing options...
levorion Posted October 24, 2012 Author Share Posted October 24, 2012 So it looks like, then, that storing the result of mysql_fetch_array() as an $array[] will capture everything including the boolean at the end, while storing it as a normal $variable will eliminate that? Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387558 Share on other sites More sharing options...
kicken Posted October 24, 2012 Share Posted October 24, 2012 while($products[]= mysql_fetch_array($result_products,MYSQL_ASSOC)); Runs in a process like this: 1) Get a value from mysql_fetch_array 2) Store that value into $products array 3) Is the value we just got false? - No, go to step 1 - Yes, go to step 4 4) Exit loop Where as this code: while($product = mysql_fetch_array($result_products,MYSQL_ASSOC)) $products[] = $product; Runs in a process like this: 1) Get a value from mysql_fetch_array 2) Store the value into $product 3) Is the value we just got false? - No, go to step 4 - Yes, go to step 5 4) Add $product to $products array 5) Exit loop Quote Link to comment https://forums.phpfreaks.com/topic/269865-implode-error-invalid-argument-please-help/#findComment-1387559 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.