Gossamer Forum
Home : Products : Gossamer Links : Discussions :

preventing categories from multi select box

Quote Reply
preventing categories from multi select box
I have a situation where I have a very long list of categories that I allow users to select multiple options from. There are certain directories that I want to prevent people from adding links to. I want those directories to NOT appear in the select box list.

What I've done is create an Enum(Yes,No) column in the Categories table called "AllowLinks".

It is default to "Yes". If can be set to "No" by the admin.

I am trying to edit sub get_all_categories in Link.pm to recognize that attribute. Here's where I've gotten to so far -- but it doesn't work.

Many thanks to anyone who can set me straight here.
Mike


sub get_all_categories {
# -------------------------------------------------------------------
# Returns a select box of all categories.
#
my $self = shift;
my $id = shift;
my $name = shift || 'CatLinks.CategoryID';
my $mult = shift || 5;
my $db = $DB->table ('Category');
# my $sth = $db->select ( ['ID', 'Full_Name'] );
my $sth = $db->select ( ['ID', 'Full_Name', 'AllowLinks'] ); # my mod.

my %res = ();

# while (my ($id, $name) = $sth->fetchrow_array) {
# $res{$id} = $name;
# }

while (my ($id, $name) = $sth->fetchrow_array) { # my mod.
if ($sth [2] == "Yes"){
$res{$id} = $name;
}
}

return $self->select ( { name => $name, values => \%res, value => $id, multiple => $mult, sort => sub { lc $_[0] cmp lc $_[1] } } );
Quote Reply
Re: [Swaylock] preventing categories from multi select box In reply to
Hi,

you want to use a condition object to modify the select, or change it to:



my $sth = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] ); # my mod.



This reads select from table ID, Full_Name where AllowLinks is Yes.


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] preventing categories from multi select box In reply to
Pugdog,

Thanks for the hint. So just for my clearification (and education): Is the code:

my $sth = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] ); # my mod.

is essentially saying "if AllowLinks = Yes load ID & Full_Name into $sth?

Is $sth an array?

thanks for the help.
Mike
Quote Reply
Re: [Swaylock] preventing categories from multi select box In reply to
Basically, yeah.

the [] part defines which fields need to be imported from the database. This stops Links SQL having to get all 20 odd fields, which can cause quite a high slowdown.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Swaylock] preventing categories from multi select box In reply to
No $sth is a statement handle.

You could do the following though to get the data into an arrray...

Code:
my @array = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] )->fetchrow;

...or for a hash reference:

Code:
my $hash_ref = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] )->fetchrow_hashref;

If you used your original code you would have to call either fetchrow() or fetchrow_hashref() using the $sth object, for example:

Code:
my $sth = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] );
my @array = $sth->fetchrow;

['ID', 'Full_Name'] is an array reference of column names. This determines which column values are selected from the database by your query.
Quote Reply
Re: [Paul] preventing categories from multi select box In reply to
Thankyou.

It's starting to make sense to me. My basic but well written "learning perl" book covers the small arrow -> in very little detail so I'm kinda working from the outside in.

Thanks again. Pugdog's suggestion worked and I got the code working.

Mike.
Quote Reply
Re: [Swaylock] preventing categories from multi select box In reply to
Could you post your final code?

Many thanks Smile

------------------------------------------
Quote Reply
Re: [DogTags] preventing categories from multi select box In reply to
sub get_all_categories {
# -------------------------------------------------------------------
# Returns a select box of all categories.
#
my $self = shift;
my $id = shift;
my $name = shift || 'CatLinks.CategoryID';
my $mult = shift || 5;
my $db = $DB->table ('Category');
# my $sth = $db->select ( ['ID', 'Full_Name'] ); #original code

# mod to prevent categories with allow links set to No to appear in select box
my $sth = $db->select ( { AllowLinks => 'Yes' }, ['ID', 'Full_Name'] ); # my mod.

my %res = ();

while (my ($id, $name) = $sth->fetchrow_array) {
$res{$id} = $name;
}

return $self->select ( { name => $name, values => \%res, value => $id, multiple => $mult, sort => sub { lc $_[0] cmp lc $_[1] } } );
}