hi there,

so i had a working search script, but it was all written as inline code/functions. i decided to rewrite the script but make it class based. im getting the following error though, "Query was empty". so i guess somehow $this->sql is not getting set. any ideas on whats going wrong here? here is the class:

class search {

/*  useage:
 *	$search = new search($string, $boolean);
 *  
 */

var $string;		//holds the search string
var $time;		//how long it took to do the query
var $result;		//holds the sql result resource
var $page_nav;	   //holds the formatted page navigation string w/ links
var $num_rslts;	    //holds the total number of results found
var $boolean;	     //true/false - is boolean mode inabled?, later holds 
var $sql;		 //holds the sql query
var $display_q;	    //holds the display query


/* Class Constructor */
function search($string, $boolean){
	$this->string = $this->clean($string);
	$this->boolean = $boolean;

	$this->run_search();

}

/* clean - makes the string ready/safe to be put into a query */
function clean($string){
	return mysql_real_escape_string($string);
}

/* turns true/false boolean mode into the query operator, will also do the reverse operation */
function make_boolean(){
	switch ($this->boolean){
		case true:
			$this->boolean = " IN BOOLEAN MODE";
			break;
		case false:
			$this->boolean = "";
			break;
		case " IN BOOLEAN MODE":
			$this->boolean = true;
			break;
		case "":
			$this->boolean = false;
			break;
	}
}

/* make the sql string  */
function make_sql($limits=""){
	$this->make_boolean();//switch to sql format

	$this->sql = "SELECT *, MATCH(".SEARCH_FIELDS.") AGAINST ('".$this->string."'"
				.$this->boolean.") AS score FROM ".TBL_PAPERS." WHERE MATCH(".SEARCH_FIELDS.") "
				."AGAINST ('".$this->string."'".$this->boolean.") ORDER BY score DESC".$limits;

	$this->make_boolean();//switch back to true/false
}

//lets run the query
function run_search(){
	global $database;

	//lets figure out how long it took to run the query	
	include("userspace/include/timer.php");
	$timer = new BC_Timer;
	$timer->start_time(); //start the timer

	$this->make_sql();
	//run the query
	$this->result = $database->query($this->sql) or die(mysql_error());

	$timer->end_time();//stop the timer
	$this->time = number_format($timer->elapsed_time(), 3);

	$this->num_rslts = mysql_num_rows($this->result);
	$this->make_pg_nav();//make the page navigation

	//run the query again, to cut down to only the rows to show
	$this->result = $database->query($this->sql) or die(mysql_error());
}


function make_pg_nav(){
	if (!isset($_GET['page'])){
		$current_page = 1;
	}elseif(strlen($_GET['page'])>0){
		$current_page = $_GET['page'];
	}
	// figure how many total pages
	$number_of_pages = floor($this->num_rslts / NUM_ROWS);
	if(($this->num_rslts % NUM_ROWS)>=1){
		$number_of_pages++;
	}
	// create page navigation links at bottom of table, as needed
	if($number_of_pages <= 1){
		$this->page_nav = "";
	}else{
		// we need to ensure that if a next or prev link is clicked, the search parameters
		// are not lost, so load a var with what is in the $_GET. 
		if(isset($_GET['page'])){
			//if page is already there, dont need to add anything to the uri till later
			$get_search = $_SERVER['REQUEST_URI'];
		}else{
			if(!strpos($_SERVER['REQUEST_URI'], "?")){ //need to check if "?" is in the uri
				//if not, add it
				$get_search = $_SERVER['REQUEST_URI']."?page=".$current_page;
			}else{
				//if so, use some glue
				$get_search = $_SERVER['REQUEST_URI']."&amp;page=".$current_page;
			}
		}

		for ($i=1; $i<=$number_of_pages; $i++){
			if($i == 1){ // start with PREV button
				if ($current_page <= 1){
					$prev_number = 1;
					$page_navigation = ""; 
				}else{
					$prev_number = $current_page - 1;
					$this->page_nav = "<a href='".str_replace("page=".$current_page, "page="
								   	   .$prev_number, $get_search)."' class='prev' "
									   ."title='Previous'>&nbsp;</a> "; 
				}
			}
			if($i == $current_page) { // bold current page
				$this->page_nav .= "<strong>".$i."</strong>&nbsp;";
			}else{
				$this->page_nav .= "<a href='".str_replace("page=".$current_page, "page="
									.$i, $get_search)."'>".$i."</a>&nbsp;";
			}
			if($i == $number_of_pages){ // end with NEXT button
				if ($current_page >= $number_of_pages){
					$next_page = $number_of_pages;
				}else{
					$next_page = $current_page + 1;
					$this->page_nav .= "<a href='".str_replace("page=".$current_page, "page="
										.$next_page, $get_search)."' class='next' title='Next'>"
										."&nbsp;</a>";
				}

			}
		}
	}

	// set starting point for database query
	if ($current_page == 1) { $start_row = 0; }
	else { $start_row = NUM_ROWS * ($current_page-1); }
	// now, get only rows to be displayed
	$limits = " LIMIT ".$start_row.", ".NUM_ROWS;
	$this->sql = $this->make_sql($limits);
}

function make_display_q(){
	$this->display_q = 	 "Looking in field(s) ".SEARCH_FIELDS." for "
						."<strong>'".$this->string."'</strong><br />"
						."and found ".$this->num_rslts." match for your search criteria in "
						.$this->time." seconds";
}

}; //end class search

    It would be handy if you could show us an example of it being used when you get the error.

      sorry, i don't have an example on a public server yet (im waiting for the admin to set up everything), im testing on a local machine. i call the class from a file called publications.php, which displays scholarly papers and data from a database. when i run publications.php the default page displays properly (shows all the papers). if $GET['searchstring'] is set, then i activate the search class.

      $search = new search($_GET['searchstring'], $boolean);

      where boolean is true or false.

      i plan to hand the result of either method to a loop that displays the data in a table. i only get the error when i search, and the page displays properly up until the point where i would expect generated output to come, and thats where the error displays. i think the problem is that somehow, $search->sql is not getting set. let me know if this helps.

        Sorry, I had assumed it would have a more complicated interface, I never realised you would just call the search method.

        I cant see anything. Have you tried doing a print_r of the object? just so you can see whats in it? or get it to echo out the SQL.

          Write a Reply...