in reply to Re^5: [Solved]: Can we use print <<HTML; twice in same file?
in thread [Solved]: Can we use print <<HTML; twice in same file?

So when fetched in sorted order $widget[0] will hold the active flag for average_speed_answer not trunk_usage?. Or have you changed the script to use different elements. For example where you had

if (row[0] eq '1') { print "<td><form><input type="checkbox" name="trunk_usage" value="1" + checked>% Trunk Usage</form></td>";

is that now

if ($widget[4] eq '1') { print "<td><form><input type="checkbox" name="trunk_usage" value="1" + checked>% Trunk Usage</form></td>";
update :

If you used a hash instead of an array

my $sql = 'SELECT name,active FROM comm_desk_widget_status WHERE user = ?'; my $sth = $dbh->prepare($sql); $sth->execute('admin'); my %widget=(); while (my ($name,$active) = $sth->fetchrow_array) { $widget{$name} = $active; }

then you could use

if ($widget{'percent_trunk_usage'} == 1) { print "<td><form><input type="checkbox" name="trunk_usage" value="1" + checked>% Trunk Usage</form></td>";

and there would be no chance of getting out of step

poj

Replies are listed 'Best First'.
Re^7: [Solved]: Can we use print <<HTML; twice in same file?
by Perl300 (Friar) on Aug 25, 2015 at 21:11 UTC

    What you suggested is really useful... speacially in my case where I might have to change the query for diff users...

    With your suggestions I can use user as a variable and one thing less to worry about for me. THanks again

Re^7: [Solved]: Can we use print <<HTML; twice in same file?
by Perl300 (Friar) on Aug 25, 2015 at 20:44 UTC
    Yes, I changed exactly as you have mentioned here.

    I wanted to change the order in code so as it shows average_speed_answer first and so on but it would change sequence on webpage to something different than expected. So I assigned $widget[] in aroder to match with the result of DB query.

      See DBI.pm#selectall_hashref. For a small data set it saves a lot of code, and it returns a hashref keyed by whichever column you choose.

      my $sql = 'SELECT name, display, active FROM comm_desk_widget_status WHERE user = ?'; my $href = $dbh->selectall_hashref( $sql, 'display', undef, 'admin' ); print_html( $href ); sub print_html { my $h = shift; foreach my $label (sort keys %{ $h } ) { my $html = "<td><form><input type='checkbox' name='$h->{ $label }->{ name } +' value='1'"; $html .= $h->{ $label }->{ active } ? ' checked' : ''; $html .= ">$label</form></td>\n"; print $html; } }

      Update: forgot the bind param; HTML typo

      The way forward always starts with a minimal test.