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

Category Menu Everywhere

(Page 1 of 2)
> >
Quote Reply
Category Menu Everywhere
Hello all,

The mod below is for Links 2.0, but how to convert the mod for using with Links SQL? Please help.






This will give a list/menu of top level categories anywhere on your templates:

sub menu {
# --------------------------------------------------------
#
my (%c, @fields);
open (DB, "<$db_category_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
@fields = &split_decode ($_);

$c{$fields[$db_main_category]}++;
}
close DB;

foreach $field (sort keys %c) {

if ($field =~ m,^([^/]*)$,) {
$field2 = &build_clean($field);
$category_list .= qq|<a href= "$build_root_url/$field">$field2</a>
|;
}
}

return $category_list;
}


Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
I actually did this, but of course, can't find which site I did it on in my back ups here.

To quote Alex, from my SQL notes file, though:

First, you need a category table object:
my $table = $DB->table('Category');
Then, to get root level categories:
my $sth = $table->select( { FatherID => 0 }, ['ID', 'Full_Name'] );
This will only get the ID and Full_Name fields, you should avoid SELECT *'s when you don't need them.


Then, simply iterate through them, formatting them as you want, and return the tag.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Category Menu Everywhere In reply to
Surely you don't use a statement handle with a select...one or t'other Wink

Last edited by:

RedRum: Dec 31, 2001, 8:49 AM
Quote Reply
Re: [RedRum] Category Menu Everywhere In reply to
I'm not sure what you mean...

$DB is the Links global to the MySQL DBI database handle object.

You use it to connect and get a table handle:

$db = $DB->table ('table_name')

$db is now the table pointer, and used to do selects, queries, adds, etc to a specific table in the database $DB

Once you get your $db table handle, you can:

$db->select_options ()

then you need to do a

$sth = $db->select

to execute the select, and put the results into a statement handle. the $db is a table object, not a statement handle.

To see if there were any hits, you would do:

$db->hits (returns the total hits on the last query without any Limit statement restrictions ie: total database matches)

Then, you could start iterating through the results, using the STATEMENT HANDLE :

while (my $row = $sth->fetchrow_hashref) {
..... stuff ......
}

Where $row is a reference to the returned hash of values (same as $rec in the other scripts)

You need to do the

if ($db->hits) {
while (my $row = $sth->fetchrow_hashref){
.... stuff ....
}
}

Construct, in order to avoid undefined value errors trying to use a null $sth.

Or did I miss the point of your post??












PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Category Menu Everywhere In reply to
Quote:
then you need to do a

$sth = $db->select

Thats the bit Im referring to. But you have it right.

Its when you include something like ->fetchrow_hashref that you don't use $sth ...eg this is wrong:

my $sth = $table->select( { FatherID => 0 }, ['ID', 'Full_Name'] )->fetchrow;

....but this is right:

my @rec = $table->select( { ID => 1 }, ['ID', 'Full_Name'] )->fetchrow;

....and this is wrong:

my @rec = $table->select( { ID => 1 }, ['ID', 'Full_Name'] );

...but this is right:

my $sth = $table->select( { FatherID => 0 }, ['ID', 'Full_Name'] );




Last edited by:

RedRum: Dec 31, 2001, 10:02 AM
Quote Reply
Re: [RedRum] Category Menu Everywhere In reply to
Hi,

Errr....sorry ...i really dont understand what kind of solution Paul and Putdog try to do with the first thread, i'm really sorry cause i'm very new with Links SQL and the code for me are so different with Links 2.0.

So sorry and please help.
Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Hi,

Add the following to your globals:

menu =>
Code:
sub {
my $tags = shift;
my $cat_db = $DB->table('Category');
my @root_cats = $cat_db->select (['Full_Name'], { FatherID => 0 })->fetchall_list;
my $output;
foreach my $root_cat (@root_cats) {
my $url = $cat_db->as_url($root_cat);
$output .= qq~<a href="$url">$root_cat</a>~;
}
return $output;
}
You may want to format it a bit differently as this is a little ugly (just change the $output .= line).

Cheers,

Alex

--
Gossamer Threads Inc.

Last edited by:

Alex: Dec 31, 2001, 12:04 PM
Quote Reply
Re: [Alex] Category Menu Everywhere In reply to
Thank you so much Alex Laugh
Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Hi Alex,

I got this error message:

Code:
Stack Trace
======================================
Links (20467): Links::environment called at /home/worldbar/public_html/db/cgi-bin/admin/Links.pm line 444 with no arguments.

Links (20467): Links::fatal called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Config.pm line 434 with arguments

(GT::Config (20467): Unable to compile 'menu' in file '/home/worldbar/public_html/db/cgi-bin/admin/templates/default/globals.txt': Global symbol "$name" requires explicit package name at (eval 25) line 7.
at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 436.
).

