Ninjalinks: Change Link Display Order
NinjaLinks is a free link directory script made by Jem as a replacement for the unsafe SimpleDir script. I use it on my site The Disney Directory, but with a few minor adjustments here and there.
None of the stuff I am about to show you is anything amazing, but for those whose knowledge of PHP is limited to includes and “I know how to install NinjaLinks” it might come in handy.
Change Link Display Order
The default means of displaying links in your directory or listing is in the order they were added, with the newest links at the top. You might like it just fine, while others might want something else. Over at TDR I changed it so links were displayed in alphabetical order, with links beginning with A displayed at the top.
So how do we do this? In your text editor open up functions.php and search for function getLinks (around line 289, if nothing has been added or subtracted). The first part of this function, and the one we will need to look at here, should look something like this:
function getLinks($offset, $limit, $category) {
global $mysql, $opt, $dbpref;
$buildQuery = "SELECT `".$dbpref."links`.*, `".$dbpref."categories`.`catname` FROM `".$dbpref."links` LEFT JOIN `".$dbpref."categories` ON `".$dbpref."links`.`category` = `".$dbpref."categories`.`id` WHERE `".$dbpref."links`.`approved` = 1";
if ($category != "all") $buildQuery .= " AND `category` = ".$category;
$buildQuery .= " ORDER BY `dateadded` DESC";
The line that is of interest to us, however is this one:
$buildQuery .= " ORDER BY `dateadded` DESC";
When you submit a link to NinjaLinks, it sends various pieces of information to the database. dateadded is merely one that identifies when the link was added (so simple, right?). The name of the aspect of information we want to base the order on is linkname which – yep, you guessed it – focuses on the name of the link. So if we replace dateadded with linkname our code now should look like this:
$buildQuery .= " ORDER BY `linkname` DESC";
Success! Kind of, anyway. The links are now displayed in alphabetical order, but with Z at the top. So what do we do now? Simple! All we have to do is change the instructions from DESC (for descending) to ASC (for ascending). Your code should look something like this:
function getLinks($offset, $limit, $category) {
global $mysql, $opt, $dbpref;
$buildQuery = "SELECT `".$dbpref."links`.*, `".$dbpref."categories`.`catname` FROM `".$dbpref."links` LEFT JOIN `".$dbpref."categories` ON `".$dbpref."links`.`category` = `".$dbpref."categories`.`id` WHERE `".$dbpref."links`.`approved` = 1";
if ($category != "all") $buildQuery .= " AND `category` = ".$category;
$buildQuery .= " ORDER BY `linkname` ASC";
And there you have it. Your links, in their specific category, are now displayed alphabetically with A at the top and Z at the bottom.
Just note that making the links alphabetical messes up the “newest links” section on your index page. However that is simple to fix: create a new function, say, getIndexLinks, with the exact same functions as getLinks was originally. Change the function on your index page, and you’re all set to go.