Passed variable is not an array or objec

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FastLearner
Forum Newbie
Posts: 2
Joined: Thu Aug 25, 2005 3:33 am

Passed variable is not an array or objec

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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

:)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
FastLearner
Forum Newbie
Posts: 2
Joined: Thu Aug 25, 2005 3:33 am

Post by FastLearner »

Unfortunately that does not fix my problem.

The search comes out empty
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I personally prefer

Code: Select all

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