Ninjalinks: Newest and Random Links

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.

Displaying The Newest Link/A Random Link

One of the features the craptastic SimpleDir (which NinjaLinks is supposed to replace) had was the ability to display the newest link, or a random link. NinjaLinks has this capability, too – if you do a little tweaking to functions.php.

Let’s say that I want my index page to be laid out like the following:

Newest Link: Newest Link
Random Link: Random Link

To do that on my index page index.php would have something like this in it:

Newest Link: <?php getNewestLink(1); ?>
Random Link: <?php getRandomLink(1); ?>

Unfortunately without a function called getNewestLink or getRandomLink all we get is an error. Therefore we need to create a pair of functions. I based mine on the code that was already in the script. So I open up functions.php and insert the following:

function getNewestLink($limit) {
	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";
	$buildQuery .= " ORDER BY 'dateadded' DESC";

	$links = $mysql->query($buildQuery." LIMIT ".$limit);
	while ($l = mysql_fetch_assoc($links)) {
		echo '<a style="font-size: 1.1em;" href="click.php?link='.$l['id'].'"';
		if ($opt['opentarget'] == 1) echo ' target="_blank"';
		if ($opt['nofollow'] == 1) echo ' rel="nofollow"';
		echo '>';
		echo $l['linkname'];
		echo '</a> ('.$l['hits'].' hits)</em>';
	}
}
function getRandomLink($limit) {
	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";
	$buildQuery .= " ORDER BY rand() DESC";

	$links = $mysql->query($buildQuery." LIMIT ".$limit);
	while ($l = mysql_fetch_assoc($links)) {
		echo '<a style="font-size: 1.1em;" href="click.php?link='.$l['id'].'"';
		if ($opt['opentarget'] == 1) echo ' target="_blank"';
		if ($opt['nofollow'] == 1) echo ' rel="nofollow"';
		echo '>';
		echo $l['linkname'];
		echo '</a>('.$l['hits'].' hits)';
	}
}

The first creates a link for the newest addition to the directory, while the second link is the one for a random link. And now with those two functions you can display your latest link and a random link.


Content and design © Catherine Haines. Powered by Wordpress and hosted by Site5.