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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
How do I display Records in a Table



Jun 7, 2003, 6:16 AM

Post #1 of 7 (908 views)
How do I display Records in a Table Can't Post


I've been banging my head with this one for awhile and would really appreciate sme help.

The Code I have below, displays 1 record per row, what I want to do is display 3 records per row, with 15 records per table.

$start = "$in{'start'}";

for ($i=$start; (($i < $start+10) && ($i < $count)); $i++)

print qq!

<TR valign="top">
<TD WIDTH="165" valign=top align="left"></TD>
<TD WIDTH="110" valign=top>
<div align="center">

<form method=post action="enlarge.cgi">
<input type=hidden name ="in" value ="$db_autoid[$i]">
<input type=IMAGE SRC="$db_thumbnail[$i]" border="0" alt=""><br><font color="black" size="2"><b>Year:</b> $db_year[$i] <br><b>Make:</b> $db_make[$i]<br><b>Model:</b> $db_model[$i] <br><b>Price:<font color="blue"> &#36; $db_price[$i]</b></font></font></form></div>



print qq!


print "</table><table width='440' align='center'>";

$previous = ($start-10);
$hold = ($i-9);
$hold1 = ($start+1);

if(($count < 11) || (($start + 10) > $count)){
print "<tr><td><div align=\"center\"><font color=\"Navy\"><b>Results $hold1 - $i of $i</b></font></div><p></td></tr><br>\n";

print "<tr align='center'>";

if($i > 10){

print "<td><form action=\"robs1.cgi\" method=post>\n";
print "<input type=IMAGE SRC=\"../images/previous.jpg\" border=\"0\">\n";
print "<input type=hidden name=\"start\" value=\"$previous\">\n";
print "</form></td>\n";

if($i < $count){
print "<div align=\"center\"><font color=\"Navy\"><b>Results $hold - $i&nbsp; of&nbsp; $count</b></font></div>\n";
print "<td><form action=\"robs1.cgi\" method=post>\n";
print "<input type=IMAGE SRC=\"../images/next.jpg\" border=\"0\"></form>\n";
print "<input type=hidden name=\"start\" value=\"$i\"></td>\n";


print "</tr></table>";

print <<EOF;


If I haven't explained myself or there are questions please let me know.

Thanks in advance



Jun 16, 2003, 2:01 PM

Post #2 of 7 (894 views)
Re: [allanonline] How do I display Records in a Table [In reply to] Can't Post

It seems to me that you are using a lot of count and position variables in your code, for no particular reason. Here is some simple pseudo-code which may explain an easier solution to your problem.

Assume all of your records are in an array (it could easily be a hash as well, it doesn't matter)

To print 3 records per row, all you have to do is tweak the TD statement a little bit. Try something like this:

my $record_count = 0;

print "<TABLE><TR>";

foreach my $record (@array)


$record_count++;#increment count by one

print "<TD>$record";

if (($record_count % 3) == 0)

{#if the count is divisible by 3, with no remainder, then you have printed 3

#records for this row, end the TD, end the TR, then create a new TR

print "</TD></TR><TR>";



{#else, create a line break, and we loop back to the next record

print "<BR>";



print "</TD></TR></TABLE>";#done with the loop, kill the table

This is rough code, but you should get the idea from this, it could be even simpler if you just used a table with no border, and used maybe a HR tag between every three rows to group them by threes. Hope this helps. Sorry if this is way off base from what you are looking for


Jun 16, 2003, 6:08 PM

Post #3 of 7 (889 views)
Re: [asayogure] How do I display Records in a Table [In reply to] Can't Post

Looks so Simple....

Exactly what I needed.



Jun 16, 2003, 7:12 PM

Post #4 of 7 (887 views)
Re: [allanonline] How do I display Records in a Table [In reply to] Can't Post

No problem, I am happy I could help.
Robert "Asayogure" McAdams
~I wish I was cool enough to be a Perl Guru ... but I'm not


Jun 27, 2003, 3:00 AM

Post #5 of 7 (855 views)
Re: [asayogure] How do I display Records in a Table [In reply to] Can't Post

Hi asayogure,

Sorry if it seems I'm buttin in on your posts here, but if you could tell me what I've done wrong with my adaptation of your code to my needs it would be appreciated

My database structure is as follows

Code|O-U|Brad|Ottum|Rd1 Box 289|McKean|16749|888 888 8888|999 999 9999|Hobbies Here|Qualifications Here|Headline Here|description here

#!/usr/bin/perl -w
use strict;

my $county = 'McKean';
my $mem_file = "/path/to/Data_files/$";
my $html_file = "/path/to/html/$county.html";

my (@data, @data1, $record_count, $record);
open(ARRAY, "$mem_file") or die "cant open $mem_file $!";
@data = <ARRAY>;
open(ARRAY, "$html_file") or die "cant open $html_file $!";
@data1 = <ARRAY>;
open(HME, ">$html_file") or die "Reason, cant open $html_file";
foreach my $detail (@data1) {
if ($detail =~ /<!--BeginHere-->/)
print HME "<!--BeginHere-->\n";
print HME "<TABLE width=100%><TH colspan=4>Listings for $county County</TH><TR>\n";
for $line (@data)
my ($email,$cat,$first_name,$last_name,$location,$county,$postal,$phone,$fax,$headline,$hobbies,$qualifications,$description) = split ('\|', $line);
$record = "$first_name $last_name<BR>$location<BR>$county<BR>Phone: $phone<BR>Fax: $fax";
print HME "<TD>$record\n";

if (($record_count % 4) == 0) {
print HME "</TD></TR><TR><TD>&nbsp;</TD></TR><TR>";
print HME "<BR>";
print HME "</TD></TR></TABLE>";
print HME "\n<!--- End Print Here --->\n";
print HME "$detail";

What I'm trying and somewhat successfully doing is reading the data_file, getting each record to print to the html file within your table structure. The script locates the <!--BeginHere--> in the html page and prints the records as requested from the database but then also prints them again, getting a double display of the table and data...

I'm sure it's something to do with my loops, but I can't put my finger to it...Any help would be appreciated...




Jul 5, 2003, 8:19 AM

Post #6 of 7 (833 views)
Re: [allanonline] How do I display Records in a Table [In reply to] Can't Post

Oops, sorry I haven't replied earlier -- my e-mail had some type of a seizure and dumped the new post notice perlguru must have sent. I will look at this and let you know ASAP.
Robert "Asayogure" McAdams
~I wish I was cool enough to be a Perl Guru ... but I'm not


Jul 10, 2003, 11:54 AM

Post #7 of 7 (817 views)
Re: [six] How do I display Records in a Table [In reply to] Can't Post

Okay, sorry this took so long, I have been busy! You have several things wrong with your code:

#1 all perl scripts that aren't going to be run from the command line, need to have the header line printed before you do much of anything else.

#2 you are using the strict pragma, so in your for loop, you have to declare your variable (for my $line, instead of just for $line)

#3 I don't know if you are doing this or not, but make sure that you make your html file writable, otherwise this code will not work.

#4 the loop (i think) you are trying to set up wants to read data from the .data file (@data), and then print the data to the html file (@data1). However, in your code you tell the script to loop through the lines in the html file. Well, if the html file is currently empty then nothing happens. If it does have stuff in it, it will run through the loop, but nothing will get changed. So on line 19 of the revised script (county_old.cgi) I have created, you need to change @data1 to @data.

#5 It appears that you want to grab the data from the .data file, then stick it into the .html file (nevermind the fact that each time you run this script you are going to have to re-build the database) However, for the code of mine that you are trying to adapt, you need to print only ONE table definition tag, then determine the row tags according to the number of records per row you want. Again, like I stated in the original post I made on this topic, whatever floats your boat is fine, but there are more effecient ways of displaying data. Anyway, your code is printing a new TABLE definition tag after each <!--BeginHere--> marker, but you aren't closing any of the tables, so, well, the output just looks ... bad. To fix this I cut out the TABLE definition and closing tags to outside the foreach loop.

Now we get to the topic of data display, and why it looks bad. The major reason for this is that you have set up two loops to do basically the same thing, so you end up squaring the math involved. For this test I stuck 5 records in the .data file (the data you included in your example), and using your original code it would print the data out 25 times! So I cut out the second 'for loop' you stuck in there, and created a new script (county_new.cgi) using the original foreach loop, and I just changed the split command to use the $detail variable (which already had the line data in it anyway) instead of creating a new variable ($line) for the other for loop. I also stuck a conclusion at the end of the script that tells you what happened, and provides a link to the html file.

You can see the (now working) script on my site (for a limited time) at:

You can download the scripts and datafiles I modified for this at

Hope this helps.


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

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