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

Grab a list of fields avaiable in a table?

Quote Reply
Grab a list of fields avaiable in a table?
I've just been looking over GT::SQL::Table, and have some up with;

Code:
my $list = $DB->table('Links')->form_names();

use Data::Dumper;
print Dumper($list);

This just returns;

Quote:
$VAR1 = {};

All I am trying to do, is grab a list of the fields in the MySQL table (need to be in the order that they would show up in MySQLMan).

Anyone got any ideas?

TIA

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: [Andy] Grab a list of fields avaiable in a table? In reply to
my $cols = $DB->table('Links')->{schema}->{cols};

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Grab a list of fields avaiable in a table? In reply to
Just out of interest, how did you work that out? I've spent literally 2 hours staring at the documents, and I would never have guessed that :(

BTW, Thanks Cool

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: [Andy] Grab a list of fields avaiable in a table? In reply to
I just know these things... Wink

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Grab a list of fields avaiable in a table? In reply to
LOL!

Don't suppose you would know how I could get the field names into an array? Using Data::Dumper to show the hash, gives me;

Code:
$VAR1 = {
'Description' => {
'pos' => '7',
'form_type' => 'TEXTAREA',
'form_size' => '30',
'weight' => '1',
'type' => 'TEXT',
'form_display' => 'Description'
},
'LinkOwner' => {
'not_null' => '1',
'pos' => '4',
'form_type' => 'TEXT',
'form_size' => '20',
'size' => '50',
'type' => 'CHAR',
'form_display' => 'LinkOwner',
'default' => 'admin'
},
'Hits' => {
'not_null' => '1',
'pos' => '10',
'form_type' => 'TEXT',
'form_size' => '10',
'regex' => '^\\d+$',
'type' => 'INT',
'form_display' => 'Hits',
'default' => '0'
},
'Date_Checked' => {
'pos' => '18',
'form_type' => 'DATE',
'form_size' => '20',
'type' => 'DATETIME',
'form_display' => 'Date Checked',
'default' => ''
},
'Status' => {
'not_null' => '1',
'pos' => '17',
'form_type' => 'TEXT',
'form_size' => '10',
'regex' => '^\\-?\\d+$',
'type' => 'SMALLINT',
'form_display' => 'Status',
'default' => '0'
},
'isChanged' => {
'values' => [
'No',
'Yes'
],
'not_null' => '1',
'pos' => '12',
'form_type' => 'SELECT',
'type' => 'ENUM',
'form_display' => 'isChanged',
'default' => 'No'
},
'isPopular' => {
'values' => [
'No',
'Yes'
],
'not_null' => '1',
'pos' => '13',
'form_type' => 'SELECT',
'type' => 'ENUM',
'form_display' => 'isPopular',
'default' => 'No'
},
'Add_Date' => {
'not_null' => '1',
'pos' => '5',
'form_type' => 'DATE',
'form_size' => '20',
'type' => 'DATE',
'form_display' => 'Add Date'
},
'Title' => {
'not_null' => '1',
'form_type' => 'TEXT',
'form_size' => '20',
'weight' => '3',
'form_display' => 'Title',
'pos' => '2',
'size' => '100',
'type' => 'CHAR'
},
'Timestmp' => {
'pos' => '19',
'form_type' => 'DATE',
'form_size' => '20',
'time_check' => '1',
'type' => 'TIMESTAMP',
'form_display' => 'Timestamp'
},
'Rating' => {
'not_null' => '1',
'form_type' => 'TEXT',
'form_size' => '10',
'regex' => '^\\d+\\.?\\d*$',
'form_display' => 'Rating',
'display' => '12',
'decimal' => '2',
'pos' => '15',
'type' => 'FLOAT',
'default' => '0'
},
'Votes' => {
'not_null' => '1',
'form_type' => 'TEXT',
'form_size' => '10',
'regex' => '^\\d+$',
'form_display' => 'Votes',
'unsigned' => '1',
'pos' => '16',
'default' => '0',
'type' => 'SMALLINT'
},
'isNew' => {
'values' => [
'No',
'Yes'
],
'not_null' => '1',
'pos' => '11',
'form_type' => 'SELECT',
'type' => 'ENUM',
'form_display' => 'isNew',
'default' => 'No'
},
'URL' => {
'not_null' => '1',
'form_type' => 'TEXT',
'form_size' => '20',
'weight' => '1',
'regex' => '^\\w+:',
'form_display' => 'URL',
'pos' => '3',
'size' => '255',
'default' => 'http://',
'type' => 'CHAR'
},
'Contact_Name' => {
'pos' => '8',
'form_type' => 'TEXT',
'form_size' => '20',
'size' => '255',
'type' => 'CHAR',
'form_display' => 'Contact Name'
},
'Contact_Email' => {
'pos' => '9',
'form_type' => 'TEXT',
'form_size' => '20',
'size' => '255',
'type' => 'CHAR',
'form_display' => 'Contact Email'
},
'ID' => {
'not_null' => '1',
'form_type' => 'TEXT',
'form_size' => '10',
'regex' => '^\\d+$',
'form_display' => 'ID',
'unsigned' => '1',
'pos' => '1',
'type' => 'INT'
},
'Mod_Date' => {
'not_null' => '1',
'pos' => '6',
'form_type' => 'DATE',
'form_size' => '20',
'type' => 'DATE',
'form_display' => 'Mod Date'
},
'isValidated' => {
'values' => [
'No',
'Yes'
],
'not_null' => '1',
'pos' => '14',
'form_type' => 'SELECT',
'type' => 'ENUM',
'form_display' => 'isValidated',
'default' => 'Yes'
}
};

