Having troubles with search

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Having troubles with search

Post by Luke »

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();
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would guess that $page isn't being set correctly,
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

It's giving me this... and page is set to 2, so it's right:

Code: Select all

SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('interest') LIMIT 10, 10
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are you sure there are more than 10 records that match?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Feyd... this is both the answer and not the answer at the same time... you just made me realize...
The code is checking for records 10-20 on each particular query, well there is a series of queries that add up to the 18 returned records, so this is not working because of that.

Code: Select all

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... because this is checking for records 10-20 on each particular query
                        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);
                    }
                }
            }
Is there a way to accomplish the following in one query?

Code: Select all

SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('successfully') LIMIT 10, 10
SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('properties') LIMIT 10, 10
SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('office') LIMIT 10, 10
SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('special') LIMIT 10, 10
SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('interest') LIMIT 10, 10
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

have you tried this:

Code: Select all

SELECT * FROM directory WHERE MATCH (directory_name,directory_description) AGAINST ('successfully  properties office special interest') LIMIT 10, 10;
does that give you anything similar to what you want ?

or do you need a bunch of separate results ?
Post Reply