Links (20467): GT::Config::error called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Config.pm line 488 with arguments

(GT::Config, CANT_COMPILE_CODE, FATAL, menu, /home/worldbar/public_html/db/cgi-bin/admin/templates/default/globals.txt, Global symbol "$name" requires explicit package name at (eval 25) line 7.
).

Links (20467): GT::Config::FETCH called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 436 with arguments

(GT::Config=HASH(0x8253830), menu).

Links (20467): GT::Template::load_vars called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 70 with arguments

(GT::Template=HASH(0x85732a4), GT::CGI=HASH(0x82537d0), GT::Config=HASH(0x8253800), HASH(0x8570530)).

Links (20467): GT::Template::parse called at /home/worldbar/public_html/db/cgi-bin/admin/Links.pm line 317 with arguments

(GT::Template, subcategory.html, ARRAY(0x8571bb0), HASH(0x8571b74)).

Links (20467): Links::user_page called at /home/worldbar/public_html/db/cgi-bin/admin/Links/SiteHTML.pm line 177 with arguments

(subcategory.html, HASH(0x8570530), HASH(0x8571b74)).

Links (20467): Links::SiteHTML::site_html_print_cat called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Plugins.pm line 102 with arguments

(ARRAY(0x84db798), [undef]).

Links (20467): GT::Plugins::dispatch called at /home/worldbar/public_html/db/cgi-bin/admin/Links/SiteHTML.pm line 28 with arguments

(GT::Plugins, /home/worldbar/public_html/db/cgi-bin/admin/Plugins, site_html_print_cat, *Links::SiteHTML::site_html_print_cat, ARRAY(0x84db798), [undef]).

Links (20467): Links::SiteHTML::display called at Links::Build::build_home line 16 with arguments

(print_cat, ARRAY(0x84db798)).

Links (20467): Links::Build::build_home called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Plugins.pm line 102 with arguments

(HASH(0x840f260)).

Links (20467): GT::Plugins::dispatch called at /home/worldbar/public_html/db/cgi-bin/admin/Links/Build.pm line 30 with arguments

(GT::Plugins, /home/worldbar/public_html/db/cgi-bin/admin/Plugins, build_home, *Links::Build::build_home, HASH(0x840f260)).

Links (20467): Links::Build::build called at /home/worldbar/public_html/db/cgi-bin/admin/Links/User/Page.pm line 77 with arguments

(home, HASH(0x840f260)).

Links (20467): Links::User::Page::generate_home_page called at /home/worldbar/public_html/db/cgi-bin/admin/Links/User/Page.pm line 53 with no arguments.

Links (20467): Links::User::Page::handle called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Plugins.pm line 102 with no arguments.

Links (20467): GT::Plugins::dispatch called at page.cgi line 22 with arguments

(GT::Plugins, /home/worldbar/public_html/db/cgi-bin/admin/Plugins, handle_page, CODE(0x8227178)).

Please help.

Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Try replacing $name with $root_cat


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Category Menu Everywhere In reply to
Heh. just did that. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Category Menu Everywhere In reply to
Hmm next to your post it says 8.05pm but my system clock says 19.57pm

How is it working it out?
Quote Reply
Re: [RedRum] Category Menu Everywhere In reply to
Thanks to all, its work now, btw, why in my ' Setup --> Environment' still show the message as below? Is it natural or something problem with Link SQL ? Another thing is, i cant delete any 'Code' by tick
the delete tick bock at 'Global Template' after i save it unless i do it manually by editing the global.txt ? In other word, I can save it or adding and editing the new code, but i cant delete it. Sorry ...i'm so
new with Links SQL.

Code:
Stack Trace
======================================
Links (24140): Links::environment called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 663 with arguments
(0).
Links (24140): GT::Template::_call_func called at /home/worldbar/public_html/db/cgi-bin/admin/templates/admin/compiled/setup_env.html.compiled line 48 with arguments
(GT::Template=HASH(0x81012ec), Links::environment, 0).
Links (24140): GT::Template::__ANON__ called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 514 with arguments
(GT::Template=HASH(0x81012ec)).
Links (24140): GT::Template::_parse called at /home/worldbar/public_html/db/cgi-bin/admin/GT/Template.pm line 88 with arguments
(GT::Template=HASH(0x81012ec), setup_env.html, HASH(0x8262564)).
Links (24140): GT::Template::parse called at /home/worldbar/public_html/db/cgi-bin/admin/Links.pm line 276 with arguments
(GT::Template, setup_env.html, ARRAY(0x8262558), HASH(0x8262564)).
Links (24140): Links::admin_page called at admin.cgi line 50 with no arguments.
Links (24140): main::main called at admin.cgi line 24 with no arguments.

Last edited by:

reenee: Dec 31, 2001, 1:27 PM
Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Hi,

