Ok, I think I understand now... Let me see if I can make it clear, and please tell me if I am way off base or not... :o)
Ok, I have these fields currently in my DB:
name content
I should add one called "type".
Then when I call the variables I should do it in one big swipe:
get_page_content("some_type");
Something like that, and it would then go load each value for that type?
Here is how I currently do it:
$default_font_family = get_page_content("default_font_family");
Each variables '$default_font_family' is the same name in the db: 'default_font_family'.
So how could I do that, where it will take the "name" and make it a variable, without telling it like I do: 'name = sub('name');'
Could I do something like this in the subroutine:
my $type = shift;
my $dbh = DB_File::connect();
$sth = $dbh->prepare (qq{ SELECT name,content FROM page_vars WHERE
+ type = ? });
$sth->execute($type);
while ( my ($db_name,$db_content) = $sth->fetchrow_hashref() ) {
${$db_name} = $db_content;
}
$sth->finish();
return 1;
Would that work? I don't think it would, but I have not tried it. Do you know off hand if it would work?
If not I suppose I could put them in a hash or array:
my $type = shift;
my $dbh = DB_File::connect();
$sth = $dbh->prepare (qq{ SELECT name,content FROM page_vars WHERE
+ type = ? });
$sth->execute($type);
my %vars;
while (my ($db_name,$db_content) = $sth->fetchrow_hashref()) {
$vars{$db_name} = $db_content;
}
$sth->finish();
return(%vars);
Then I could just have each variable instead of being called by the name be called by a hash:
instead of doing:
$default_font_family = ...
do this:
%vars = get_page_content("some_type");
Then %vars would contain all the variables so I could print
them by doing:
$vars{default_font_family}
The only bad thing about using the hash, is that I have already done a lot of work, where I don't do that, so I'd have to put a whole lot of time into search and replace :o)
Anyways, did I understand it the way you meant it?
Thank you much, as usual!
Richard
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.