This is what I would expect. I've been trying to get just the field names into an array. Tried quite a few ways.. but most of them give me errors... and those that don't give me errors, just put in blank values :(

TIA

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: [yogi] Grab a list of fields avaiable in a table? In reply to
BTw.. these are my poor attempts Tongue

Code:
my $cols = $DB->table('Links')->{schema}->{cols};

foreach my $key (keys ($cols)) {
print "$cols{$key}\n";
}

...gives an error, because $cols is not recognised as a hash, but a hashref?

Code:
my %cols = $DB->table('Links')->{schema}->{cols};

foreach my $key (keys (%cols)) {
print "$cols{$key}\n";
}

...gives no errors, but it doesn't give a list of the fields Frown

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: [Andy] Grab a list of fields avaiable in a table? In reply to
$DB->table('Links')->ordered_columns();

is probably what you want.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Andy] Grab a list of fields avaiable in a table? In reply to
Code:
my $cols = $DB->table('Links')->{schema}->{cols};

foreach my $key (keys %{$cols}) {
print "$cols->{$key}\n";
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Grab a list of fields avaiable in a table? In reply to
Genious :)

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: [yogi] Grab a list of fields avaiable in a table? In reply to
A bit better would be:

Code:
my %cols = $DB->table('Links')->cols;
foreach my $column (keys %cols) {
print "$column\n";
}

for two reasons:

1. You are not accessing the internal structure directly (which may change).
2. You are getting a copy of the structure, not a reference. This is important as if you make a change to it by accident, you've updated the def file. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Grab a list of fields avaiable in a table? In reply to
Thanks for the tip.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Alex] Grab a list of fields avaiable in a table? In reply to
In Reply To:
A bit better would be:

Code:
my %cols = $DB->table('Links')->cols;
foreach my $column (keys %cols) {
print "$column\n";
}

Hi both. Thanks for the replies.

Alex, is there any way this could be ordered in the order that they are stored in the database? i.e;

ID,Title,URL,Description...

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: [Andy] Grab a list of fields avaiable in a table? In reply to
Hi,

my @column_names = $DB->table('Links')->ordered_columns;

will do the trick.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Grab a list of fields avaiable in a table? In reply to
Thanks... that worked a charm :)

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!