Login   Register  
PHP Classes
elePHPant
Icontem

File: class.Search.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ed Williams  >  Search  >  class.Search.inc  >  Download  
File: class.Search.inc
Role: ???
Content type: text/plain
Description: Search Class V1.2
Class: Search
Author: By
Last change:
Date: 2002-01-16 16:48
Size: 6,752 bytes
 

Contents

Class file image Download
<?
//////////////////////////////////////////////////////////////////////////
// CLASS : 
//    Search
// PURPOSE: 
//    A framework for mysql database searches and displaying the results
//    making it quick and easy to set up a html search form.
// VERSION : 
//    1.2
// AUTHOR : 
//    ed_williams@postmaster.co.uk
// USAGE : 
//    see end of file
//////////////////////////////////////////////////////////////////////////

class Search
{	

//////////////////////////////////////////////////////////////////////////
// private
//////////////////////////////////////////////////////////////////////////

	var $total;
	var $page; 
	var $start;
	var $end;
	var $dblink;
	
//////////////////////////////////////////////////////////////////////////
// public 
//////////////////////////////////////////////////////////////////////////
	
	var $links_perpage; // number of results displayed per page
	var $links_max; // number of navigation links to display per page

	//
	// constructor (chain with derived class)
	function Search($dblink)
	{
		global $page, $total;				
		
		// initialize values						
		$this->dblink = $dblink; 		
		$this->page  = intval($page);
		$this->total = intval($total);						
		
		// calculate total display pages
		if (!$this->total)
		{		
			/*	
			$res = mysql_query($this->SQL(), $this->dblink);
			$this->total = mysql_num_rows($res);
			*/
			$this->total = $this->GetTotal();
		}
		
		// init start and end of navbar
		$this->start = $this->page - ceil($this->links_max/2);
		$totalpages = ceil($this->total/$this->links_perpage);		
		if ($this->page+ceil($this->links_max/2) > $totalpages)
			$this->start = $totalpages-$this->links_max;
		if ($this->start < 0 )
			$this->start = 0;							
		$this->end = $this->links_max + $this->start;		
		if ($this->end > $totalpages)
			$this->end = $totalpages;
	}		
	
	//
	// Override these functions (called by class in order)	
	function GetTotal() 
	{ 
		$res = mysql_query($this->SQL(), $this->dblink);
		return mysql_num_rows($res);
	}
	function SQL() {}
	function Display_Result_Start($start, $end, $total) {}
	function Display_Result_Link($row) {}
	function Display_Result_End() {}	
	function Display_NavBar_Start($isactive) {}
	function Display_NavBar_Link($page, $isactive) {}
	function Display_NavBar_End() {}			
	
	//
	// display the search results
	function Display_Results()
	{											
		// calculate offsets
		$soff = 1 + ($this->page*$this->links_perpage);
		$eoff = $soff + $this->links_perpage;		
		if ($eoff > $this->total)
			$eoff = $this->total;				
		if ($soff > $this->total)
			$soff = $eoff = 0;
			
		// search and display results				
		$this->Display_Result_Start($soff, $eoff, $this->total);
		if ($this->total) 
		{			
			$res = mysql_query($this->SQL().sprintf(" limit %d,%d", 
					$this->page*$this->links_perpage, $this->links_perpage), 
					$this->dblink);

			while($row = mysql_fetch_array($res))
				$this->Display_Result_link($row);			
		}
		$this->Display_Result_End();
	}
		
	//
	// display the result navigation bar
	function Display_NavBar()
	{	
		if (!$this->total)
			return;		

		// display start scroll link
		$this->page--;
		$this->Display_NavBar_Start(($this->page+1));				
		$this->page++;

		// display in between		
		$page = $this->page;
		for($i=$this->start ; $i < $this->end; $i++)
		{														
			$this->page = $i;
			$this->Display_NavBar_Link($i+1, ($i != $page));									
		}
						
		// display end scroll link
		$this->page = $page+1;
		$this->Display_NavBar_End($this->end != $this->page);				
		$this->page--;
	}
	
	//
	// used to get an anchor tag within a class function
	function Anc($url)
	{
		return "<a href=\"".$this->URL($url)."\">";
	}
	
	//
	// used to get a URL within a class function
	function URL($url)
	{		
		$i = parse_url($url);
		$url .= (empty($i["query"]) ? "?" : "&");		
		$url .= "page=".$this->page."&total=".$this->total;
		return $url;
	}	
	function HasResults()
	{
		return $this->total;
	}
};

/////////////////////////////////////////////////////////////////////////////
// Example class see below:
/////////////////////////////////////////////////////////////////////////////
/*
class MySearch extends Search
{
	var $links_perpage =10;
	var $links_max = 20;
			
	function MySearch($dblink) { $this->Search($dblink); }		
	
	// provide select sql
	function SQL()
	{
		global $query;
		return "select * from auth_user where username like '%".$query."%'";
	}
	// display results
	function Display_Result_Start($start, $end, $total)
	{
		echo "Found ", $total, " displaying (",$start,"-", $end,")";	
	}
	function Display_Result_Link($row)
	{
		global $query;
		echo "<hr>", $this->Anc("display.html?uid=".$row["user_id"]."&query=".$query);
		echo $row["username"];
		echo "</a>";
	}
	function Display_Result_End()
	{
		echo "<hr>";
	}
	// display nav bar
	function Display_NavBar_Start($isactive)
	{
		global $query;
		echo ($isactive ? $this->Anc("index.html?query=".$query)."&lt;</a>" : "&lt;");
		echo "&nbsp;";
	}
	function Display_NavBar_Link($page, $isactive)
	{
		global $query;
		echo ($isactive ? $this->Anc("index.html?query=".$query).$page."</a>" : "<b>".$page."</b>");
		echo "&nbsp;";
	}
	function Display_NavBar_End($isactive)
	{
		global $query;
		echo ($isactive ? $this->Anc("index.html?query=".$query)."&gt;</a>" : "&gt;");
		echo "&nbsp;";
	}	
};

//////////////////////////////////////////////////////////////////////////////
// Example search form below:
//////////////////////////////////////////////////////////////////////////////

<!-- start index.html -->

<form action="index.html">
search <input type="text" name="query"><input type="submit">
</form>
<?

include("class.Search.inc");
include("myclass.Search.inc");

$dblink = mysql_pconnect("localhost", "root", "");
mysql_select_db("auth_user", $dblink);

$search = new MySearch($dblink);

$search->Display_Results();
$search->Display_NavBar();


<!-- end index.html -->

//////////////////////////////////////////////////////////////////////////////
// NOTES see below:
//////////////////////////////////////////////////////////////////////////////

When you display a hyperlink for the navagation bar you must use either
the class function $this->Anc(to display a anchor tag) or $this->URL (to
get a url string for an anchor tag).You must also make sure you pass along
any variables that the function SQL will need as this is called every time
a set of results is displayed.

NavBar functions only called when a sql query retrieves results.
*/
?>