explode error

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

explode error

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

Post by feyd »

something tells me $res doesn't exist.

var_export() the return from related_products()
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Are you sure $res isn't null?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Damn your fast fingers, Feyd...
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

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

Post by feyd »

that could create a null situation.. please, just humor us and check it.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

yes you were correct, why is this and how can i correct it?

thanks again

alex
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically.. if $relevancy is 0 or null, $res won't exist.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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;
}
Post Reply