RSS Forums RSS
Please support our Perl advertiser: Programming Forums
Views: 4248 | Replies: 4
Reply
Join Date: Apr 2005
Posts: 3
Reputation: edw5086 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edw5086 edw5086 is offline Offline
Newbie Poster

Solution Print 5 records per table row (see script)

  #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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

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

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

if ($Item eq "") {
print "<td>$Item</td>"<td>$Page</td>"<td>$Desc</td>"<td>$Pack</td>"<td>$Price</td>\n";
}

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/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);

     # /* Changed The Line Below */
     if ($Item ne "") {
          print "<TR><td>$Item</td>"<td>$Page</td>"<td>$Desc</td>"<td>$Pack</td>"<td>$Price</td></TR>\n";
     }
}

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

sub dienice {
     my($msg) = @_;
     print "<h2>Error</h2>\n";
     print $msg;
     exit;
}
Reply With Quote  
Join Date: Apr 2005
Posts: 3
Reputation: edw5086 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edw5086 edw5086 is offline Offline
Newbie Poster

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

  #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  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

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

  #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?
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  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

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

  #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:

#!/usr/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

$cnt = 1;

foreach $i (@grok) {
        chomp($i);
        ($Item,$Page,$Desc,$Pack,$Price) = split(/\|/,$i);

        if ($cnt == 5) {
                print "<td>$Item</td><td>$Page</td><td>$Desc</td><td>$Pack</td><td>$Price</td>\n";
                print "</TR><TR>\n";
                $cnt = 1;
        } else {
                if ($Item ne "") {
                        print "<td>$Item</td><td>$Page</td><td>$Desc</td><td>$Pack</td><td>$Price</td>\n";
                        $cnt++;
                }
        }
}

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

sub dienice {
        my($msg) = @_;
        print "<h2>Error</h2>\n";
        print $msg;
        exit;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:05 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC