Page 1 of 1

Passed variable is not an array or objec

Posted: Thu Aug 25, 2005 3:41 am
by FastLearner
Hi There, I have been fighting with this for a while now and have had no luck. Could some one help me.

I am getting the following errors when I search on "All Catergories" in a shopping cart application. Searching on specific categories is fine although.

Warning: reset(): Passed variable is not an array or object in /home/gty/public_html/search.php on line 22

Warning: Variable passed to each() is not an array or object in /home/gty/public_html/search.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/gty/public_html/search.php:22) in /home/gty/public_html/includes/functions.php o

n line 51

Here is my code:

Code: Select all

function search_by_cat ($cat_id, $query)
{

	global $txt, $tpl_block, $sub_cat;
	global $subcatid;
	$sql_where_cat = ''; $sql_where_title = ''; $i = 0; $old_c = '';

	// search subcat_id
	reset  ($sub_cat[$cat_id]); //THIS IS LINE 22
	while (list ($key, $val) = each  ($sub_cat[$cat_id])) //AND LINE 23
		$sql_where_cat .= "cat_id = '$key' OR ";

	$sql_where_cat = substr ($sql_where_cat, 0, -3);
Can anyone offer some suggertions?

Regards
Marcus

Posted: Thu Aug 25, 2005 6:22 am
by raghavan20
As it says line 22 and 23 are using an array value they shou ld be using the array itself instead.
replace those values with just $sub_cat

Posted: Thu Aug 25, 2005 6:47 am
by s.dot
reset() and each() will only accept arrays, and will return false and perhaps provide an error

$array['value'] <- not an array
$array <- array

:)

Posted: Sun Aug 28, 2005 4:16 am
by FastLearner
Unfortunately that does not fix my problem.

The search comes out empty

Posted: Mon Aug 29, 2005 12:57 am
by n00b Saibot
I think this is so because the global array $sub_cat is not set when the code reaches this stage and hence the error...

Posted: Mon Aug 29, 2005 2:02 am
by s.dot
FastLearner wrote:Unfortunately that does not fix my problem.

The search comes out empty
How are you setting up your SQL for the "search all categories"
If the search comes up empty, then something in your query is wrong.

Posted: Mon Aug 29, 2005 8:13 am
by raghavan20
a tip:

Code: Select all

select * from table_name where search_field = '' //does not display any record
select * from table_name where search_field  =  '' //does not display any record
select * from table_name where search_field like '%' //does display all records
select * from table_name where search_field like '%%' //does display all records

Posted: Mon Aug 29, 2005 8:17 am
by feyd
I personally prefer

Code: Select all

$match = implode("','",array_keys($sub_cat));
$sql = "SELECT * FROM table WHERE someField IN('{$match}')";
;)