paging of an array

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

paging of an array

Post by hame22 »

Hi all

I have an array $r generated from the following code:

Code: Select all

function return_relevancy($result,$q_num, $q_array, $keyword_set)
{
	$num_results = mysql_num_rows($result);
	
	//for each result get variables
	for ($c=0;$c<$num_results;$c++)
	{
		$relevancy = 0;
		$row = mysql_fetch_object($result);
		$mypage=$row->product_id;
		$mycontent=strtolower(strip_tags($row->long_desc));
		$mytitle = strtolower(strip_tags($row->title));
		$strap_line = strtolower(strip_tags($row->strap_line));
		$intro = strtolower(strip_tags($row->intro));
		$keywords = strtolower(strip_tags($row->keywords));
		$ed_desc = strtolower(strip_tags($row->ed_desc));
		$theme = strtolower(strip_tags($row->theme));
		$who = strtolower(strip_tags($row->who));
		
	//if there were no keywords searched for to not perform rankings	
	if ($keyword_set == "no")
	{
		$res["$mypage"] = $relevancy;
	}
	else {
		
		//perform relevancy test
		for ($d=0; $d<$q_num; $d++)
		{
			$relevancy+=substr_count($mycontent, strtolower(strip_tags($q_array[$d])));
			
			//if keyword is in title
			if(substr_count($mytitle, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 10;
			}
			//if keyword is in strap line
			if(substr_count($strap_line, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 10;	
			}
			
			//if keyword is in intro
			if(substr_count($intro, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 1;
			}
			
			//if keyword is in keywords
			if(substr_count($keywords, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 5;
			}
			
			//if keyword is in ed_desc
			if(substr_count($keywords, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 5;
			}
			
			//if keyword is in theme
			if(substr_count($theme, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 3;
			}
			
			//if keyword is in who
			if(substr_count($who, strtolower(strip_tags($q_array[$d]))))
			{
				$relevancy=$relevancy + 2;
			}
			
		} //end of for
		
		//if an activity has a relevancy of at least 1 display it in search results
		if ($relevancy>0)
		$res["$mypage"] = $relevancy;
	}
	}
		
	    //sort results
		if(count($res)>0)
		arsort ($res);
		return $res;
}

And I display it throught the following function:

Code: Select all

function print_answers($r)
{
	$no_results = (count($r));
		

		//display results header
		 print '<table width="100%" cellspacing="0" cellpadding="0"  ><tr><td class="resultsline">'.$no_results.' training activities returned</td></tr>
		 <tr>';	 pagernumber($page, $i, $pager); print'</tr></table><br>';
	
	//if there were no results display error	 
	if (count ($r) <1)
	{
	print 'nothing found';
	}
	else
	{
		while($element=each($r))
		{
			$act_id = $element["key"];
			$row = activity_query($act_id);
			
			$title = $row['title'];
			$short_desc = $row['short_desc'];
			$man_id = $row['relation'];
			
			$row = manual_query($man_id);
	        $man_title = $row['man_title'];
	        
	        //display
			
			Print '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><p><strong><a href="activity.php?act_id='.$act_id.'" class="resulttitle">'.$title.'</a></strong></td><td align="right">  <span align ="right" class="italicmanual">featured in: '.$man_title.'</span><td><tr><table>
 		<div class="shortdesc">'.$short_desc.'</div></p>';
			
			print ' activity '.$element["key"].'&'.$element[1].'</br>';
		}
	}
}

what I want to do is split the results into pages of 10 results per page, i.e. i want to use paging.

However the method I have used in the past has involved limiting the mysql queries, while the relevany function reduces these queries into smaller numbers of results (if u see what i mean!)

I was wondering if there was anything that I could perform on $r in the print_answers() function to achieve this??

i hope this makes sense, thanks in advance

alex
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You might want to have a look at http://dev.mysql.com/doc/mysql/en/fulltext-search.html.

Anyway, the principle of pagination is still the same:

1-) determine number of total rows (select count in a query, use http://www.php.net/count to count elements in an array)

2-) determine an offset and a limit (the same as you calculate it with mysql , based on $page and $rows_per_page)

3-) get the rows you want to show (simply return $data[$offset] to $data[$offset + $rows_per_page]. I'm pretty sure there is a function that allows you to grab that part of an array. Have a look at http://www.php.net/array)

Done ;)
Post Reply