Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI, HTML TABLE, and LOOP

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
US
I have written the following program. The purpose is to read three records and print them accross, and then read another three records and print them until all the records are written. With the logic in my program, it reads and prints one record, and prints it accross the three places. It prints the first record three times but it does not print the picture. Any help will be appreciated.

#!/usr/bin/perl
print "Content-type:text/html\n\n";

#storer.cgi - use with store.html and store.cgi and store.out - item for Sale

open(INF,"store1.out") or dienice("Couldn't open auto.out for reading: $! \n");

print &quot;<h2>ITEMS For Sale</h2>\n&quot;;

@data = <INF>;
close(INF);

# First we initialize some counters and hashes for storing the
# summarized data.
$count =0;
$commentary =&quot;&quot;;
$d =&quot;\$&quot;;


foreach $i (@data) {

$coun=1;

sub_loop;

chomp($i);
($name,$add,$city,$state,$zip,$item,$photo,$size,$color,$price,$email,$tele,
$time,$photo1,$comment,$month,$day,$year)=split(/\|/,$i);



# this is the same as $count = $count + 1;

$count++;

print &quot;$coun\n&quot;;

if ($coun=1) {

$name1 = $name;
$add1 = $add;
$city1 = $city;
$state1 = $state;
$zip1 = $zip;
$item1 = $item;
$photo1 = $photo;
$size1 = $size;
$color1 = $color;
$price1 = $price;
$email1 = email;
$tele1 = $tele;
$time1 = $time;
$photo11 = $photo1;
$comment1 = $comment;
$month1 = $month;
$day1 = $day;
$year1 = $year;
print &quot;=1\n&quot;;

}

if ($coun=2) {

$name2 = $name;
$add2 = $add;
$city2 = $city;
$state2 = $state;
$zip2 = $zip;
$item2 = $item;
$photo2 = $photo;
$size2 = $size;
$color2 = $color;
$price2 = $price;
$email2 = email;
$tele2 = $tele;
$time2 = $time;
$photo12 = $photo1;
$comment2 = $comment;
$month2 = $month;
$day2 = $day;
$year2 = $year;
print &quot;=2\n&quot;;
}

if ($coun=3) {

$name3 = $name;
$add3 = $add;
$city3 = $city;
$state3 = $state;
$zip3 = $zip;
$item3 = $item;
$photo3 = $photo;
$size3 = $size;
$color3 = $color;
$price3 = $price;
$email3 = email;
$tele3 = $tele;
$time3 = $time;
$photo13 = $photo1;
$comment3 = $comment;
$month3 = $month;
$day3 = $day;
$year3 = $year;
print &quot;=3\n&quot;;
}


$coun++;

if ($coun<3) {
&loop;
}


print &quot;<table width=\&quot;100%\&quot;> <tr>\n&quot;;
print &quot;<td><img src=\&quot;/images/$photo11\&quot; width=\&quot;150\&quot; height=\&quot;150\&quot; align=\&quot;left\&quot; hspace=\&quot;15\&quot;></td>\n&quot;;
print &quot;<td><img src=\&quot;/images/$photo12\&quot; width=\&quot;150\&quot; height=\&quot;150\&quot; align=\&quot;left\&quot; hspace=\&quot;15\&quot;></td>\n&quot;;
print &quot;<td><img src=\&quot;/images/$photo13\&quot; width=\&quot;150\&quot; height=\&quot;150\&quot; align=\&quot;left\&quot; hspace=\&quot;15\&quot;></td></tr>\n&quot;;
print &quot;<tr><td>Item : $item1 <br> Price: <u>$d$price1</u><br>Color: <u>$color1</u> Size: <u>$size1</u><br> Phone:
<u>$tele1</u><br> Time-to-call: <u>$time1</U><br> E-mail: <u>$email1</u><br>\n&quot;;
print &quot;Description: $comment1.<br>\n&quot;;
print &quot;<td>Item : $item2 <br> Price: <u>$d$price2</u><br>Color: <u>$color2</u> Size: <u>$size2</u><br> Phone:
<u>$tele2</u><br> Time-to-call: <u>$time2</U><br> E-mail: <u>$email2</u><br>\n&quot;;
print &quot;Description: $comment2.<br>\n&quot;;

print &quot;<td>Item : $item3 <br> Price: <u>$d$price3</u><br>Color: <u>$color3</u> Size: <u>$size3</u><br> Phone:
<u>$tele3</u><br> Time-to-call: <u>$time3</U><br> E-mail: <u>$email3</u><br>\n&quot;;
print &quot;Description: $comment3.<br></tr>\n&quot;;

print &quot;$coun, $count\n&quot;;

&loop1;
}

