[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Perl cgi - dynamically generate tabulated form

Asked by illuzian in Perl Programming Language

Tags: Perl, Form, CGI, Table, DBI, Mysql, SQL

HI All,

Need some help with something I'm writing and I've hit a mental block.

At current I'm fetching some data from sql and have stored a hash ref for each row in an array:

sub loadsheet {
        my $loadsheet = param(".SheetDate");
        $sth = $dbh->prepare("select Jobs.JobName,Jobs.JobDesc,Jobs.JobHelp From Jobs,Jobstatus where Jobstatus.Date = DATE('$loadsheet') AND Jobs.JobName = Jobstatus.JobName");
        $sth->execute;
        my @tbldata;
        while (my $a = $sth->fetchrow_hashref()) {
                push(@tbldata, $a);
        }
        print htmlhead("Sheet");
        foreach my $hash (@tbldata) {
                foreach ( keys %$hash ) {
                       print h1("$_ $$hash{$_}");
                }
        }
        end_html;


Basically what want is to create a table+form ie:
COL HEADINGS
jobname | desc | url | tickbox | tickbox | textbox
jobname | desc | url | tickbox | tickbox | textbox
jobname | desc | url | tickbox | tickbox | textbox
[SUBMIT]

I can handle everything except how to generate the table with all form fields in it (last 3 columns), obviously I need to form in a table so that the presentation remains readable.

I could generate each field in the foreach loop however without it being in a table I'm somewhat lost, on top of that I also wouldn't have an idea how to gather the data submitted by the form to later 'update set' into the sql DB using form fields with the same name.


Thanks in advance guys.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
#!/usr/bin/perl
use CGI qw(:standard);
use warnings;
use strict;
use DBI;
use DBD::mysql;
#Configurable settings:
my $dbuser = 'user';
my $dbpass = 'pass';
my $dbhost = 'localhost';
#End config
#Database setup
my $dsn = "dbi:mysql:webdb:$dbhost:3306";
my $dbh = DBI->connect($dsn, $dbuser, $dbpass)
        or die "Can't connect to the DB: $DBI::errstr\n";
my $sth;
#Get day of week and date
(undef,undef,undef,my $dayofmonth,my $month,my $year,my $dayofweek,undef,undef) = localtime(time); $year += 1900; $month++;
my $date = "$dayofmonth-$month-$year";
#This converts current day to database format
my %days = (
        "1" => "Mon",
        "2" => "Tue",
        "3" => "Wed",
        "4" => "Thu",
        "5" => "Fri",
        "6" => "Sat",
        "7" => "Sun",
        );
my $dbday = $days{$dayofweek};
#Index of subroutine pages
my %pages = (
	'Login To Sheet'	=> \&newsheet, #Action to perform if no current sheet is detected
	'Go To Sheet'		=> \&loadsheet, #Action to perform if a current sheet was detected
	'Proceed To Sheet'	=> \&loadsheet, #Loads the daily schedule
);
#Figur out which page to load
my $page = param(".Page");
 
if ($pages{$page}) {
	$pages{$page}->();
} 
else {
	login();
}
 
#Generate html header and page name USAGE: print htmlhead("Page Name");
sub htmlhead {
	return header(), start_html(shift);
}
 
#Login page
sub login {
	$sth = $dbh->prepare("select Date from Sheets where Current = 1");
	$sth->execute;
	my @currentsheets;
	while (my $a = $sth->fetchrow_array()) {
		push(@currentsheets, $a);
	}
	if (@currentsheets) {
		print htmlhead("Login Page"),
                h1("Sheet Login - Current Sheet Already Available"),
		h3("Select from list:"),
		start_form(),
		popup_menu(-NAME => ".SheetDate", -VALUES => \@currentsheets),
		submit(-NAME => ".Page", -VALUE => "Go To Sheet"),
		end_form(),
		end_html;
		
	}
	else {
		my %teammem;
		$sth = $dbh->prepare("select Fname,Lname,TeamID from Team");
		$sth->execute;
		while (my @a = $sth->fetchrow_array()) {
		$teammem{"$a[2]"} = "$a[0] $a[1]";
		}
		print htmlhead("Login Page"),
		h1("Sheet Login $dbday"),
		start_form(),
		p("Enter TeamID:", popup_menu(-NAME => ".TeamID", -VALUES => \%teammem)),
		submit(-NAME => ".Page", -VALUE => "Login To Sheet"),
		end_form(),
		end_html;
	}
}
 
sub newsheet {
	my $teamid = param(".TeamID");
	$sth = $dbh->prepare("INSERT into Jobstatus (Date,JobName)  SELECT CURDATE(),JobName FROM Jobs WHERE $dbday = 1");
	$sth->execute;
	$sth = $dbh->prepare("INSERT INTO Sheets (Date,Current,Locked,TeamID) VALUES(CURDATE(),1,1,$teamid)");
	$sth->execute;
	print htmlhead("test"),
	h3("User $teamid created new sheet for $date($dbday)"),
	hidden(-NAME => ".SheetDate", -DEFAULT => "$date"),
	submit(-NAME => ".Page", -VALUE=> "Proceed To Sheet"),
	end_html;
}
 
sub out {
	print header(), start_html("output"),
	h1("Output", param(".TeamID"));
}
 
sub loadsheet {
	my $loadsheet = param(".SheetDate");
	$sth = $dbh->prepare("select Jobs.JobName,Jobs.JobDesc,Jobs.JobHelp From Jobs,Jobstatus where Jobstatus.Date = DATE('$loadsheet') AND Jobs.JobName = Jobstatus.JobName");
	$sth->execute;
	my @tbldata;
	while (my $a = $sth->fetchrow_hashref()) {
		push(@tbldata, $a);
	}
	print htmlhead("Sheet");
	foreach my $hash (@tbldata) {
		print p(
		#foreach ( keys %$hash ) {
		#	print h1("$_ $$hash{$_}");
		#}
	}	
	end_html;
	
}
[+][-]09/19/09 11:11 PM, ID: 25376052Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Perl Programming Language
Tags: Perl, Form, CGI, Table, DBI, Mysql, SQL
Sign Up Now!
Solution Provided By: illuzian
Participating Experts: 2
Solution Grade: A
 
[+][-]09/18/09 05:20 AM, ID: 25365088Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/18/09 02:31 PM, ID: 25370151Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]09/18/09 10:18 PM, ID: 25371576Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_EXPERT_20070906