Gossamer Forum
Quote Reply
Category List
I am trying to build a 3 coloumn list of all categories listed alphabetically. I was building my categories by ID and the following global worked fine, I am now building based on categories and need some help with the output....

sub {
my ($db, $sth, $row, $oldletter, $letter, $i);
my $output="";

$db = $DB->table("Category");
$db->select_options("ORDER BY Name");
$sth = $db->select( ["ID","Name","sm_graphic"] );

my $cols = 3;
my $row_count = $sth->rows;
my $breakpoint = int (($row_count) / $cols) + ( (($row_count) % $cols) ? 1 : 0);
my $width = int (100 / $cols);
my $output = qq|<table width="100%" border=0><tr><td width="$width%" valign="top"><tr><td valign="top">\n|;

while ($row = $sth->fetchrow_hashref) {
$letter = uc(substr($row->{Name}, 0, 1));
if ($letter !~ /$oldletter/i ){
$output .= qq|<P><font face="Arial"><b><a name="$letter">$letter</a></b></font></P>\n|;
$oldletter = $letter;
}
$output .= qq|<font face="Arial" size="2"><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}</a></font><br>\n|;
($i > 0) and !($i % $breakpoint) and ($output .= qq|</td>\n<td valign="top" width="$width%">\n|);
$i++;
}
$output .= "</td></tr></table></font>\n";
return $output;
}


Thanks....
George
Quote Reply
Re: [macbethgr] Category List In reply to
What problems are you having?

- Jonathan
Quote Reply
Re: [jdgamble] Category List In reply to
Well, I was building based on category ID number, so the url includes the category ID number (http://root/67/index.html).... I am no longer building based on the category ID number, so I need the url to return the proper path(http://root/...gory_Name/index.html), rather than the category ID number.... Since the global includes other functions that I need, I would like some help to figure out how to do that...

George
Quote Reply
Re: [macbethgr] Category List In reply to
try changing
Code:

$output .= qq|<font face="Arial" size="2"><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}</a></font><br>\n|;

to this:
Code:

my $name = $row->{Full_Name};
$name = s, ,_,g;
$output .= qq|<font face="Arial" size="2"><a href="$CFG->{build_root_url}/$name">$row->{Name}</a></font><br>\n|;


More, More, More...

- Jonathan
Quote Reply
Re: [jdgamble] Category List In reply to
Thanks, I gave it a try, but it only returned the root category (http://root)
George
Quote Reply
Re: [macbethgr] Category List In reply to
I tested it, and this function does what you need (All I did is delete the graphic part)...

Code:

my ($db, $sth, $row, $oldletter, $letter, $i);
my $output;
$db = $DB->table("Category");
$db->select_options("ORDER BY Name");
$sth = $db->select( ["ID","Name"] );
my $cols = 3;
my $row_count = $sth->rows;
my $breakpoint = int (($row_count) / $cols) + ( (($row_count) % $cols) ? 1 : 0);
my $width = int (100 / $cols);
my $output = qq|<table width="100%" border=0><tr><td width="$width%" valign="top"><tr><td valign="top">\n|;
while ($row = $sth->fetchrow_hashref) {
$letter = uc(substr($row->{Name}, 0, 1));
if ($letter !~ /$oldletter/i ){
$output .= qq|<P><font face="Arial"><b><a name="$letter">$letter</a></b></font></P>\n|;
$oldletter = $letter;
}
$output .= qq|<font face="Arial" size="2"><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}</a></font><br>\n|;
($i > 0) and !($i % $breakpoint) and ($output .= qq|</td>\n<td valign="top" width="$width%">\n|);
$i++;
}
$output .= "</td></tr></table></font>\n";


May your day be merry,

- Jonathan
Quote Reply
Re: [jdgamble] Category List In reply to
Thanks for all your help, but I dont think you understand what I am trying to do.... I do not want the category ID to be returned as part of the url since I am not building my directory that way. I want the full url of each category to be returned. If I were building the directory using category ID, then the script worked fine to begin with, but that type of build is not search engine friendly.
George
Quote Reply
Re: [macbethgr] Category List In reply to
I'm sorry, I pasted the original code instead of the changed one (wheeww)

try this:

Code:

my ($db, $sth, $row, $oldletter, $letter, $i);
my $output;
$db = $DB->table("Category");
$db->select_options("ORDER BY Name");
$sth = $db->select( ["ID","Name", "Full_Name"] );
my $cols = 3;
my $row_count = $sth->rows;
my $breakpoint = int (($row_count) / $cols) + ( (($row_count) % $cols) ? 1 : 0);
my $width = int (100 / $cols);
my $output = qq|<table width="100%" border=0><tr><td width="$width%" valign="top"><tr><td valign="top">\n|;
while ($row = $sth->fetchrow_hashref) {
$letter = uc(substr($row->{Name}, 0, 1));
if ($letter !~ /$oldletter/i ){
$output .= qq|<P><font face="Arial"><b><a name="$letter">$letter</a></b></font></P>\n|;
$oldletter = $letter;
}
my $name = $row->{Full_Name};
$name =~ s, ,_,g;
$output .= qq|<font face="Arial" size="2"><a href="$CFG->{build_root_url}/$name">$row->{Name}</a></font><br>\n|;
($i > 0) and !($i % $breakpoint) and ($output .= qq|</td>\n<td valign="top" width="$width%">\n|);
$i++;
}
$output .= "</td></tr></table></font>\n";


Hope this helps more than that last one did Wink,

- Jonathan
Quote Reply
Re: [jdgamble] Category List In reply to
Thanks, that worked.... I only had to make a minor change so that "." and "&" were changed to "_"
I changed this line:
$name =~ s, ,_,g;
to:
$name =~ s/[^\w\d_\-\/]/_/g;

Thanks again,
George
Quote Reply
Re: [macbethgr] Category List In reply to
I don't know why I didn't think of this before, but instead of:

Code:

my $name = $row->{Full_Name};
$name =~ s, ,_,g;


it should be:
Code:

my $name = $db->as_url($row->{Full_Name});


Sorry about that...

- Jonathan