Jump to content

random pagination


czukoman20

Recommended Posts

I have this pagination code that i've been working with. and i only have one issue with it.

Im trying to make it so the user is going through the pages it is in a random order.

I want to make it so that another user won't have that same order for fairness issues.

 

More explaination would be the

("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand()")  Function

makes a random order out of the users...  user3 user4 user1 user2 user5

When another person from a different computer comes into play .... it will be the exact same random order.

How would i make it so it repicks and order for every person.

So the first person would have 3 4 1 2 5

Second person would have 5 2 3 1 4

just examples.

 

If i am not clear... then please tell me . dont leave me hanging please

 

 <?php
// Connects to your Database
@mysql_connect("db1093.perfora.net", "dbo215169417", "TkA.Btc7") or die(mysql_error());
@mysql_select_db("db215169417") or die(mysql_error());

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand()") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 1;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM users WHERE userid_2 > 0 $max") or die(mysql_error());

//This is where you display your query results
while($values = mysql_fetch_array( $data_p ))
{
//Print $values'Name'];
echo "<br>";;
   $username = $values['username'];
   echo " <img src=\"http://www.adworld-online.com/images/banners/". $username ."_banner.jpg\" width=\"500\" height=\"95\"> <br>";
   echo ".::. Company: " . $values['companyname'] . "<br>  ";
   echo "Short info: " . $values['info'] . "<br> ";
if($session->logged_in){
   echo "<a href=\"http://www.adworld-online.com/submit.php?user=$username\">.::. Contact Us </a> <br />";
   }
else{
   echo "<a href=\"http://www.adworld-online.com/submit2.php?user=$username\">.::. Contact Us </a> <br />";
   }
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
?>

 

 

Link to comment
Share on other sites

Try making a getting a set of values from the db that are in range and randomly organizing them in php. Then query for that set of values. Something like this:

 

$uids = array();
$qry = mysql_query("SELECT userid_2 FROM users WHERE userid_2 > 0");
while ($row = mysql_fetch_assoc($qry))
  $uids[] = $row['userid_2'];
shuffle($uids);

$qry = mysql_query("SELECT * FROM users WHERE userid_2 IN (" . implode(",", $uids) . ")");

Link to comment
Share on other sites

Still doesn't work.. just to be clear tho .... u put the code like this ?? ?

 

<?php
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
//$data = mysql_query("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand(NOW())") or die(mysql_error());
$uids = array();
$data = mysql_query("SELECT userid_2 FROM users WHERE userid_2 > 0");
while ($row = mysql_fetch_assoc($data))
   $uids[] = $row['userid_2'];
shuffle($uids);

$data = mysql_query("SELECT * FROM users WHERE userid_2 IN (" . implode(",", $uids) . ")");
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;
//ect.....

?>


Link to comment
Share on other sites

it doesnt have any error... and here is the link to what i have

http://www.adworld-online.com/browse/ad3.php

 

it always starts with user 4 and banner 4.. there is no change within its randomness.

 

my datatable is setup with the table of users and then the subtable i am dealing with is userid_2 and the values of this if greater than 0 are the ones that i want to display info

Link to comment
Share on other sites

It is not useless. you dont quite fully understand.. oy. the site im building advertises banners. what i want to do is to be able to not make one banner have a better chance than the others. So i nailed it down to random order or pagination so thatway no DOUBLES appear. Thats my reasoning. I dont like ur rudeness, but i will see if what u said works ok. Thanks

Link to comment
Share on other sites

So the first person would have 3 4 1 2 5

Second person would have 5 2 3 1 4

 

to obtain this maybe you can do this

random those number and store that in session

and each time the page loads you get the specific number to use from your select as condition

Link to comment
Share on other sites

if you hav a user ID..... simply take all teh user ID's and place the m in an array and place that array in a cookie or session for tha tuser.  then all you do is do pagination with those ID's.... shuffle($IDs);  and then work from that.  only selecting rows in a given range in teh shuffles ID array/

 

I know itsmessay with allthe mysql queries.... but its one method.

 

gdlk

Link to comment
Share on other sites

if you hav a user ID..... simply take all teh user ID's and place the m in an array and place that array in a cookie or session for tha tuser.  then all you do is do pagination with those ID's.... shuffle($IDs);  and then work from that.  only selecting rows in a given range in teh shuffles ID array/

 

I know itsmessay with allthe mysql queries.... but its one method.

 

gdlk

if your talking about unknown user and not admins this not applicable

Link to comment
Share on other sites

ok .. maybe this

 

random a number that represents your BANNER ID ---

assuming we get this trough randomizing

 

$x=array(1=>3,2=>1,3=>5);

 

$_SESSION[count] =$_SESSION[count] +1;

if ($_SESSION[count]  == count($x)){

$_SESSION[count] = 1;

}

selecct  * from table where bannerid = $x[$_SESSION[count]];

 

 

just an idea hope it helps

Link to comment
Share on other sites

[code]$x=array(1=>3,2=>1,3=>5);

$_SESSION[count] =$_SESSION[count] +1;
if ($_SESSION[count]  == count($x)){
$_SESSION[count] = 1;
}
$data = mysql_query("SELECT * FROM users WHERE userid_2 = $x[$_SESSION[count]]") or die(mysql_error());

 

I put this down and i have a parse error

 

this line

$data = mysql_query("SELECT * FROM users WHERE userid_2 = $x[$_SESSION[count]]") or die(mysql_error());[/code]

 

error = Parse error: parse error, unexpected '[', expecting ']' in /homepages/19/d214339524/htdocs/browse/ad3.php on line 129

Link to comment
Share on other sites

this error is always reported here sorry

any ways you

$x=array(1=>3,2=>1,3=>5);

$_SESSION[count] =$_SESSION['count'] +1;
if ($_SESSION['count']  == count($x)){
$_SESSION['count'] = 1;
}
$data = mysql_query("SELECT * FROM users WHERE userid_2 = {$x[$_SESSION['count']]}") or die(mysql_error());

 

try

Link to comment
Share on other sites

well it works... but the randomization is still the same.. oy

 

would it have to do with anything in the second SELECT function

$x=array(1=>3,2=>1,3=>5);

$_SESSION[count] =$_SESSION['count'] +1;
if ($_SESSION['count']  == count($x)){
$_SESSION['count'] = 1;
}
$data = mysql_query("SELECT * FROM users WHERE userid_2 = {$x[$_SESSION['count']]}") or die(mysql_error());

$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM users WHERE userid_2 $max") or die(mysql_error());

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.