Hi
What I am trying to do is build one parent class that then calls subclasses to query a database for a search term. I can get this to work with one subclass (SearchNews) but when trying to call a second child class (SearchProjects) all the variables are reset and only the Projects results are returned.
The calling code is:
$Search = new Search($keywords);
$News = new SearchNews($keywords);
$Projects = new SearchProjects($keywords);
$News->getResults();
$Projects->getResults();
$Search->writeResults();
And the Search classes are:
<?php
//Full text searching on news, projects and pages
class Search {
protected $totalResults, $results, $keywords;
function __construct($keywords) {
$this->keywords = $keywords;
$this->totalResults = 0;
$this->results = array();
}
protected function addResults($results) {
$this->results = $this->results+$results;
}
protected function setResults($toAdd) {
$this->totalResults = $this->totalResults+$toAdd;
}
protected function returnTotal() {
return $this->totalResults;
}
protected function sortResults() {
asort($this->results, SORT_NUMERIC);
}
public function writeResults() {
$this->sortResults();
$this->return = "<p>The search keywords '<em>".$this->keywords."</em>' returned ".$this->returnTotal()." results.</p>";
$this->return .= "<table class=\"searchTable\">";
$this->return .= "<tr><th width=\"250\">Title</th><th>Body</th><th width=\"100px\">Relevance</th><th width=\"100px\">Area</th></tr>";
foreach($this->results as $res) {
$this->return .= "<tr>";
$this->return .= "<td><a href=\"".$res['URL']."\">".$res['Title']."</a></td>";
$this->return .= "<td>".cleanOutput($res['Copy'])."</td>";
$this->return .= "<td align=\"center\">".$res['Score']."</td>";
$this->return .= "<td align=\"center\">".$res['Area']."</td>";
$this->return .= "</tr>";
}
$this->return .= "</table>";
return $this->return;
}
}
class SearchNews extends Search {
function __construct($keywords) {
parent::__construct($keywords);
}
public function getResults() {
$query = "SELECT `Title`, `ID`, `Intro`, MATCH(`Title`, `Copy`, `Intro`) AGAINST ('".$this->keywords."' IN BOOLEAN MODE) AS `Score` FROM tblnews WHERE MATCH(`Title`, `Copy`, `Intro`) AGAINST ('".$this->keywords."' IN BOOLEAN MODE) ORDER BY `Score` DESC LIMIT 10";
$results = mysql_query($query) or die("Cannot perform news search: ".mysql_error()."<br />".$query);
$tr = parent::setResults(mysql_num_rows($results));
if(mysql_num_rows($results) > 0)
while($res = mysql_fetch_array($results))
$this->results[] = array('Score'=>(round($res['Score'], 3)*100), 'Title'=>$res['Title'], 'Copy'=>firstParagragh($res['Intro']), 'URL'=>'/news/'.$res['ID'].'/', 'Area'=>'News');
}
}
class SearchProjects extends Search {
function __construct($keywords) {
parent::__construct($keywords);
}
public function getResults() {
$query = "SELECT `Title`, `ProjectID`, `Copy`, MATCH(`Title`, `Copy`) AGAINST ('".$this->keywords."' IN BOOLEAN MODE) AS `Score` FROM tblprojects WHERE MATCH(`Title`, `Copy`) AGAINST ('".$this->keywords."' IN BOOLEAN MODE) ORDER BY `Score` DESC LIMIT 10";
$results = mysql_query($query) or die("Cannot perform news search: ".mysql_error()."<br />".$query);
$tr = parent::setResults(mysql_num_rows($results));
if(mysql_num_rows($results) > 0)
while($res = mysql_fetch_array($results))
$this->results[] = array('Score'=>(round($res['Score'], 3)*100), 'Title'=>$res['Title'], 'Copy'=>firstParagragh($res['Copy']), 'URL'=>'/projects/'.$res['ProjectID'].'/', 'Area'=>'Projects');
}
}
?>
I thought using $this->results[] would add to the parent variable but im guessing the parent constructor is resetting the variables when the second child class is called. How do i get around this (keep existing variable data)?
thanks