Print 5 records per table row (see script)

Reply

Join Date: Apr 2005
Posts: 3
Reputation: edw5086 is an unknown quantity at this point 
Solved Threads: 0
edw5086 edw5086 is offline Offline
Newbie Poster

Print 5 records per table row (see script)

 
0
  #1
Apr 29th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
0
  #2
Apr 29th, 2005
I could be wrong, but it seems to me that line:

  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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: edw5086 is an unknown quantity at this point 
Solved Threads: 0
edw5086 edw5086 is offline Offline
Newbie Poster

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

 
0
  #3
Apr 29th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
0
  #4
Apr 29th, 2005
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?
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
0
  #5
Apr 29th, 2005
Alright, it's not "pretty" by way of displaying it to the page, but it does exactly what you want it to do:

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC