Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Invalid argument supplied for foreach()


magnusj

Recommended Posts

Posted

Hi all!

I had big problems getting my head around this error: Invalid argument supplied for foreach() that was showing up in Admin categories.php when I tried to copy or move products.

 

After searching the osCommerce forums it seems that this error is showing up in different contribs all over the place. It´s a PHP5 problem. I did not find the answer until searching with Google. And here is a solution that worked for me:

 

 

Change

 

"foreach ($products_id as $product){"

 

to

 

"foreach ((array)$products_id as $product){"

 

That´s it. Worked for me :-)

 

Good luck!

Posted

why you say it is a php5 problem? I do not see anything with the php documentation

 

http://www.php.net/manual/en/control-structures.foreach.php

 

Also according to the php docs the first parameter passed should be an array expression. So with the cast you're doing there you try to translate an integer into an array? (assuming some code in a contribution has it wrong you're only masking the problem)

Posted

Hi enigma.

Well, here´s the thing. As you can see I know very little about coding, I just found this solution on the web. And as my error disappeared I just assumed it worked.

 

About being a PHP5 problem, I first put this post in "General Support" by mistake. There someone called "wheeloftime" told me to "add to it that it is a PHP5 problem". And since I pretty much do whatever anyone tells me I did :-"

 

So, enigma... if I´m only masking the problem this way, pleeease - what is the correct way to handle the Invalid argument supplied for foreach() error?

 

Magnus J

Posted

ok you could add some extra checking before using the foreach.

 

For example:

 

//Initialization
$products_array = array();
...

if( is_array($products_array) {
 foreach ($products_array as $product){
 // do something here with each array element from $product
 }
}

 

also check the php link I posted has some examples there.

Posted

Hi enigma.

I can' t get it to work. Can I just remove the foreach thing? Like this:

 

Change:

 

foreach ($products_id as $product){

$duplicate_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . tep_db_input($product) . "' and categories_id = '" . tep_db_input($new_parent_id) . "'");

$duplicate_check = tep_db_fetch_array($duplicate_check_query);

if ($duplicate_check['total'] < 1) tep_db_query("update " . TABLE_PRODUCTS_TO_CATEGORIES . " set categories_id = '" . tep_db_input($new_parent_id) . "' where products_id = '" . tep_db_input($product) . "' and categories_id = '" . $current_category_id . "'");

}

 

to the following:

 

$duplicate_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . tep_db_input($product) . "' and categories_id = '" . tep_db_input($new_parent_id) . "'");

$duplicate_check = tep_db_fetch_array($duplicate_check_query);

if ($duplicate_check['total'] < 1) tep_db_query("update " . TABLE_PRODUCTS_TO_CATEGORIES . " set categories_id = '" . tep_db_input($new_parent_id) . "' where products_id = '" . tep_db_input($product) . "' and categories_id = '" . $current_category_id . "'");

 

 

Seems to work, but...?

Posted

I would have to see the whole file. This looks like the categories.php moded. So either post it the whole file or the link to the contribution and version so I can see it.

Posted

Sry, the changed bit should read:

 

$duplicate_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . tep_db_input($products_id) . "' and categories_id = '" . tep_db_input($new_parent_id) . "'");

$duplicate_check = tep_db_fetch_array($duplicate_check_query);

if ($duplicate_check['total'] < 1) tep_db_query("update " . TABLE_PRODUCTS_TO_CATEGORIES . " set categories_id = '" . tep_db_input($new_parent_id) . "' where products_id = '" . tep_db_input($products_id) . "' and categories_id = '" . $current_category_id . "'");

 

with two instances of tep_db_input($products_id) instead of tep_db_input($product)

 

 

And yes, this is categories.php moded.

Here it is in full, with the above change beginning on line 321 and line 451:

categories.txt

 

Thanks!

Posted

ok, I briefly checked the code. This seems to take place when you copy an entire category at the 'copy_to_confirm' and it reads the posted variable which theoretically should be an array. So the elseif could also check for the array at the same place ie:

 

		  } elseif ($HTTP_POST_VARS['copy_as'] == 'duplicate' && is_array($products_id) ) {

		foreach($products_id as $product_id) {
		   	// BOF Open Featured Sets
........

Posted

When I do that the copy function doesn´t work. No error messages, but nothing happens.

 

When I just remove the foreach like I described above, everything SEEMS to work ok.

Posted

So then it means it is not an array as the code flow skips the if-statement. And the code has another problem because the query in that spot is performed checking for $products_id

 

This statement

$product_query = tep_db_query("select products_quantity, products_model, products_retail, products_image, products_price, products_date_available, products_weight, products_tax_class_id, products_featured, products_featured_until, manufacturers_id from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");

 

Retrieves a row using $products_id which theoretically should been an array which is incorrect. So my question is with this code can you even copy multiple products? Because that was the intention of the code as I see it. If not you should ask the person who did the code to fix it.

Posted

Ok, now I re-checked and made some changes again according to the "mass move" contrib. Now it should work... or so I thought. But I now get the same error that made me start this topic:

 

Warning: Invalid argument supplied for foreach() in /usr/home/path/path/path/public_html/shop/admin/categories.php on line 320

 

Head spinning... don´t know what to do... tired...

 

I have attached the "new" categories.php file: categories.php

 

Thanks for all your help enigma.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...