Problem with random results and pagination
Posted: Fri Mar 27, 2009 7:09 am
I have a search engine pulling the results from a MYSQL database with pagination.
THE PROBLEM: I need the results to come up random on all the pages after each search or whenever the page is refreshed without doubling. I can do it with one page but the problem is with more pages.
I'll be very grateful if somebody can come up with any ideas as I cannot find a solution anywhere.
Here is part of the code as it's too long so I hope this gives you an idea what I am trying to do:
THE PROBLEM: I need the results to come up random on all the pages after each search or whenever the page is refreshed without doubling. I can do it with one page but the problem is with more pages.
I'll be very grateful if somebody can come up with any ideas as I cannot find a solution anywhere.
Here is part of the code as it's too long so I hope this gives you an idea what I am trying to do:
Code: Select all
<?php
include("config.php");
$page = $_GET['page'];
$records_per_page = 7;
$padding = 2;
if(!filter_var($page, FILTER_VALIDATE_INT, 1))
$page=1;
$post_id = $_GET['post'];
$offset = ($page-1) * $records_per_page;
$result = mysql_query("SELECT * FROM logins , category WHERE logins.category_id = category.category_id and category.category = '$post_id' LIMIT $offset, $records_per_page");
function build_url($filename, $key, $value){
$values = array();
$query_str = array();
//get the query string arguments and store them in
//the $values array
parse_str($_SERVER['QUERY_STRING'], $values);
//loop through the $values array and add the appropriate
//keys to the query string
foreach($values as $k=>$v){
//IF, though, a key in the existing query string matches the same key
//we're trying to add, ignore it, since we'll add it manually in a moment
//This prevents having multiples of the same keys
if($k!=$key){
$query_str[] = "{$k}={$v}";
}
}
//add in our new key and value
$query_str[] = "{$key}={$value}";
//reconstruct the full URL using the implode() function to piece together all
//the query string values in the $query_string array, joining them together with "&"
return "$filename?".implode("&", $query_str);
}
$count_result = mysql_query("SELECT COUNT(*) FROM logins , category WHERE logins.category_id = category.category_id and category.category = '$post_id'");
$count_row = mysql_fetch_array($count_result);
//fetch the total number of rows in the table
$count = $count_row["COUNT(*)"];
while ($user_query = mysql_fetch_array ($count_result)) {
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo "$post_id"?></title>
<link href="style2.css" rel="stylesheet" type="text/css"/>
<link href="table.css" rel="stylesheet" type="text/css"/>
<link href="layout.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
</script>
<style type="text/css">
body {
color: #000;
font: 14px Arial, Helvetica, sans-serif;
}
</style>
</head>
<body link="#0A77CC" vlink="#0A77CC" alink="#0A77CC">
<div id="main">
<div><img src="images/header.gif" width="751" height="114" border="0" usemap="#Map" />
<map name="Map" id="Map">
<area shape="rect" coords="-2,0,755,120" href="index.php" />
</map>
</div>
<div id="content">
<div class="search1">
<div class="roundedcornr_box_797844">
<div class="roundedcornr_top_797844"><div></div></div>
<div class="roundedcornr_content_797844">
<div align="left" style="padding:0 20px 0 40px;">
<div style="max-height:40px;"><form action="view.php" method="get" name="search">
<div align="left" class="text1" style="margin-top:-10px; width:170px;">Business Search:</div>
<input style="margin-top:3px; vertical-align: top; margin-right:20px; height:25px; padding:6px 0 0 4px;" name="post" type="text" size="80" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" autocomplete="OFF"/>
<input onclick="$rand" style="vertical-align:bottom;" class="submit" type="IMAGE" src="images/search_small.gif" alt="Submit button" name="submit"/>
<input name="yes" type="hidden" value="yes" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -9px; left: 30px;" alt="upArrow" />
<div align="left" class="suggestionList" id="autoSuggestionsList">
</div>
</div>
</form></div>
</div>
</div>
<div class="roundedcornr_bottom_797844"><div></div></div>
</div>
</div>
</div>
<div align="right" class="page"><span style="padding:15px 10px 10px 15px;">
</span>
<div style="float:left; padding-left:10px; color: #0A77CC;"><?php echo "<b>$count</b>"; echo " results for "; echo "<b>\"$post_id\" </b>";?></div><div class="pager" ><?php
//we will use this information to build our navigation list
echo (($page>1)?"<a href='".build_url("view.php", "page", $page-1)."'>prev</a>":"prev")." | ";
for($i=1; $i<=($count/$records_per_page)+1; $i++){
if($i!=$page)
echo "<a href='".build_url("view.php", "page", $i)."'>$i</a>";
else
echo $i;
//output a little symbol (|) to seperate the links
//but not for the last link
if($i<$count/$records_per_page)
echo " | ";
}
echo " | ".(($page<$count/$records_per_page)?"<a href='".build_url("view.php", "page", $page+1)."'>next</a>":"next");?></div></div>
<div id="contentWrapper">
<div id="contentMain">
<table style="width:100%;">
<?php
$j = 2;
if($row = mysql_fetch_array($result)){
do{
if ($j % 2 == 0) {
$color = "vzebra-odd";
} else {
$color = "vzebra-odd2";
}?>
<tr id="hor-zebra" class="<?php echo ($color); ?>">
<td><?php echo "<span class= \"company\">{$row['company_name']}</span><br>"; ?>
<?php echo "<br>";?>
<?php echo ($row['address1']); ?>
<?php if (!empty($row['address2']) ){
echo $zap;
}?>
<?php echo ($row['address2']); ?>
<?php if (!empty($row['address3']) ){
echo $zap;
}?>
<?php echo ($row['address3']);?>
<?php if (($row['town']) > 0){
echo $zap;
}?>
<?php echo ($row['town']); ?>
<?php if (!empty($row['county']) ){
echo $zap;
}?>
<?php echo ($row['county']); ?>
<?php if (($row['post_code']) > 0){
echo $zap;
}?>
<?php echo ($row['post_code']); ?>
<?php echo "<br>";?>
<?php if (($row['telephone']) > 0){
echo $tel;
}?>
<?php echo ($row['telephone']); ?>
<?php if (($row['mobile']) > 0){
echo $mob;
}?>
<?php echo ($row['mobile']); ?>
<?php if (($row['fax']) > 0){
echo $fax;
}?>
<?php echo ($row['fax']); ?>
<?php if (!empty($row['email']) ){
echo $e_m;
}?>
<?php echo "<a href= mailto:" .$row['email'].">".$row['email']."</a>" ;?>
<?php if (!empty($row['www']) ){
echo $w;
}?>
<?php echo "<a href=http://".$row['www']." target=\"_blank\">".$row['www']."</a>"; ?>
<?php echo "<br>";?>
<?php if (!empty($row['bullet1']) ){
echo $dot;
}?>
<?php echo($row['bullet1']); ?>
<?php if (!empty($row['bullet2']) ){
echo $dot;
}?>
<?php echo ($row['bullet2']); ?>
<?php if (!empty($row['bullet3']) ){
echo $dot;
}?>
<?php echo ($row['bullet3']); ?></td>
</tr><?php
$j++;
} while($row = mysql_fetch_array($result));
}?>
</table>
<div style="margin-top:10px;">
<div align="right" class="page"> <div style="float:left; padding-left:10px; color: #0A77CC;"><?php echo "<b>$count</b>"; echo " results for "; echo "<b>\"$post_id\" </b>";?></div>
<div class="pager" >
<?php
//we will use this information to build our navigation list
echo (($page>1)?"<a href='".build_url("view.php", "page", $page-1)."'>prev</a>":"prev")." | ";
for($i=1; $i<=($count/$records_per_page)+1; $i++){
if($i!=$page)
echo "<a href='".build_url("view.php", "page", $i)."'>$i</a>";
else
echo $i;
//output a little symbol (|) to seperate the links
//but not for the last link
if($i<$count/$records_per_page)
echo " | ";
}
echo " | ".(($page<$count/$records_per_page)?"<a href='".build_url("view.php", "page", $page+1)."'>next</a>":"next");
?>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>