Jump to content

Well this is new. Interesting problem with fulltext search and pagination.


man5

Recommended Posts

The issue I am having is that everytime I click the next button, it takes me to the index page.  My question is, how do I set up a proper search results link in "href="?page=1"

 

 

index.php

				<form action="search" method="GET">
					<input type="search" name="search" placeholder="Find products, services ...">
					<input type="submit" name="submit" value="">
				</form>

search.php

if(isset($_GET['submit']) && !empty($_GET['search'])) {	

	$value	= 	escape(trim(Input::get('search')));
	
	try { 

		// Find out how many items are in the table
		$q = DB::getInstance()->query("SELECT * FROM records WHERE MATCH(title) AGAINST('$value' IN BOOLEAN MODE)");
		
		$total = $q->count();
		
		// How many items to list per page
		$limit = 10;

		// How many pages will there be
		$pages = ceil($total / $limit);

		// What page are we currently on?
		$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
			'options' => array(
				'default'   => 1,
				'min_range' => 1,
			),
		)));
	
		// Calculate the offset for the query
		$offset = ($page - 1)  * $limit;

		// Some information to display to the user
		$start = $offset + 1;
		$end = min(($offset + $limit), $total);
		
		
		// Prepare the paged query
		$stmt = DB::getInstance()->query("SELECT users.*, records.* FROM records
		LEFT JOIN users ON records.user_id = users.user_id
		WHERE MATCH(title) AGAINST('$value' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT {$limit} OFFSET ".$offset);
		
		if($stmt->count()) {
			
			// Display the results
			foreach($stmt->results() as $row) {
				
				$date 			 = 	escape($row->posted);
				$record_date	 =	escape(Input::facebook_date_format($date));
				$record_id 		 = 	escape($row->record_id);
				$title			 = 	trim($row->title);
				$slug_title		 =	$row->slug_title;
				$view_count 	 = 	escape(intval(number_format($row->views + 1)));
				
				$username 		 = 	escape($row->username);
			
				require 'snippets/record_widget.php'; 
				
			}
			
		} else {
			?><div class="message"><?php echo 'No records found.';?></div><?php
		}
		
		<?php
		// The "back" link
		$prevlink = ($page > 1) ? '<a href="?page=1" title="First page"><img src="images/left-page-arrow-end.png"/></a> <a href="?page=' . ($page - 1) . '" title="Previous page"><img src="images/left-page-arrow-start.png"/></a>' : '<span class="disabled"><img src="images/left-page-arrow-end-disabled.png"/></span> <span class="disabled"><img src="images/left-page-arrow-start-disabled.png"/></span>';

		// The "forward" link
		$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page"><img src="images/right-page-arrow-start.png"/></a> <a href="?page=' . $pages . '" title="Last page"><img src="images/right-page-arrow-end.png"/></a>' : '<span class="disabled"><img src="images/right-page-arrow-start-disabled.png"/></span> <span class="disabled"><img src="images/right-page-arrow-end-disabled.png"/></a></span>';

		// Display the paging information
		?>
		<div class="pages">
			<span class="pages-arrows"><?php echo $prevlink; ?></span> <span class="pages-numbers"><?php echo $page; ?> of <?php echo $pages; ?></span> <span class="pages-arrows"><?php echo $nextlink; ?></span>
		</div>

	} catch (Exception $e) {
		echo '<p>', $e->getMessage(), '</p>';
	}
			

} else {

	Redirect::to('index');
	
}
Edited by man5
Link to comment
Share on other sites

I solved it.  For future reference, here is how the page links should be set up. Remember that it has to match the search results url in the search bar.

<?php
// The "back" link
$prevlink = ($page > 1) ? '<a href="?search='. $value .'&submit=&page=1" title="First page"><img src="images/left-page-arrow-end.png"/></a> <a href="?search='. $value .'&submit=&page=' . ($page - 1) . '" title="Previous page"><img src="images/left-page-arrow-start.png"/></a>' : '<span class="disabled"><img src="images/left-page-arrow-end-disabled.png"/></span> <span class="disabled"><img src="images/left-page-arrow-start-disabled.png"/></span>';

// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?search='. $value .'&submit=&page=' . ($page + 1) . '" title="Next page"><img src="images/right-page-arrow-start.png"/></a> <a href="?search='. $value .'&submit=&page=' . $pages . '" title="Last page"><img src="images/right-page-arrow-end.png"/></a>' : '<span class="disabled"><img src="images/right-page-arrow-start-disabled.png"/></span> <span class="disabled"><img src="images/right-page-arrow-end-disabled.png"/></a></span>';

// Display the paging information
?>
Link to comment
Share on other sites

form data is only available on the page request that the form submitted to. it's up to your code to propagate the search term with the pagination links. in the search.php code, there's no need to check the $_GET['submit'] or to even have it as a named field in the form, just make sure there's a $_GET['search'], assuming you only want to display anything if a search term was entered.

 

to build your pagination links, you should use a function like http_build_query, as it will let you take any existing $_GET parameters and combing in your page parameter. another thing http_build_query does for you, that your current code is lacking, is to urlencode all the values being put into the link, so that the link won't be broken if someone includes a search term that includes characters that must be urlencoded. http_build_query also lets you specify the separator to use between parameters, which for a link, should be &

Link to comment
Share on other sites

form data is only available on the page request that the form submitted to. it's up to your code to propagate the search term with the pagination links. in the search.php code, there's no need to check the $_GET['submit'] or to even have it as a named field in the form, just make sure there's a $_GET['search'], assuming you only want to display anything if a search term was entered.

 

to build your pagination links, you should use a function like http_build_query, as it will let you take any existing $_GET parameters and combing in your page parameter. another thing http_build_query does for you, that your current code is lacking, is to urlencode all the values being put into the link, so that the link won't be broken if someone includes a search term that includes characters that must be urlencoded. http_build_query also lets you specify the separator to use between parameters, which for a link, should be &

 

The reason I have the isset $_GET['submit'] is only because I want the user to redirect to index page if they click on the submit button without any key terms.

 

As for the http_build_query, can you show it in an example, using the above pagination code?

Edited by man5
Link to comment
Share on other sites

The reason I have the isset $_GET['submit'] is only because I want the user to redirect to index page if they click on the submit button without any key terms.

This is just as easy to do by checking if the search term has a value.  By using the submit check, now you just made your script need to retain that in the url rather than just the info you truly need, the search term.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.