Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: Constant Variables

by The Mad Hatter (Priest)
on Mar 28, 2003 at 00:32 UTC ( [id://246376]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Constant Variables
in thread Constant Variables

It sounds as if you want to bind the variable $color1 to the value of a the corresponding column in the current row. Check out the bind_col and bind_columns methods of DBI.

Update: As CPAN appears to be down, here is the basic syntax (grabbed from perldoc DBI);

$rc = $sth->bind_col($column_number, \$var_to_bind); and $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
The variables pretty much explain themselves ;-), but if you don't get it, do a perldoc DBI (or equivalent) and find the complete doc.

Replies are listed 'Best First'.
Re: Constant Variables
by powerhouse (Friar) on Mar 28, 2003 at 05:25 UTC
    Actually, what I want to do, is have all my variables in the column 'name'. They are not yet, PERL strings. I want to make them Perl strings, containing the value in the column 'valu', of the same record id.

    For instance, I have these in there so far..

    name valu
    default_font_family Arial, Helvetica, sans-serif
    default_font_color #000000
    standout_text_color #F52D37
    disabled_text_color #C0C0C0

    Those are the top few. I want the values in the column 'name' to be Perl Strings, that contain the VALUE in column value.

    Like these examples that do NOT work:
    $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my $settings = $sth->fetchrow_hashref()) { use CONSTANT $settings->{name} => $settings->{value}; } $sth->finish(); # OR $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my $settings = $sth->fetchrow_hashref()) { $$settings->{name} = $settings->{value}; # OR ${$settings->{name}} OR @{$settings->{name}} I've tried a +ll 3, nothing is working. } $sth->finish();
    So now, when I get to a "form" or something, if I know I have the variables in column "name" above, I can put the strings. such as this...

    print qq~Welcome to <font style="font-family: $default_font_family; co +lor: $default_font_color">Company Name</font>~;
    Something like that. The reason they are in a database, instead of just in a file, is that I'm making an admin interface, where they can be editted, easily, by my partners, who do not know perl at all, so they can make changes without having to get into the source code.

    If I still did not make it clear what I'm trying to do, please let me know, I'll try to clarify it again ;)

    thx,
    Richard
      I think this might work (though I'm at school and I can't test it):
      $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); my %settings = %{$sth->fetchrow_hashref}; # I'm assuming you only + have one row... foreach my $key (keys %settings) { my $varname = "$" . $key; eval "$varname = $settings{$key}" or die "problem with eval"; } $sth->finish();
      Also, from what you've said, it seems as if you are just using the settings to globally control the properties of HTML text so you only have to change the property in one place. If this is the case, then you should use CSS (Cascading Style Sheets) to define site-wide styles and include the CSS file in every page. CSS is a very powerful mechanism for easily changing the layout and design of a site.

      Update: Hmm, I just saw that you want to use the values in the column name as the variable name. The code above uses the name of the field (or column) in MySQL as the variable name. Here is the code to do what you want:

      $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my %settings = %{$sth->fetchrow_hashref};) { my $varname = "$" . $settings{name}; eval "$varname = '$settings{value}'" or die "problem with eval"; } $sth->finish();
      That should do the trick, but it is also untested.
Re: Constant Variables
by powerhouse (Friar) on Mar 28, 2003 at 05:45 UTC
    Would it be easier to just us a "flat file" type of database?

    I think it might be easier to just use a seperate file called something like settings.config and then in the admin interface, make it where when the edit the settings, it just OPENS it, and gets the variables that way, then it can re-write the file everytime the administrator updates the values.

    I know how to do that. I just thought it might be faster to just put them in my MySQL database. Of course, If I'm only requiring a config file, then it would probably be faster then running through a database and trying to assign variables.

    At least in the config file I could make it look like this:
    $default_font_family = "whatever"; $default_font_color = "whatever2";
    Do you think that would be easier? I'm starting to feel that way, since there does not seem to be an easy way to do it with a Database. At least from what I seen thus far.

    thx,
    Richard
      Yes, since it is just config, a flat file would probably be easier to do, although if you really wanted, I think it is possible to do the MySQL thing (looping through and binding each column is the way to go, I think).

      If you defined the variables in the config file like you said, all you would have to do is a do filename; and you'd have the variables.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://246376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found