Page 1 of 1

explode error

Posted: Mon Aug 08, 2005 8:14 am
by hame22
Hi i am having trouble using the exlode function on a string and am getting the following error

Warning: Variable passed to each() is not an array or object

my code is as follows:

Code: Select all

$q_array=explode(",", $theme);
	
	$q_num=(integer)count($q_array);
	
	
	
	related_products($q_num, $q_array);
	$r = related_products($q_num, $q_array);
	print_related($r);

Code: Select all

function related_products($q_num, $q_array)
{
	db_connect();
	
	$result = mysql_query("Select * from activities");
	
	$num_results = mysql_num_rows($result);
	
	for ($c=0;$c<$num_results;$c++)
	{
		$relevancy = 0;
		$row = mysql_fetch_object($result);
		$mypage=$row->product_id;
		$theme = $row->theme;
		
		for ($d=0; $d<$q_num; $d++)
		{
			$relevancy+=substr_count($theme, strtolower(strip_tags($q_array[$d])));
			
			}
		
			if ($relevancy>0)
		$res["$mypage"] = $relevancy;

	}
	
		
	    //sort results
		if(count($res)>0)
		arsort ($res);
		return $res;
		
	
		
}
	

function print_related($r)
{

	while($element=each($r)  )
		{
			
			if($c<3)
			{
			
			$act_id = $element["key"];
			$row = activity_query($act_id);
			
			$title = $row['title'];
			
			print '<a href="activity.php?act_id='.$act_id.'" class="navlinks">'.$title.'</a><br>';
			$c++;
			}
			
		}
	}
any ideas, it works if i use explode with a space; $q_array=explode(" ", $theme);

just not with the comma

thanlks

alex


feyd | we have

Code: Select all

tags for a reason.. please use them.[/color]

Posted: Mon Aug 08, 2005 8:54 am
by feyd
something tells me $res doesn't exist.

var_export() the return from related_products()

Posted: Mon Aug 08, 2005 8:55 am
by Grim...
Are you sure $res isn't null?

Posted: Mon Aug 08, 2005 8:55 am
by Grim...
Damn your fast fingers, Feyd...

Posted: Mon Aug 08, 2005 8:58 am
by hame22
i dont think $res is null as when i use the explode using a space " " results are returned and displayed, i just have the problem the moment i add a "," into the script

Posted: Mon Aug 08, 2005 9:00 am
by feyd
that could create a null situation.. please, just humor us and check it.

Posted: Mon Aug 08, 2005 9:08 am
by hame22
yes you were correct, why is this and how can i correct it?

thanks again

alex

Posted: Mon Aug 08, 2005 10:03 am
by feyd
basically.. if $relevancy is 0 or null, $res won't exist.

Posted: Tue Aug 09, 2005 3:07 am
by CoderGoblin
As an aside you may also want to check up the following command which is useful to step through an array..
foreach you can also use this with range to perform a cleaner (my opinion) for loop.
Example:

Code: Select all

foreach (range(0, 12) as $number) {
   echo $number;
}