Jump to content

Implode Error (Invalid Argument) Please Help


levorion

Recommended Posts

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 by levorion
Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
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.