View Single Post
Old 10-30-2011, 03:26 PM  
Tempest
Too lazy to set a custom title
 
Industry Role:
Join Date: May 2004
Location: West Coast, Canada.
Posts: 10,217
Quote:
Originally Posted by eMonk View Post
Sorry one more question about this....

I'm trying to display to results by RAND() and have added the following to the top of the code:

Code:
session_start();

$rand = $_SESSION['rand'];
if (empty($rand)) {
  srand((float)microtime()*1000000);
  $rand = "0.".rand();
  $_SESSION['rand'] = $rand;
}
Which works but when you click on page 2, etc it gets randomized again.

How can I have it so when you refresh the page or revisit it randomizes the results but doesn't again when you click on page 2, page 3, etc?

I tried adding in the following in a few places but doesn't seem to do anything:

Code:
if(isset($_SESSION['rand']))
    unset($_SESSION['rand']);
Also updated the $data_p query and now it looks like this:

Code:
$data_p = "SELECT * FROM model WHERE status = 'Active' ORDER BY RAND($rand) $max";
Not sure why that's not working.. Sessions can be a pain in the ass sometimes... Keep in mind that session stuff has to occur before anything else gets output since it uses cookies.... Cleaned things up a little and this "should" work.

Code:
$page = (isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);

$page = ($page < 1 ? 1 : $page);

session_start();

$rand = (isset($_SESSION['rand']) ? intval($_SESSION['rand']) : 0);

if( $rand < 1 || $page < 2 ){

	$rand = mt_rand(100000, 999999);

	$_SESSION['rand'] = $rand;
}

session_write_close();
Without using sessions, you could just pass the rand parameter in the url along with the page number. At least that way you could then know for sure it's there or not.
Tempest is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote