rand() with a seed.

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.
[php]

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();
[/php]

I am specifically editing this line of code:
[php]
. "\n ORDER BY " . $mtconf->get(‘first_listing_order1’) . ’ ’ . $mtconf->get(‘first_listing_order2’) . ‘, rand()’;
[/php]

What I have been trying to do is this:
[php]
. "\n ORDER BY " . $mtconf->get(‘first_listing_order1’) . ’ ’ . $mtconf->get(‘first_listing_order2’) . ‘, rand($seed)’;
[/php]

With this code above it:
[php]
//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);
[/php]

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:
[php]$fields = $this->fields[$link->link_id];[/php]

Here is the surrounding code:
[php]if($this->pageNav->total > 0) {
?>


<?php echo $this->pageNav->getResultsCounter(); ?>
<?php echo $this->pageNav->getPagesLinks(); ?>

<?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 ) { ?>

<?php echo $this->pageNav->getResultsCounter(); ?>
<?php echo $this->pageNav->getPagesLinks(); ?>

<?php
}[/php]

Thanks for any help you can give me.

You have to put borders for your random number :slight_smile:
When i was builing a register form i used this:

[php]
$random = rand(23456789,98765432);
[/php]
:wink:

I tried this but unfortunately it didn’t work. When I try to put borders in the line of code it errors out on this line:
[php]$fields = $this->fields[$link->link_id];[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service