944,083 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 6891
  • Perl RSS
Apr 29th, 2005
0

Print 5 records per table row (see script)

Expand Post »
Hello Everyone! how are you? fine i hope.
How can i print just 5 records per Table row and the next 5 records on the next table row? Much thanks in advance!
------------------------------------------------------------------------
#!/perl/bin/perl
print "Content-type:text/html\n\n";

open(INF,"data.txt") or dienice("Can't open data.txt: $! \n");
@grok = <INF>;
close(INF);

print <<EndHdr;
<html><head><title>My Data</title></head>
<body>
<center>
<h2 align="CENTER">My Data</h2>
<table border="1">
<tr>
EndHdr

foreach $i (@grok) {
chomp($i);
($Item,$Page,$Desc,$Pack,$Price) = split(/\|/,$i);
if ($Item eq "") {
print "<td>$Item</td>"<td>$Page</td>"<td>$Desc</td>"<td>$Pack</td>"<td>$Price</td>\n";
}
}

print <<EndFoot;
</tr>
</table>
</center>
</body>
</html>
EndFoot

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
edw5086 is offline Offline
3 posts
since Apr 2005
Apr 29th, 2005
0

Re: Print 5 records per table row (see script)

I could be wrong, but it seems to me that line:

Perl Syntax (Toggle Plain Text)
  1. if ($Item eq "") {
  2. print "<td>$Item</td>"<td>$Page</td>"<td>$Desc</td>"<td>$Pack</td>"<td>$Price</td>\n";
  3. }

is not correct. You are saying "if $item variable is empty, then print this to the web page". Maybe you planned for $item to be empty, and if so, then to print the stuff to the page, but I'm guessing that's partly why it's not working. If I am wrong, please let me know. Also, That would not print 5 records, per row, it would print 1 record with 5 fields, per row. I'm figuring that is what you want. The code should probably look like this:

Perl Syntax (Toggle Plain Text)
  1. #!/perl/bin/perl
  2. print "Content-type:text/html\n\n";
  3.  
  4. open(INF,"data.txt") or dienice("Can't open data.txt: $! \n");
  5. @grok = <INF>;
  6. close(INF);
  7.  
  8. print <<EndHdr;
  9. <html><head><title>My Data</title></head>
  10. <body>
  11. <center>
  12. <h2 align="CENTER">My Data</h2>
  13. <table border="1">
  14. <tr>
  15. EndHdr
  16.  
  17. foreach $i (@grok) {
  18. chomp($i);
  19. ($Item,$Page,$Desc,$Pack,$Price) = split(/\|/,$i);
  20.  
  21. # /* Changed The Line Below */
  22. if ($Item ne "") {
  23. print "<TR><td>$Item</td>"<td>$Page</td>"<td>$Desc</td>"<td>$Pack</td>"<td>$Price</td></TR>\n";
  24. }
  25. }
  26.  
  27. print <<EndFoot;
  28. </tr>
  29. </table>
  30. </center>
  31. </body>
  32. </html>
  33. EndFoot
  34.  
  35. sub dienice {
  36. my($msg) = @_;
  37. print "<h2>Error</h2>\n";
  38. print $msg;
  39. exit;
  40. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Apr 29th, 2005
0

Re: Print 5 records per table row (see script)

Your right about that "ne"! Mistakes can be made when your thinking of to much stuff at one time. It's corrected, but i still need help with printing 5 records per row. I think something like $count==1 or some sort of thing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
edw5086 is offline Offline
3 posts
since Apr 2005
Apr 29th, 2005
0

Re: Print 5 records per table row (see script)

Ok, I just wanted to make sure that you REALLY wanted 5 records per row, because you already have 1 record, with 5 fields in a row.... I'll work on this problem though and let you know what I come up with. You know how that will like right?
Perl Syntax (Toggle Plain Text)
  1. Item1 Page1 Desc1 Pack1 Price1 Item2 Page2 Desc2 Pack2 Price2 Item3 Page3 Desc3 Pack3 Price3 ...

If you have no problems with attaching the file that you are reading this info from, I will gladly help you to set this code in motion.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Apr 29th, 2005
0

Re: Print 5 records per table row (see script)

Alright, it's not "pretty" by way of displaying it to the page, but it does exactly what you want it to do:

Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. print "Content-type:text/html\n\n";
  4.  
  5. open(INF,"data.txt") or dienice("Can't open data.txt: $! \n");
  6. @grok = <INF>;
  7. close(INF);
  8.  
  9. print <<EndHdr;
  10. <html><head><title>My Data</title></head>
  11. <body>
  12. <center>
  13. <h2 align="CENTER">My Data</h2>
  14. <table border="1">
  15. <tr>
  16. EndHdr
  17.  
  18. $cnt = 1;
  19.  
  20. foreach $i (@grok) {
  21. chomp($i);
  22. ($Item,$Page,$Desc,$Pack,$Price) = split(/\|/,$i);
  23.  
  24. if ($cnt == 5) {
  25. print "<td>$Item</td><td>$Page</td><td>$Desc</td><td>$Pack</td><td>$Price</td>\n";
  26. print "</TR><TR>\n";
  27. $cnt = 1;
  28. } else {
  29. if ($Item ne "") {
  30. print "<td>$Item</td><td>$Page</td><td>$Desc</td><td>$Pack</td><td>$Price</td>\n";
  31. $cnt++;
  32. }
  33. }
  34. }
  35.  
  36. print <<EndFoot;
  37. </tr>
  38. </table>
  39. </center>
  40. </body>
  41. </html>
  42. EndFoot
  43.  
  44. sub dienice {
  45. my($msg) = @_;
  46. print "<h2>Error</h2>\n";
  47. print $msg;
  48. exit;
  49. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Matching negative numbers
Next Thread in Perl Forum Timeline: help with writing CGI!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC