Jump to content

Rand() with a seed.


Recommended Posts

Hi There.

 

I am trying to modify a component that was built for Joomla and having a little bit of trouble. Currently I am able to get the rand() function working but since there are pages the listings will double up when flipping to the next page. So if I place a number in the rand for example rand(3), pagination works but isn't truely random since it loads the same order everytime. After a search here, I had heard about making a seed using ip+hour+day+month and using that in the rand function but I currently get an error in another file that I am not sure on how to correct. Any help will be appreciated.

 

Here is the code that works.

 		
# Retrieve Links
$sql = "SELECT l.*, cl.*, cat.*, u.username AS username, u.name AS owner, img.filename AS link_image FROM #__mt_links AS l"
. "\n LEFT JOIN #__mt_cl AS cl ON cl.link_id = l.link_id "
. "\n LEFT JOIN #__users AS u ON u.id = l.user_id "
. "\n LEFT JOIN #__mt_cats AS cat ON cl.cat_id = cat.cat_id "
. "\n LEFT JOIN #__mt_images AS img ON img.link_id = l.link_id AND img.ordering = 1 "
. "\n WHERE link_published='1' && link_approved='1' && cl.cat_id='".$cat_id."' "
. "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now' ) "
. "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down >= '$now' ) "
. "\n ORDER BY " . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', rand()';


// if( $mtconf->get('min_votes_to_show_rating') > 0 && $mtconf->get('first_listing_order1') == 'link_rating' ) {
// $sql .= "\n ORDER BY link_votes >= " . $mtconf->get('min_votes_to_show_rating') . ' DESC, ' . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', ' . $mtconf->get('second_listing_order1') . ' ' . $mtconf->get('second_listing_order2');
// } else {
// original $sql .= "\n ORDER BY " . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', ' . $mtconf->get('second_listing_order1') . ' ' . $mtconf->get('second_listing_order2');

// $sql .= "\n ORDER BY " . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', ' . rand();


// }

	$sql .= "\n LIMIT $limitstart, " . $mtconf->get('fe_num_of_links');
	$database->setQuery( $sql );

	$links = $database->loadObjectList();

 

I am specifically editing this line of code:

. "\n ORDER BY " . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', rand()';

 

What I have been trying to do is this:

. "\n ORDER BY " . $mtconf->get('first_listing_order1') . ' ' . $mtconf->get('first_listing_order2') . ', rand($seed)';

 

With this code above it:

//generate individual seed... 
$ip=$_SERVER['REMOTE_ADDR']; 
$hour=date("H"); 
$day=date("j"); 
$month=date("n"); 
$ip=str_replace(".","",$ip); 
$seed=($ip+$hour+$day+$month);

 

But I get a error in another file that I am not sure on how to correct. The line of code it wants me to correct is:

$fields = $this->fields[$link->link_id];

 

Here is the surrounding code:

if($this->pageNav->total > 0) {
	?>
	<div class="pages-links">
		<span class="xlistings"><?php echo $this->pageNav->getResultsCounter(); ?></span>
		<?php echo $this->pageNav->getPagesLinks(); ?>
	</div>

	<?php
	$i = 0;
	foreach ($this->links AS $link) {
		$i++;
		$fields = $this->fields[$link->link_id];
		include $this->loadTemplate('sub_listingSummary.tpl.php');
	}

	if( $this->pageNav->total > $this->pageNav->limit ) { ?>
	<div class="pages-links">
		<span class="xlistings"><?php echo $this->pageNav->getResultsCounter(); ?></span>
		<?php echo $this->pageNav->getPagesLinks(); ?>
	</div>
	<?php
	}

Link to comment
https://forums.phpfreaks.com/topic/255301-rand-with-a-seed/
Share on other sites

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.