Having troubles with search
Posted: Mon Nov 14, 2005 6:32 pm
OK... I doubt anybody will be kind enough to look through this mess of code, but if you are, let me know why it's not working. What's happening is that when I type in keywords, and the results generate more than one page, I go to click on the link for Page 2 and nothing comes up. Page one works fine... It's only on my machine or I'd post a link. It's a long shot, but maybe somebody can help... I noted where I think the problem may be, but I dont know.
Code: Select all
<?php
include_once("common_functions.php");
include_once("common_classes.php");
include_once("directory_class.php");
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<form name="search" method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="keywords" value="" />
<input type="hidden" name="action" value="search" />
<input type="submit" value="Search" />
</form>
<?php
$error = new error;
$dirhandle = new dirHandle;
$dbh = new dataBase;
$table = 'directory';
$inc = 10;
if($dbh->connect()){
// Get keywords from URL
$keywords = $_GET['keywords'];
// Set default page, and then check to see if there is already a page set in the URL to display
$page = 1;
if(!empty($_GET['page'])){
$page = $_GET['page'];
}
// Starting record should be the amount of pages times the increment of records allowed per page minus that increment
$start = $page * $inc - $inc;
// Set default SQL Query
$sql = "SELECT * FROM " . $table . " LIMIT " . $start . ", " . $inc;
if($_GET['action'] == "search"){
// Initialize search class
$search = new search($table);
if($sql = $search->find($keywords)){
if(is_array($sql)){
// the find() mothod will return an array of queries if there is more than one keyword... if so, do this:
foreach($sql as $newsql){
if($results = $dbh->select($newsql)){
$amount += mysql_num_rows($results);
// The following line tells mysql where to start and how many to display, based on previously assigned variables
$newsql = $newsql . " LIMIT " . $start . ", " . $inc; // The problem is here I think
echo "$newsql<br>";
if($results = $dbh->select($newsql)){
while($row = $dbh->get_row($results, 'MYSQL_ASSOC')){
// Since we know there is keyword to search for, explode the words into an array
$key_words_array = explode(" ", $keywords);
foreach($key_words_array as $val){
// Now take each word and highlight it in the results
$row = $dirhandle->highlight($row, $val);
}
// Add the record to the html to display
$html .= $dirhandle->display($row);
}
}
else{
$error->Log($dbh->last_error);
}
}
else{
$error->Log($dbh->last_error);
}
}
}
else{
// If there is only one keyword...
if($results = $dbh->select($sql)){
$amount = mysql_num_rows($results);
// The following line tells mysql where to start and how many to display, based on previously assigned variables
$sql = $sql . " LIMIT " . $start . ", " . $inc;
while($row = $dbh->get_row($results, 'MYSQL_ASSOC')){
// Since we know there is only one keyword, we only need to run the highlight for that one keyword
$row = $dirhandle->highlight($row, $keywords);
$html .= $dirhandle->display($row);
}
}
else{
$error->Log($dbh->last_error);
}
}
}
else{
$error->Log($dbh->last_error);
}
}
else{
// If there are no words to search for...
if($results = $dbh->select($sql)){
$amount = $dbh->get_row_amount($table);
$sql = $sql . " LIMIT " . $start . ", " . $inc;
while($row = $dbh->get_row($results, 'MYSQL_ASSOC')){
$html .= $dirhandle->display($row);
}
}
else{
$error->Log($dbh->last_error);
}
}
if(!isset($amount)){
$amount = $dbh->get_row_amount($table);
}
echo $dirhandle->make_bread_crumbs($amount, $page, $inc, $_GET['action']);
// This html was created above.
echo $html;
echo $dirhandle->make_bread_crumbs($amount, $page, $inc, $_GET['action']);
}
$dbh->print_last_error();
?>