Jump to content

random pagination issue


czukoman20

Recommended Posts

I've been workin on perfecting this code for quite awhile.. and now the only issue left is to display the banners in a random order

 

<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM users WHERE type_market = 'sto' ORDER BY rand()") or die(mysql_error());


$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM users WHERE type_market = 'sto' $max ") or die(mysql_error());
while($values = mysql_fetch_array( $data_p ))
{
echo "<br>";;
$username = $values['username'];
$temp = $values['temp'];
if($session->logged_in)
{
$sub = "submit2.php";
}else{
$sub = "submit2.php";
}

$types = array("jpg","gif","png","swf");
$dir = dirname(__FILE__)."/images/banners/";
foreach($types as $type)
{
if(file_exists($dir.$username."_banner.".$type))
{

	echo "<a href=\"http://www.adworld-online.com/sub-temp/$temp?user=$username\"target="._blank."><img src=\"http://www.adworld-online.com/browse/images/banners/".$username."_banner.".$type."\" width=\"600\" height=\"110\" border=\"0\"><br />";
}
}
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
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> ";
}
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> ";
}
?>

 

 

ORDER by RAND() doesn't work.. because it just pics the same random order every time you load the page up.

ex. order 5 numbers by rand = 42531 if you start over it will still order them in 42531

 

I would like it to display them in a random order everytime the page is refreshed on page1

I can't seem to figure it out.

help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/109675-random-pagination-issue/
Share on other sites

something like this:

<?php
$rand_array = array();
while ($array_row = mysql_fetch_assoc($data)){
$rand_array[] = $array_row['banner']; /* <-- CHANGE THIS!!! */
}
$high = count($rand_array) - 1;
$r = rand(0,$high);
$random_banner = $rand_array[$r];
?>

You'll need to figure out where to place this in your code, but it will take all the items in your db, add them into an array, count the array, subtract 1 (because arrays start at 0, not 1), then pick a random one. 

 

Make sure too add that into a foreach or while loop, or you'll get the same arrangement every time (I think.....tired....)

 

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.