The stack trace is normal for Environment.

As for globals, are you using 2.1.0 beta 2? If so, thats a known bug that we are working on fixing.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Category Menu Everywhere In reply to
Ohh..ok...Thank you so much Alex.
Yes...i'm using 2.1.0 Beta 2.
Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Hello All,

I got problem with 'Plugins', i can't install any plugins unless i chmod first the cgi-bin directory to '777', if not, i will notice with the message:
Unable to install plugin: 'Unable to extract file: '/home2/user/public_html/adv/cgi-bin/admin/../detail_page.cgi' (Could not open /home2/user/public_html/adv/cgi-bin/admin/../detail_page.cgi. Reason: (Permission denied))'

How to solve it? Please help and thanks so much.


Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Hi,

You need to chmod cgi-bin 777 so that the program can create detail_page.cgi file. Once the plugin is installed, you can set it back to 755. Alternatively, you can create an empty file in cgi-bin called detail_page.cgi and chmod it 666 so the plugin can overwrite it.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Category Menu Everywhere In reply to
Hi Alex,

I did try the code and its work, but only on 'Static' page and the path is correct, but not with 'Dynamic' page and the path is not correct, Please help.

Code:
sub {
my $tags = shift;
my $cat_db = $DB->table('Category');
my @root_cats = $cat_db->select (['Full_Name'], { FatherID => 0 })->fetchall_list;
my $output;
foreach my $root_cat (@root_cats) {
my $url = $cat_db->as_url($root_cat);
$output .= qq~<a href="$url">$root_cat</a><br>~;
}
return $output;
}
Quote Reply
Re: [reenee] Category Menu Everywhere In reply to
Oops, change:

$output .= qq~<a href="$url">$root_cat</a><br>~;

to:

$output .= qq~<a href="$CFG->{build_root_url}/$url">$root_cat</a><br>~;

and it should work.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Category Menu Everywhere In reply to
Hmmmmm....

Mine came out quite niely, but it dropped the frist category....

any suggestions?

George
Quote Reply
Re: [macbethgr] Category Menu Everywhere In reply to
ooops! I found the first category, it's not sorted by alphabet..... How can I sort them by alphabet?

Also.... This global prints all categories with the "Father" value of 0.... How would I print out a menu of categories where the "Father" value is not 0..... ?

Thanks!
George
Quote Reply
Re: [macbethgr] Category Menu Everywhere In reply to
A few minor changes to this sub,and you could pass in a parameter to indicate how (what field) you wanted to sort by.

This is a chunk of code I have in my utilties file at the moment, and I hope to get it released soon, with updates to the other plug ins. This is something that goes with the top_n searches, last_n links, etc.

For those new, or who might not remember, about a year ago I started to hack heavily on the system, then about 8-10 months ago, just after making significant improvements, my life sort of bottomed out, and took a second down turn on 9/11.

Anyway, what I was doing at the time, and have been trying to reconstruct, is to add a new "utilities" group to the Plugins area, Plugins::PUGDOG, into which I've been putting my various routines and utilties. I've avoided where possible, using globals to do things, since I have to update each site manually to do that. By putting the functions into a hierarchy, and calling them as functions from inside the templates, you get the same effect, and even more features -- plus easy updating (just copy the new modules in, to all your sites.

It's the same idea PERL uses, and that Links itself uses.

Because the Plugins area is designed to be user-edited, and third-party accessible, I chose to put the modules in there, rather than in some other area, even if the routines are not really "plugins" like my Days_Old routines.

Layout is currently:

PUGDOG::Utils (general things, odds and ends, etc)
PUGDOG::Image (all my image routines, and such)
PUGDOG::Upload (all the stuff I've done working with files, this all has to be redone, moved out of "Image")


And a few others. But, basically, the idea is that my routines will not clash with anyone elses, and they should survive any sort of upgrade, and be accessible from any routines.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Category Menu Everywhere In reply to
Wondering if you ever came up with an easy way to sort order the categories alphabetically?Smile
Quote Reply
Re: [Nosmada] Category Menu Everywhere In reply to
in case you're still wondering how to sort them... just add "sort" before @root_cats

Code:
sub {
my $tags = shift;
my $cat_db = $DB->table('Category');
my @root_cats = $cat_db->select (['Full_Name'], { FatherID => 0 })->fetchall_list;
my $output;
foreach my $root_cat (sort @root_cats) {
my $url = $cat_db->as_url($root_cat);
$output .= qq~<a href="$CFG->{build_root_url}/$url">$root_cat</a><br>~;
}
return $output;
}

r
Quote Reply
Re: [ryel01] Category Menu Everywhere In reply to
It works; but what if I want to sort the menu listing by a specific filed in category table.
"sort_order" instead of the normal sorting???
thank you
Mark
> >