CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Having trouble with this

 



Ag555
New User

Jul 3, 2010, 3:11 PM

Post #1 of 3 (901 views)
Having trouble with this Can't Post

Hey, I'm creating a program but when it comes to displaying the information on a web page it's not showing the data which I have inputted. The program is split in 2, one part of the user to input data which then gets stored to a file, whic his here:





open STUDENT, ">>students.lst";

print "This program will allow the lecturer to enter student details into the system.\n"; #tell user what program does

print "How many students would you like to enter? \n";

$amount = <STDIN>; #user input how many students entered.

$counter = 0; #set counter to 0

while($counter<$amount){ #while loop will keep going until the same amount as user has entered.

print "Student Name: ";
$student_name = <STDIN>;
print STUDENT $student_name;

#these are the information, which are getting inputted by user
print "Student ID: ";
$student_ID = <STDIN>;
print STUDENT $student_ID;

print "Course: ";
$course = <STDIN>;
print STUDENT $course;

print "Age: ";
$Age = <STDIN>;
print STUDENT $Age;


$counter++; #counter incremented by 1 so loop continues

} #end the while loop

exit;





I then have program 2, which I think is the problem, if anyone can see any problems as to why it's not showing correctly on the web page, I would be grateful, thanks.







open STUDENT, "<students.lst"; #opening the list as input

open STUDENT2, ">cohort.html"; #open html document as output.

print "This will put the information in a HTML document, alphabetically in table format. \n";

print STUDENT2 "<html><body><head><h1><u>Student details Alphabetically </u></h1></head></p>";

#start of the table
print STUDENT2 "<TABLE BORDER=2>";


print STUDENT2 "<TR><TD><th> Student Name</TH></TD>";
print STUDENT2 "<TD><TH> Student ID</TH></TD>";
print STUDENT2 "<TD><TH> Course</TH> </TD>";
print STUDENT2 "<TD><TH> Age </TH></TD></TR>";

while (!EOF STUDENT) {

$student_name = <STUDENT>;
$student_ID = <STUDENT>;
$Course= <STUDENT>;
$age= <STUDENT>;


print STUDENT2 "<TR><TD>",$student_name,"</TD>";
print STUDENT2 "<TD>" ,$student_ID, "</TD>";
print STUDENT2 "<TD>" ,$course, "</TD>";
print STUDENT2 "<TD>" ,$age, "</TD></TR>";

}

print STUDENT2 "</TABLE>";
#above it putting information in table format on html document

}

"</body></html>";
exit; #exit program


funkerama
Novice

Jul 12, 2010, 8:00 AM

Post #2 of 3 (837 views)
Re: [Ag555] Having trouble with this [In reply to] Can't Post

Without seeing output or errors, my only thought (and I'm no expert) is that you need to reference the data as array elements instead:


Quote
while (!EOF STUDENT) {

$student_name = <STUDENT>;
$student_ID = <STUDENT>;
$Course= <STUDENT>;
$age= <STUDENT>;


print STUDENT2 "<TR><TD>",$student_name,"</TD>";
print STUDENT2 "<TD>" ,$student_ID, "</TD>";
print STUDENT2 "<TD>" ,$course, "</TD>";
print STUDENT2 "<TD>" ,$age, "</TD></TR>";


Try:

$student_name = $STUDENT[0];
$student_ID = $STUDENT[1];
$Course= $STUDENT[2];
$age= $STUDENT[3];

Just a thought...I may be way off (which wouldn't surprise me).


savo
User

Jul 14, 2010, 6:51 AM

Post #3 of 3 (826 views)
Re: [Ag555] Having trouble with this [In reply to] Can't Post

You have a few problems first try to post code using the code tags.

Allways start all scripts with


Code
use warnings; 
use strict;


Open files with


Code
my $student_fh; 
if ( !open $student_fh, '>>', 'students.lst' ) {
die "didnt open? ($!)";
}


Your while loop needs to look like this


Code
while (<student_fh>) {  

}


I am assuming that you have got passed this already but I would recommend editing what you have now to reflect the above and get it running without warnings then come back with your new problems.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives