Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Directory global (lists all categories)

Quote Reply
Directory global (lists all categories)
Hello,

I am trying to bulid a global that will return a list of categories in alphbetical order. I have taken a global I have found in the forum and modified it, but I am stuck on a few things.
I would like the list to be displayed in 3 columns and would like each new letter to have a header like this:

A
aardvark (2)
alamo (6)
aspen (1)

B
Batter (2)
Button (4)
botox (5)

C
Cat (6)
cyber (1)

Here is what I got so far (it's not much), it displays all the categories in one long list:

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

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

while ($row = $sth->fetchrow_hashref) {
$output .= qq|<br><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}\n|;
}

$output .= qq|</a>|;

return $output;

}



If anyone could help me I would be grateful

Thanks

George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
sub {
my ($db,$sth,$row);
my $output="";

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


for ($i = 0; $i <27, $i++;){
while ($row = $sth->fetchrow_hashref) {
$output .= qq|<br><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}\n|;

$num = 65 + $i;
if (substr($output, 0, 1) == chr($num)){print)
}}

$output .= qq|</a>|;

return $output;

}



something like that. i think.
Quote Reply
Re: [tpl] Directory global (lists all categories) In reply to
of course it isn't very efficient. but if you're only running it once... or you could do

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

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


while ($row = $sth->fetchrow_hashref) {
$output .= qq|<br><a href="$CFG->{build_root_url}/$row->{ID}">$row->{Name}\n|;

$letter = substr($output, 0, 1);

if ($letter ne $oldletter){print "$letter");

print cat

$oldletter = $letter;

}

$output .= qq|</a>|;

tat's a lot faster

return $output;

}
Quote Reply
Re: [tpl] Directory global (lists all categories) In reply to
Or you could just do:

<%set cat_loop = some_global%> (I think you can do that...if not...oops)

some_global:

Code:
sub { $DB->table('Category')->select->fetchall_arrayref({}) }

Then cat_loop is a template loop of all categories meaning you can customize the layout pretty much how you like.

Last edited by:

Paul: Jul 22, 2002, 7:52 AM
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
Hi,

None of these actually worked Unsure. Any other help would be appriciated.

George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
I did put together a small piece of code. I hope it will work.
I had no time to try it out, so there may syntax error in it, but the main logic should be good. If not it should be a good base to start.

Code:
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'] );

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

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

The link count is missing from the generated list, but this is an easy task, even you can do it. If not, others will help you.

Good luck!

BTW: let me know how it worked.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jul 23, 2002, 11:18 AM
Quote Reply
Re: [webmaster33] Directory global (lists all categories) In reply to
Thanks for working on this.
I got the following error:

A fatal error has occured:

GT::SQL::Table (16888): Unknown method 'hits' called at (eval 13) line 10.

Thanks
George

Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
Wink change $sth->hits to $sth->rows
Quote Reply
Re: [Paul] Directory global (lists all categories) In reply to
Eh. Khm. I told there will be syntax error for first time Wink
Yep. Thanks, Paul.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Directory global (lists all categories) In reply to
Thanks Webmaster33 & Paul

It seems to be working with only a few minor problems: Only 1 column is output and the list is broken down by CAPS and lowercase letters. Here is what I am getting as output:

0

00cat

1

12cat
13cat

4

41cat
42cat

8

81cat

A

A1cat
A2cat

a

a3cat

A

A4cat
A5cat
A6cat

a

a7cat

A

A8cat
A9cat

B

B1cat
B2cat

b

b3cat

B

B4cat


How would I get it to look like this?:

0-9

00cat
12cat
13cat
41cat
42cat
81cat

A

A1cat
A2cat
a3cat
A4cat
A5cat
A6cat
a7cat
A8cat
A9cat

B

B1cat
B2cat
b3cat
B4cat


Thanks for all of your help,

George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
This code do this:
- now the group letters are big
- both small and capital letters are listed in same group
- I did not corrected the bug, which causes not split into 3 columns
(I should execute the script to correct it. You have to correct this.)
- it is not possible so easily to merge the groups 0-9 into one. It is only possible if you do an exception, and you treat numbers separately.

Try this:
Code:
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'] );

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 $table = qq|<table><tr><td width="$width%" valign="top">\n|;

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

You got the starting steps help, so I think you should finish yourself.
If you still have problems, post it here, and you may get the help.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Directory global (lists all categories) In reply to
Thanks,

I ran the script and got the following output:

0

00cat
12cat
13cat
41cat
42cat
81cat
A1cat
A2cat
a3cat
A4cat
A5cat
A6cat
a7cat
A8cat
A9cat
B1cat
B2cat
b3cat

Also still in 1 column.... I will dig into this to see what I can figure out, thanks for your help.

George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
Oh sorry, I did a mistake:
$letter =~ /^$oldletter.*/i should be changed to: $letter !~ /^$oldletter.*/i

Code:
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'] );

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 $table = qq|<table><tr><td width="$width%" valign="top">\n|;

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

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Directory global (lists all categories) In reply to
Thanks,

Still the same results, but without the 0 this time.

George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
Working on. Will work soon.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
This will work. I tested it.

Code:
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"] );

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 border=0><tr><td width="$width%" valign="bottom"><tr><td>\n|;

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

The problem was with the the table and the regexp.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jul 24, 2002, 7:35 AM
Quote Reply
Re: [webmaster33] Directory global (lists all categories) In reply to
WOW! Works like a charm! You are the best!


Thanks!
George
Quote Reply
Re: [macbethgr] Directory global (lists all categories) In reply to
I'm not sure the html is valid :)