Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

grab featured links

Quote Reply
grab featured links
I'm going to bump this up to its own thread.

As you can see, if you install, and try this code, it's very flexible, can be called in many ways, and do different things.

It's got much room for improvement (such as picking random featured link), etc...

Code:
sub grab_featured_link { ## expects a CategoryID number only
# --------------------------------------------------------
#
my $cat_id = shift; # Contains a vallid categoryID so we are not going to check it.
my $LINKDB = new Links::DBSQL $LINKS{admin_root_path} . "/defs/Links.def";
my $DEBUG_GRAB=0;
print "Content-type: text/html\n\n" if ($DEBUG_GRAB);
my ($cat, $cat_r, $cat_ID, $get_link, $f_link, $output, $offset, $count);
$output = '';

## $get_link = $LINKDB->prepare (" SELECT * FROM Links WHERE CategoryID = ? AND is_Featured = 'Yes' LIMIT 1 "); ## prepare the select statement
$get_link = $LINKDB->prepare (" SELECT * FROM Links WHERE CategoryID = ? AND Priority = 'Yes' LIMIT 1 "); ## prepare the select statement
$get_link->execute ($cat_id); ## execute the select statement to find a featured link.
$f_link = $get_link->fetchrow_hashref || (); ## fetch the returned row, or assign an empty list

if (!$f_link) { ## if there are no featured links, pick a random one
print "There are no featured links <BR>\n" if ($DEBUG_GRAB);
print "The category ID we are checking is ...", $cat_id, "<BR>\n" if ($DEBUG_GRAB);
$get_link = $LINKDB->prepare ( "SELECT COUNT(*) FROM Links WHERE CategoryID = $cat_id"); ## gets all the links
$get_link->execute() or die $DBI::errstr;
$count = $get_link->fetchrow_array;
if ($count) {
print "COUNT IS .... ", $count,"<BR>\n" if ($DEBUG_GRAB);
($count == 1) ? $offset=0 : $offset = int rand ($count - 1) ;
print "The ofset value is .... ", $offset, "<BR>\n" if ($DEBUG_GRAB);
$get_link = $LINKDB->prepare ( " SELECT * FROM Links Where CategoryID = $cat_id LIMIT $offset, 1 ");
$get_link->execute() or die $DBI::errstr;
$f_link = $get_link->fetchrow_hashref;
print "F_LINK is...:", $f_link, "<BR>\n" if ($DEBUG_GRAB);
}
}
$cat = get_category_name($cat_id) ;
## $output .= "Today's Featured Link in <B>$cat</B> is: <BR>\n" if ($f_link); ## debugging flag, mainly
$output .= "Today's Featured Link is:\n" if ($f_link); ## debugging flag, mainly
($f_link) ? $output .= &site_html_link($f_link) : $output .= ''; ## if there are no links at all

return $output;
}
You add that to HTML_Templates.pm just after the print_cat routines (since it's only called from there), and you call it by adding the following line after the call to &load_templates

defined $dynamic and $output .= &grab_featured_link ($cat_r->{ID}); ### testing, only with page.cgi though


Quite possibly the most flexible -- and full of potential -- piece of code I've written in a while <G> ... and it's so short! ;)



PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