close INF;

print <<EndHTML;
EndHTML
sub dienice {
my($msg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print $msg;
exit;
}

 
can you show the contents of your store1.out file and maybe an example of how you want it to appear as HTML formatted text

Duncan
 
Here is a sample record:
|||||Purse|Yes|6 x 8 |light black|10|shaha1s@cmich.edu|773-6497|after 5 P.M.|purse2.jpg|Hand-made purse with embroidery.|10|11|2003
There are three similar records.

I want them to show up as:


Picture Picture Picture

name name name
size size size
and so on.

Thanks for your help.
 
When you say &quot;It doesn't print the picture&quot;, what does it show - the broken picture image, or nothing at all? Take a look at the <img> tag in the generated HTML page - maybe it'll show up a problem that isn't obvious in the source code.

You also have the problem of printing each record three times, rather than printing them three at a time - which I assume is what you're trying to do. Some adjustments to your loops and judicious use of arrays will make it work, and make it simpler too...
[tt]
#!/usr/bin/perl
print &quot;Content-type:text/html\n\n&quot;;

#storer.cgi - use with store.html and store.cgi and store.out - item for Sale

# $COLUMNS holds the number of columns to display
# It's easier to change it subsequently if you put it up here!
$COLUMNS = 3;

open(INF,&quot;store1.out&quot;) or dienice(&quot;Couldn't open auto.out for reading: $! \n&quot;);

print &quot;<h2>ITEMS For Sale</h2>\n&quot;;

@data = <INF>;
close(INF);

# First we initialize some counters and hashes for storing the
# summarized data.
$commentary =&quot;&quot;;
$d =&quot;\$&quot;;

$col = 1;

print &quot;<table width=\&quot;100%\&quot;>\n&quot;;

foreach $i (@data) {

chomp($i);
($name,$add,$city,$state,$zip,$item,$photo,$size,$color,$price,$email,$tele,
$time,$photo1,$comment,$month,$day,$year)=split(/\|/,$i);

$name[$col] = $name;
$photo1[$col] = $photo1;
$item[$col] = $item;
# You can type in the rest of the columns here!

if ($col == $COLUMNS) {
print &quot;<tr>\n&quot;;
for ($i=1; $i <= $COLUMNS; $i++) {
print &quot;<td><img src=\&quot;/images/$photo1[$i]\&quot; width=\&quot;150\&quot; height=\&quot;150\&quot; align=\&quot;left\&quot; hspace=\&quot;15\&quot;></td>\n&quot;;
}
print &quot;</tr>\n&quot;;

print &quot;<tr>\n&quot;;
for ($i=1; $i <= $COLUMNS; $i++) {
print &quot;<td>Item : $item[$i]<br>Name: $name[$i]</td>&quot;;
}
print &quot;</tr>\n&quot;;

# Having printed one line of records, reset for the next
$col = 1;
} else {
$col++
}
}

# We get here when we've reached the end of the file
# but there might be a half-finished row still to print...

if ($col > 1) {

print &quot;<tr>\n&quot;;
for ($i=1; $i < $col; $i++) {
print &quot;<td><img src=\&quot;/images/$photo1[$i]\&quot; width=\&quot;150\&quot; height=\&quot;150\&quot; align=\&quot;left\&quot; hspace=\&quot;15\&quot;></td>\n&quot;;
}
for ($i=$col; $i <= $COLUMNS; $i++) {
print &quot;<td>&amp;nbsp;</td>&quot;;
}
print &quot;</tr>\n&quot;;

print &quot;<tr>\n&quot;;
for ($i=1; $i < $col; $i++) {
print &quot;<td>Item : $item[$i]<br>Name: $name[$i]</td>&quot;;
}
for ($i=$col; $i <= $COLUMNS; $i++) {
print &quot;<td>&amp;nbsp;</td>&quot;;
}
print &quot;</tr>\n&quot;;
}

print &quot;</table>&quot;;

close INF;

print <<EndHTML;
EndHTML
sub dienice {
my($msg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print $msg;
exit;
}
[/tt]
I haven't been able to test this, but it should be more-or-less right. Hopefully it'll put you on the right track.

-- Chris Hunt
 
ChrisHunt:

Your program worked. I really appreciate your help. I was wondering what to change if I wanted to print four columns accross instead of three. I could see how you set it up for three.

Thanks again.

GREAT JOB!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top