Jump to content

search string multipage link...


jpopuk

Recommended Posts

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='); ?>  

 

Here is the main PHP code for the whole page.

 

<?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> » <a href="/admin/">Admin Panel</a> » <a href="/admin/database/">Database</a> » 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('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'));
} 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');

?>

 

Here is the code for the index.tpl.php file which is the template for the PHP code.

 

<div style="position: absolute; top: 5px; right: 5px;"><a href="/admin/database/posts/new.php"><img src="/admin/images/button_new.gif" border="0" alt="New" /></a></div>
<form method="post" action="/admin/database/posts/">
<p align="center" style="margin-top: 0;">Search <input type="text" name="search_string" value="<?php echo isset($_POST['search_string']) ? $_POST['search_string'] : ''; ?>" /> <input type="submit" name="search" value="Go" /></p>
</form>
<form method="post" action="/admin/database/posts/">
<div class="table_box">
	<table border="0" cellpadding="3" cellspacing="1" width="100%">
		<tr>
			<td colspan="7" class="header"><h1>Posts</h1></td>
		</tr>
		<tr>
			<td width="10%" class="table_row"><strong>Delete:</strong></td>
		  	<td width="30%" class="table_row"><strong>Name:</strong></td>
			<td width="50%" class="table_row"><strong>Email:</strong></td>
			<td width="20%" class="table_row"></td>
		</tr>
		<?php foreach (get_posts($page) as $post) { ?>
			<tr>
				<td align="center" width="10%" class="table_row"><input type="checkbox" name="delete<?php echo $post['post_id']; ?>" value="yes" /></td>
				<td width="30%" class="table_row"><a href="/admin/database/posts/edit.php?post_id=<?php echo $post['post_id']; ?>" onclick="return popup('/admin/database/posts/edit.php?post_id=<?php echo $post['post_id']; ?>')"><?php echo $post['title']; ?></a></td>
				<td width="50%" class="table_row"><a href="mailto:<?php echo $post['email']; ?>"><?php echo $post['email']; ?></a></td>
				<td width="20%" class="table_row"><a href="/admin/database/posts/edit.php?post_id=<?php echo $post['post_id']; ?>" onclick="return popup('/admin/database/posts/edit.php?post_id=<?php echo $post['post_id']; ?>')"><img src="/admin/images/button_edit.gif" border="0" alt="Edit" /></a></td>
			</tr>
            <?php $emails[] = $post['email']; } if(0 < ($num = (int)get_num_posts())){
		$emails = implode(";",array_unique($emails)); } ?>
	</table>
</div>
    <?php echo multipage(get_num_posts(), 15, $page, '/admin/database/posts/index.php?search_string=' . $search_string . '&page='); ?>
    <table align="center" width="25%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><p align="center"><?php if(0 < ($num = (int)get_num_posts())){
		printf(
			   '<a href="mailto:[email protected]?bcc='. $emails .'"><img src="/admin/images/button_email.gif" width="138" height="22" border="0" /></a>', 
			   $num
		);
}
?></p></td>
    <td><p align="center"><input type="submit" name="submit" value="Submit Changes" /></p></td>
  </tr>
</table>
</form>
<script language="javascript" type="text/javascript">
<!--
function popup(url) {
newwindow=window.open(url,'name','height=600,width=800,scrollbars=1');
if (window.focus) {newwindow.focus()}
return false;
}

// -->
</script>

 

I have tried this multipage line, but this doesn't work:

 

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

 

All I am trying to achieve is when you view the data and click next it will go to the next page in the database, but when you do a search and the results load, when you click next for the search results it will load the relevant set of results on the second page rather than the second page of all the database posts.

 

I hope this makes sense.

 

Cheers,

 

Paul

Link to comment
https://forums.phpfreaks.com/topic/198634-search-string-multipage-link/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.