Page 1 of 1

Passing Keyword Variable in Paginated Results

Posted: Sat Jun 04, 2005 9:51 am
by BZorch
I have been trying to figure this out all week experimenting with my limited knowledge of PHP and searching this forum and the web to find out what I am doing wrong. I have successfully gotten a query with the keyword embedded in the code to successfully paginate as well as a dynamic query that does not paginate the results. When I use a form to create a dynamic keyword search, I can not get the code to recognize the keyword past the first results. The keyword is not passed to the other paginated pages which results in a new search as if I am starting over again. My code for the search and form is below. I apologize if it is too long, but I felt it was probably a fundamental error and the whole code was necessary to see it. Any help would be appreciated.

Code: Select all

<?php
if  (isset($_GET['keyword'])) {
$kw='%'.$_GET['keyword'].'%';
}else { if  (isset($_GET['search'])) {
//Handle search
if (empty($_GET['keyword'])){
$kw=FALSE;
$message .= '<p>You did not enter a keyword.</p>';

} else {$kw='%'.$_GET['keyword'].'%';
}
}
}

if ($kw){

require_once ('mysql_connect.php'); // Connect to the db.

// Number of records to show per page:
	$display = 10;
	
// Determine how many pages there are. 
	if (isset($_GET['np'])) { // Already been determined.
		$num_pages = $_GET['np'];
	} else { // Need to determine.
		$query = "SELECT File_Name, Categories FROM Baldwin_park_test WHERE Baldwin_park_test.Categories LIKE '$kw' 
OR Baldwin_park_test.City LIKE '$kw' OR Baldwin_park_test.State LIKE '$kw' OR Baldwin_park_test.Country LIKE '$kw' 
OR Baldwin_park_test.Site1 LIKE '$kw' ORDER BY File_Name ASC"; // Standard query.
		$query_result = mysql_query ($query);
		$num_records = @mysql_num_rows ($query_result);
		
		if ($num_records > $display) { // More than 1 page.
			$num_pages = ceil ($num_records/$display);
		} else {
			$num_pages = 1;
		}
	}
// Determine where in the database to start returning results.
	if (isset($_GET['s'])) { // Already been determined.
		$start = $_GET['s'];
	} else {
		$start = 0;
	}
//search for keyword

$query="SELECT File_Name, Categories FROM Baldwin_park_test WHERE Baldwin_park_test.Categories LIKE '$kw' 
OR Baldwin_park_test.City LIKE '$kw' OR Baldwin_park_test.State LIKE '$kw' OR Baldwin_park_test.Country LIKE '$kw' 
OR Baldwin_park_test.Site1 LIKE '$kw' ORDER BY File_Name ASC LIMIT $start, $display";
$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows ($result); // How many photos are there?

if ($num > 0) { // If it ran OK, display the records.

	
		// Make the links to other pages, if necessary.
		if ($num_pages > 1) {
			
			echo '<p>';
			// Determine what page the script is on.	
			$current_page = ($start/$display) + 1;
			
			// If it's not the first page, make a Previous button.
			if ($current_page != 1) {
				echo '<a href="'. $_SERVER['PHP_SELF'].'?s=' . ($start - $display) . '&np=' . $num_pages . '&keyword='.$_GET['keyword']. '">Previous</a> ';
			}
			
			// Make all the numbered pages.
			for ($i = 1; $i <= $num_pages; $i++) {
				if ($i != $current_page) {
					echo '<a href="'. $_SERVER['PHP_SELF'].'?s=' . (($display * ($i - 1))) . '&np=' . $num_pages .'&keyword='.$_GET['keyword']. '">' . $i . '</a> ';
				} else {
					echo $i . ' ';
				}
			}
			
			// If it's not the last page, make a Next button.
			if ($current_page != $num_pages) {
				echo '<a href="'. $_SERVER['PHP_SELF'].'?s=' . ($start + $display) . '&np=' . $num_pages .'&keyword='.$_GET['keyword']. '">Next</a>';
			}
			
			echo '</p><br />';
			
		} // End of links section.
	
	// Fetch and print all the records.
	while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
		echo "\n<div id=\"pix_div\"><a href=\"../../Baldwin_Park/".stripslashes($row[0])."\"> <img alt=\"whatever\" src=\"../../Baldwin_Park/Thumbnails/".stripslashes($row[0])."\"" ."</a></div>\n";
	}

	echo '</div>';
	mysql_free_result ($result); // Free up the resources.	

} else { // If it did not run OK.
	echo '<p>There are no records that match your keyword.</p>'; 
}

mysql_close(); // Close the database connection.
}


?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<div align="center">
</div>
<p align="center"><b>Keyword:</b> 
  <input type="text" name="keyword" size="50" maxlength="50" value="<?php if (isset($_GET['keyword'])) echo $_GET['keyword']; ?>" />
   <input type="submit" name="search" value="Go" /></p>
</form><!-- End of Form -->