Search string multipage link

Hi, - I have a simple database, where you can post data for a person. Within that database you can search within the data results.

There are 15 posts per page, at the moment when I do a search and there are more than 15 results, the first page shows the relevant results to the search but when you click the next page link it just goes to page 2 of all the database posts rather than page 2 of the searched results.

Here is the code for the multipage links:

<?php echo multipage(get_num_posts(), 15, $page, '/admin/database/posts/index.php?page='); ?>

And here is the code for the page which search’s through each database cell.

<?php

define('ADMIN_ROOT_PATH', './../../');
require ADMIN_ROOT_PATH . 'includes/common.inc.php';

$tpl	= new template(ADMIN_ROOT_PATH . 'templates/');
$body	= new template(ADMIN_ROOT_PATH . 'templates/database/posts/');

$tpl->set('title', $config['site_name'] . ' - Admin Panel - Database - Posts');
$tpl->set('navigation', '<a href="/">Home</a> &raquo; <a href="/admin/">Admin Panel</a> &raquo; <a href="/admin/database/">Database</a> &raquo; Posts');

// Posts

if (!isset($_POST['submit'])) {
	/**
	* get_num_posts() - get the number of posts
	*
	* @return   integer, number of posts
	*/
	function get_num_posts() {
		$search_string = isset($_POST['search_string']) ? check_input($_POST['search_string']) : '';

		$result = $GLOBALS['db']->query("SELECT COUNT(p.post_id) FROM " . $GLOBALS['config']['db']['prefix'] . "database_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "categories c ON p.category_id = c.category_id WHERE p.title LIKE '$search_string' OR p.age LIKE '$search_string' OR p.agegroup LIKE '$search_string' OR p.gender LIKE '$search_string' OR p.ethnic_id LIKE '$search_string' OR p.religion LIKE '$search_string' OR p.lifestage LIKE '$search_string' OR p.address_line1 LIKE '$search_string' OR p.address_line2 LIKE '$search_string' OR p.city LIKE '$search_string' OR p.county LIKE '$search_string' OR p.postcode LIKE '$search_string' OR p.occupation LIKE '$search_string' OR p.ocother LIKE '$search_string' OR p.jobtitle LIKE '$search_string' OR p.hoccupation LIKE '$search_string' OR p.hjobtitle LIKE '$search_string' OR p.hseg LIKE '$search_string' OR p.email LIKE '$search_string' OR p.phone LIKE '$search_string' OR p.mobile LIKE '$search_string' OR p.cage LIKE '$search_string' OR p.cage2 LIKE '$search_string' OR p.cage3 LIKE '$search_string' OR p.cage4 LIKE '$search_string' OR p.cage5 LIKE '$search_string' OR p.clifestage LIKE '$search_string' OR p.clifeother LIKE '$search_string' OR p.pets LIKE '$search_string' OR p.pets2 LIKE '$search_string' OR p.pets3 LIKE '$search_string' OR p.pets4 LIKE '$search_string' OR p.cmake LIKE '$search_string' OR p.cmake2 LIKE '$search_string' OR p.cmodel LIKE '$search_string' OR p.regyear LIKE '$search_string' OR p.ctype LIKE '$search_string' OR p.ctype2 LIKE '$search_string' OR p.mobprov LIKE '$search_string' OR p.mobprov2 LIKE '$search_string' OR p.internet LIKE '$search_string' OR p.internet2 LIKE '$search_string' OR p.gas LIKE '$search_string' OR p.gas2 LIKE '$search_string' OR p.electric LIKE '$search_string' OR p.electric2 LIKE '$search_string' OR p.landline LIKE '$search_string' OR p.landline2 LIKE '$search_string' OR p.tvprov LIKE '$search_string' OR p.tvprov2 LIKE '$search_string' OR p.pastres LIKE '$search_string' OR p.socialgrad LIKE '$search_string' OR p.reshistory LIKE '$search_string' OR p.reshistory2 LIKE '$search_string' OR p.findme LIKE '$search_string' OR p.findme2 LIKE '$search_string' OR p.content LIKE '$search_string' OR c.category_name LIKE '$search_string'");

		return $GLOBALS['db']->result($result, 0);
	}

	/**
	* get_posts() - get the posts
	*
	* @param    $page integer, current page
	* @return   array, posts
	*/
	function get_posts($page) {
		$search_string = isset($_POST['search_string']) ? check_input($_POST['search_string']) : '';

		$result = $GLOBALS['db']->query("SELECT p.*, c.category_name FROM " . $GLOBALS['config']['db']['prefix'] . "database_posts p LEFT OUTER JOIN " . $GLOBALS['config']['db']['prefix'] . "categories c ON p.category_id = c.category_id WHERE p.title LIKE '$search_string' OR p.age LIKE '$search_string' OR p.agegroup LIKE '$search_string' OR p.gender LIKE '$search_string' OR p.ethnic_id LIKE '$search_string' OR p.religion LIKE '$search_string' OR p.lifestage LIKE '$search_string' OR p.address_line1 LIKE '$search_string' OR p.address_line2 LIKE '$search_string' OR p.city LIKE '$search_string' OR p.county LIKE '$search_string' OR p.postcode LIKE '$search_string' OR p.occupation LIKE '$search_string' OR p.ocother LIKE '$search_string' OR p.jobtitle LIKE '$search_string' OR p.hoccupation LIKE '$search_string' OR p.hjobtitle LIKE '$search_string' OR p.hseg LIKE '$search_string' OR p.email LIKE '$search_string' OR p.phone LIKE '$search_string' OR p.mobile LIKE '$search_string' OR p.cage LIKE '$search_string' OR p.cage2 LIKE '$search_string' OR p.cage3 LIKE '$search_string' OR p.cage4 LIKE '$search_string' OR p.cage5 LIKE '$search_string' OR p.clifestage LIKE '$search_string' OR p.clifeother LIKE '$search_string' OR p.pets LIKE '$search_string' OR p.pets2 LIKE '$search_string' OR p.pets3 LIKE '$search_string' OR p.pets4 LIKE '$search_string' OR p.cmake LIKE '$search_string' OR p.cmake2 LIKE '$search_string' OR p.cmodel LIKE '$search_string' OR p.regyear LIKE '$search_string' OR p.ctype LIKE '$search_string' OR p.ctype2 LIKE '$search_string' OR p.mobprov LIKE '$search_string' OR p.mobprov2 LIKE '$search_string' OR p.internet LIKE '$search_string' OR p.internet2 LIKE '$search_string' OR p.gas LIKE '$search_string' OR p.gas2 LIKE '$search_string' OR p.electric LIKE '$search_string' OR p.electric2 LIKE '$search_string' OR p.landline LIKE '$search_string' OR p.landline2 LIKE '$search_string' OR p.tvprov LIKE '$search_string' OR p.tvprov2 LIKE '$search_string' OR p.pastres LIKE '$search_string' OR p.socialgrad LIKE '$search_string' OR p.reshistory LIKE '$search_string' OR p.reshistory2 LIKE '$search_string' OR p.findme LIKE '$search_string' OR p.findme2 LIKE '$search_string' OR p.content LIKE '$search_string' OR c.category_name LIKE '$search_string' ORDER BY title DESC LIMIT " . ($page - 1) * 15 . ", 15");

		return fetch_array($result);
	}

	$body->set('page', isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
	$tpl->set('body', $body->fetch('index.tpl.php'));
} else {
	$result = $db->query("SELECT post_id FROM " . $config['db']['prefix'] . "database_posts");

	while ($post = $db->fetch_array($result)) {
		$status = isset($_POST['status' . $post['post_id']]) && $_POST['status' . $post['post_id']] == 0 ? 0 : 1;
		$delete = isset($_POST['delete' . $post['post_id']]) && $_POST['delete' . $post['post_id']] == 'yes' ? 'yes' : 'no';

		if (isset($_POST['status' . $post['post_id']])) {
			$db->query("UPDATE " . $config['db']['prefix'] . "database_posts SET status = $status WHERE post_id = $post[post_id]");
		}

		if ($delete == 'yes') {
			$db->query("DELETE FROM " . $config['db']['prefix'] . "database_posts WHERE post_id = $post[post_id]");
			$db->query("DELETE FROM " . $config['db']['prefix'] . "database_comments WHERE post_id = $post[post_id]");
		}
	}

	output('Posts successfully updated!');
	redirect('/admin/database/posts/', 2);
}

echo $tpl->fetch('layout.tpl.php');

?>

If anyone has any tips, or ideas on how to setup the multipage link to work with the search that would be fantastic.

Thanks,

Paul

Ok, I have tried for the past couple of hours to crack this, and made pretty much no progress. I have sussed a few things that I would assume the problem to be…

This is how the body is set…

$body->set('search_string', isset($_REQUEST['search_string']) ? check_input($_REQUEST['search_string']) : '');
	$body->set('page', isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
	$tpl->set('body', $body->fetch('index.tpl.php'));

This is the search line for multipage…

<?php echo multipage(get_num_posts(), 15, $page, '/admin/database/posts/index.php?search_string=' . $search_string . '&amp;page='); ?>

Now, when I do a search the links show the correct search phrase ‘search_string=Your Search’ but it still loads the 2nd page of all the database posts rather than the second page for the ‘Your Search’ results.

I am not sure why the search_string isn’t requesting the actual search as the relevant page.

Here is the function for multipage, maybe something in here needs to be configured to work?

<?php function multipage($num, $per_page, $page, $page_url) {
	$multipage = array();

	if ($num > 0) {
		$multipage[] = 'Page:';
	}

	if ($page > 1) {
		$multipage[] = '<a href="' . $page_url . 1 . '">&laquo;</a>';
		$multipage[] = '<a href="' . $page_url . ($page - 1) . (substr($page_url, -1) == '/' ? '/' : '') . '">&#8249;</a>';
	}

	for ($i = ($page <= 2 ? 1 : $page - 2); $i <= ($page >= ceil($num / $per_page) - 2 ? ceil($num / $per_page) : $page + 2); $i++) {
		$multipage[] = $i == $page ? '<u>' . $i . '</u>' : '<a href="' . $page_url . $i . (substr($page_url, -1) == '/' ? '/' : '') . '">' . $i . '</a>';
	}

	if ($page < ceil($num / $per_page) - 2) {
		$multipage[] = '... ' . '<a href="' . $page_url . ceil($num / $per_page) . (substr($page_url, -1) == '/' ? '/' : '') . '">' . ceil($num / $per_page) . '</a>';
	}

	if ($page < ceil($num / $per_page)) {
		$multipage[] = '<a href="' . $page_url . ($page + 1) . (substr($page_url, -1) == '/' ? '/' : '') . '">&#8250;</a>';
		$multipage[] = '<a href="' . $page_url . ceil($num / $per_page) . (substr($page_url, -1) == '/' ? '/' : '') . '">&raquo;</a>';
	}

	return implode('&nbsp;', $multipage);
} ?>

I’m in the thinking stages of thinking this is the problem…

$body->set('search_string', isset($_REQUEST['search_string']) ? check_input($_REQUEST['search_string']) : '');

When you search something, the link shows correctly as it should. E.g. /admin/database/posts/index.php?search_string=MY SEARCH&page=2

All results showing MY SEARCH should return, but they don’t.

That is where I assume the $body->set line isn’t working correctly, as the body should be the search string when a search is made.

Any help would be so hugely helpful :slight_smile: