PDA

View Full Version : Random Sort


Kevin Songhurst
12-18-2006, 05:16 PM
Can Random sort please be added to the site so that customers whose name start with w do not always end up at the bottom of the page and it will stop companies calling themselves a1plumbing or something similar, everyone gets a chance at being top.

Thanks in advance

Bryan Ex
12-18-2006, 07:18 PM
I would love to have a random sort option as well!

Hey Dave Baker... you busy these days? :p

John Turner
12-19-2006, 08:01 AM
Hi there,

Here one variant.

1. Open '../index.php'
*** find following code

$links =& $gDirDb->getLinksByCategory($category['id'], $start, $gDirConfig['num_index_links'], $featured_top, $sponsored_top, $gDirEditor['id']);


*** replace it with following code

$random = $_GET['order'] ? FALSE : TRUE;
$links =& $gDirDb->getLinksByCategory($category['id'], $start, $gDirConfig['num_index_links'], $featured_top, $sponsored_top, $gDirEditor['id'], $random);


*** Save and close it

2. Open '../classes/Dir.php'
*** find following code

function getLinksByCategory($aCategory = 0, $aStart = 0, $aLimit = 0, $aFeatured = TRUE, $aSponsored = FALSE, $aEditor = '')


*** replace it with following code

function getLinksByCategory($aCategory = 0, $aStart = 0, $aLimit = 0, $aFeatured = TRUE, $aSponsored = FALSE, $aEditor = '', $aRandom = FALSE)


*** find following code

$sql .= 'ORDER BY ';
if ($aFeatured)
{
// Featured go first
$sql .= '`t1`.`featured` DESC, ';
}
if ($aSponsored)
{
$sql .= '`t1`.`sponsored` DESC, ';
}
// Default order
$order = 'alphabetic' == $gDirConfig['links_sorting'] ? 'title' : $gDirConfig['links_sorting'];
$order_type = 'ascending' == $gDirConfig['links_sorting_type'] ? 'ASC' : 'DESC';
$sql .= '`t1`.`'.$order.'` '.$order_type.' ';
$sql .= $aLimit ? 'LIMIT '.$aStart.', '.$aLimit : '';



*** replace it with following code

if ($aFeatured || $aSponsored)
{
$sql .= 'ORDER BY ';
if ($aFeatured)
{
// Featured go first
$sql .= '`t1`.`featured` DESC, ';
}
if ($aSponsored)
{
$sql .= '`t1`.`sponsored` DESC, ';
}
}
if(!$aRandom)
{
// Default order
$order = 'alphabetic' == $gDirConfig['links_sorting'] ? 'title' : $gDirConfig['links_sorting'];
$order_type = 'ascending' == $gDirConfig['links_sorting_type'] ? 'ASC' : 'DESC';
$sql .= '`t1`.`'.$order.'` '.$order_type.' ';
}
$sql .= $aLimit ? 'LIMIT '.$aStart.', '.$aLimit : '';



*** Save and close it.

It might helps.

Kevin Songhurst
12-19-2006, 12:15 PM
Hi
tried all that but still no luck.

Dave Baker
12-19-2006, 07:35 PM
Modification for versions Pro1.2 and Pro2.0
For Pro 1.2 version
Admin Panel -> CONFIGS -> Page Navigation and Listings ->Allow link ordering for visitors->enabled->Save Changes
***Open table yourPrefix_config in your database and find row with name "links_sorting", in my database it has id="45". Edit this row, replace value multiple_values with the following:

'alphabetic','date','rank','clicks','rand'

***Save Changes***
***Open languages/yourLanguage.php and add text:***

'rand' => 'Rand',

***Save file***
***Open file classes/Dir.php and find function***

function getLinksByCategory($aCategory = 0, $aStart = 0, $aLimit = 0, $aFeatured = TRUE, $aSponsored = FALSE, $aEditor = '')

***In this function find code (This code will meet in three places of this function)***

$order = ($gDirConfig['links_sorting'] == 'alphabetic') ? 'title' : $gDirConfig['links_sorting'];
$order_type = ($gDirConfig['links_sorting_type'] == 'ascending') ? 'ASC' : 'DESC';
$sql .= "ORDER BY `{$order}` {$order_type} LIMIT 0, 10000) ";

***Replace this code in each case with the following code***

$order = ($gDirConfig['links_sorting'] == 'alphabetic') ? 'title' : $gDirConfig['links_sorting'];
$order = ($order == 'rand') ? "RAND()" : "`".$order."`";
$order_type = ($gDirConfig['links_sorting_type'] == 'ascending') ? 'ASC' : 'DESC';
$sql .= "ORDER BY {$order} {$order_type} LIMIT 0, 10000) ";

***Save file***

For Pro2.0 version
***Open table yourPrefix_config in your database and find row with name "links_sorting", in my database it has id="45". Edit this row, replace value multiple_values with the following:

'alphabetic','date','rank','clicks','rand'

***Save Changes***
Admin Panel » Language Manager » Add Phrase
Key: rand
Value: Rand
Category: Common
***Save***
***Open classes/Dir.php and find function***

function getLinksByCategory($aCategory = 0, $aStart = 0, $aLimit = 0, $aFeatured = TRUE, $aSponsored = FALSE, $aEditor = '')

***In this function find code***

// Default order
$order = 'alphabetic' == $gDirConfig['links_sorting'] ? 'title' : $gDirConfig['links_sorting'];
$order_type = 'ascending' == $gDirConfig['links_sorting_type'] ? 'ASC' : 'DESC';
$sql .= '`t1`.`'.$order.'` '.$order_type.' ';

***Replace with the following code***

// Default order
$order = 'alphabetic' == $gDirConfig['links_sorting'] ? 'title' : $gDirConfig['links_sorting'];
$order = ($order == 'rand') ? "RAND() " : '`t1`.`'.$order.'` ';
$order_type = 'ascending' == $gDirConfig['links_sorting_type'] ? 'ASC' : 'DESC';
$sql .= $order.$order_type.' ';

***Save file***
You can set 'rand' as default here: Admin Panel » Configuration » Page Navigation and Listings » Links order by » rand

Bryan Ex
12-19-2006, 08:10 PM
Perfect! Installing that one next for sure Dave...

Dave Baker
12-20-2006, 12:59 AM
Bryan :good:

Kevin Songhurst
12-20-2006, 01:06 PM
Works great, thanks very much
Kevin

Bryan Ex
12-26-2006, 08:09 PM
This is very close to being perfect Dave but there is a small problem. The random sort order is recalculated for each page which means some links appear more than once (sometimes on every page) and others never get listed. If I sort by clicks for example, with 30 links in a category and 10 links per page the first 10 results are displayed on page 1. Results 11 ~ 20 are on page 2 and 21 ~ 30 on page 3. With random sort the each page is being recalculated instead of 1 ~ 10 , 11 ~20, and 21 ~30. Is there a way to cache the random array so that the random order is only calculated once when a user browses a category?

Not sure if I explained that very well so if you don't understand I will try again. :ooi:

Dave Baker
12-29-2006, 07:48 PM
Bryan you have perfectly stated the problem and it does not demand additional explanations.
The problem consists in that the script execute query at transition to each page, a method of random event is organized by means of the following structure of Sql language.

ORDER BY RAND() LIMIT $aLimit

The query with this structure chooses certain amount of links($aLimit) from the certain category.
In order to take data from the table once and pass to each page using only them will be necessary to keep them. Any saving of these data will result in deceleration of script work.
I am not confident I can find a decision for you but I will try.

Bryan Ex
12-29-2006, 09:33 PM
Don't worry about it Dave. It would be nice but not necessary. I will just uninstall this mod for now and set default sort order back to date.