Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem with random results and pagination
Old 03-27-2009, 09:30 AM Problem with random results and pagination
Junior Talker

Posts: 2
Name: virtuoso
Trades: 0
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.
virtuoso1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2009, 02:24 PM Re: Problem with random results and pagination
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Have I understood you correctly? Do you want to randomize the result when using your search engine? And the problem lies in that you want it to be random the first time, but it gets randomized every time a click a new page, so you want to randomize only when you enter the search, but then not again until you enter a new search?

I'm not entirely sure of how to do that evan for a single page, but since you have, can't you use a random function which uses a seed, and then store the seed and reuse it for every page, making it create the same random sequence again? Keep using that seed until next search where a new seed is generated.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-28-2009, 01:25 PM Re: Problem with random results and pagination
reli4nt's Avatar
Extreme Talker

Posts: 168
Location: New York
Trades: 0
You can query for a list of the ids,
randomize the order,
store this in a session or cookie
and and then php to set up the links tot he subsequent and previous pages rather than sql.

In each page you just need to find the correct range of the results and pull the results for those.

Hope that makes sense.
__________________

Please login or register to view this content. Registration is FREE

Designing the world we live in.
Defining the terms we live by.
reli4nt is offline
Reply With Quote
View Public Profile Visit reli4nt's homepage!
 
Old 03-30-2009, 05:13 AM Re: Problem with random results and pagination
Junior Talker

Posts: 2
Name: virtuoso
Trades: 0
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:
<?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 "&nbsp;results for&nbsp;"; 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 "&nbsp;results for&nbsp;"; 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>
virtuoso1 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem with random results and pagination
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26062 seconds with 12 queries