[ back to toc ]

Writing a for loop in cgi - RESEND

Date: 2002/04/22 14:37

Q:

Hi Peter,

Thanks for the response although it turns out I didn't give you enough
information. I was afraid of that. Sorry for not giving you enough info.

Take a look at the following page:

http://www.pcpowersystem.com/peter.php

This php code is fully functional and does everything I want it to do.
You'll notice I set $troid as a fixed variable. In real life this variable
will be set when the member logs in.

Here is the code:

<?
require('local.config.inc.php3');

$troid = '6082601';

?>
<html>
<head>
<title>Coop TROPN Preferences</title>
</head>
<body>

<?
if ($Action == 'Edit') {
// Set Power Leg# in rycoop table

if ($Submit) {
$sql ="UPDATE rycoop SET tropn=$tropn WHERE id=$troid";

$result = mysql_query("$sql");
if (!$result) {
$error = mysql_error();
echo "Update failed: " . $error;
echo "<p>Click <a href=\"$webaddress.$PHP_SELF\">HERE</a> to
return.</center>";
}
else {
echo "<p align=center>TROPN successfully set to ".$tropn." for
member: ".$troid.".";
}
}
}
else {

$selstr = "SELECT * FROM $MemberTable WHERE id=$troid";
$result = mysql_query("$selstr");

if (@mysql_num_rows($result)) {

?>

<p align=center>Select Power Leg

<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<input type="hidden" name="Action" value="Edit">

<?

while ($myrow = mysql_fetch_array($result)) {

?>

<table width="250" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr>
<td align=left><font size=-1>Select Power Leg</td>
<td><select name="tropn"><option>1<br>

<?
for ($i=2; $i <= $myrow['maxtr']; $i++) {
?>
<option><?php echo $i; ?><br>
<?
}

?>
</select>
</td>
</tr>
</table>
<p align="center"><input type=submit name=Submit value="Select
Power Leg"> <input type=reset>
</form>

<?
}
}
}

Now what I would like to do is apply the same functionality I have used to
display the drop down menu in the cgi script below. Scroll down about
three quarters of the way. I have commented out a big section where I want
to add it. I have also gone into a more detailed explanation:

#!/usr/bin/perl
##########################################################################
###
# File Name: cooporder.cgi
# Description: Co-op Order Form
# Written By: Mike Artobello
# Version: 1.0
# Last Modified: 05/23/01
##########################################################################
###

# Require Config File
if (-e 'config.cgi') {
require 'config.cgi';
}
else {
print "Content-type: text/html\n\n";
print qq~
<html><head><title>Config File Not Available</title></head>
<body><center><h2>Config File Not Available</h2></center>\n</body>
</html>~;
exit;
}

# Require Subroutine File
if (-e 'common.cgi') {
require 'common.cgi';
}
else {
print "Content-type: text/html\n\n";
print qq~
<html><head><title>Subroutine File Not Available</title></head>
<body><center><h2>Subroutine File Not Available</h2></center>\n</body>
</html>~;
exit;
}

##########################################################################
###
# Initialize Variables
##########################################################################
###
use CGI;

# Setup CGI processing
$cgi = new CGI;

# Form Values
$id = $cgi->param('id');
$password = $cgi->param('password');

# Get Today's Date
chomp ($today = 'date '+%Y%m%d'');

# Co-op Price
$amount = '100.00';

##########################################################################
###
# Main Processing Routine
#
# Process member login
# Print order form
#
##########################################################################
###

# Process member login
if ($id eq '' || $password eq '') { &Logon; }
&MemberLogin($id,$password);
if ($member{id} eq '') { &ReturnError ("Invalid Member","Please check your
ID number and Password and re-try!"); }

# Get billing info
&GetBilling($id);

# Check if account is current
$ldate = $billing{lastbill};
$ldate =~ s/-//g;
$ndate = $billing{nextbill};
$ndate =~ s/-//g;
$pdate = $billing{pastdue};
$pdate =~ s/-//g;
#if ($today >= $pdate) { &ReturnError ("Account Past Due","Your account is
past due. You must first make a payment."); }

# Decrypt card info, if present
if ($billing{ccinfo}) { ($cardnum,$expdate) =
&Decrypt($billing{ccinfo}); }

&PrintPage;

exit;

##########################################################################
###
# Subroutine: Display Login Page
#
##########################################################################
###
sub Logon {

print "Content-type: text/html\n\n";

print qq~
<html>
<head>
<title>PC Power System Co-op Order Login</title>
<link rel="stylesheet" type="text/css" href="condensed_tour.css">
</head>

<body background="../images/teaser_bg.gif" topmargin=20>

<div>
<table width="500" cellpadding="0" cellspacing="0" border="0"
align=center>
<tr><td align="left" valign="top" width="18" height="18"><img
src="../images/bt_t_l.gif" width="18" height="18" align="Top"
border="0"></td><td align="left" valign="top" width="472" height="18"
bgcolor="#FFFFFF"><font size="-2"> </font></td><td align="left"
valign="top" width="18" height="18"><img src="../images/bt_t_r.gif"
width="18" height="18" align="Top" border="0"></td></tr><tr><td width=500
bgcolor="#FFFFFF" colspan=3 align=center>

<table width="93%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="../images/t_t_l.gif" width="15" height="15" border="0"></td><td
width=100% rowspan="3" valign="middle">

<div class=heading2>PC Power System Co-op Order Login</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="../images/t_t_r.gif" width="15" height="15" border="0"></td>
</tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="../images/t_b_l.gif" width="15"
height="15" border="0"></td><td align="left" valign="bottom" width="15"
height="15"><img src="../images/t_b_r.gif" width="15" height="15"
border="0"></td></tr></table>

<form method="post" action="cooporder.cgi">
<div>To place your Co-op order enter your Life Force PIN and
Password.</div>

<table align=center cellspacing=3>
<tr>
<td><b><i><font face=arial>Life Force PIN:</i></b></td>
<td><input name="id" type=text size=10 maxsize=8"></td>
</tr>
<tr>
<td><b><i><font face=arial>Password:</i></b></td>
<td><input name="password" type=password size=10 maxsize=12"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type=submit name=submit
value="Logon"> <input type=reset></td>
</tr>
</form>
</table>

<form method="post" action="$webaddress/cgi/pwlookup.cgi">

<div>Forgot your Life Force PIN/Password? If so, just enter your email
address below and it will be sent to you. You MUST use the same email
address you used when you signed up.</div>

<div class=heading3b><input name="email" type=text size=25
maxsize=50></div>

<div class=heading3b><input type=submit name=submit value="Find
Me!"></div>

</form>

<div class="heading5d">Copyright 2000 - 2001 © PC POWER SYSTEM - All
rights reserved</div>

</td></tr><tr><td align="left" valign="top" width="18" height="18"><img
src="../images/bt_b_l.gif" width="18" height="18" align="Top"
border="0"></td><td align="left" valign="top" width="472" height="18"
bgcolor="#FFFFFF"><font size="-2"> </font></td><td align="left"
valign="top" width="18" height="18"><img src="../images/bt_b_r.gif"
width="18" height="18" align="Top" border="0"></td></tr>
</table>
<div>
</body>
</html>
~;

exit;
}

##########################################################################
###
# Subroutine: Print Page
##########################################################################
###
sub PrintPage {
$name = $billing{name};
$address = $billing{address};
$city = $billing{city};
$state = $billing{state};
$zip = $billing{zip};
$email = $billing{email};
$cycle = $billing{cycle};

if ($billing{cardtype} =~ /Visa/i) { $S1 = "
SELECTED"; }
elsif ($billing{cardtype} =~ /MasterCard/i) { $S2 = "
SELECTED"; }
elsif ($billing{cardtype} =~ /Discover/i) { $S3 = "
SELECTED"; }
elsif ($billing{cardtype} =~ /American Express/i) { $S4 = "
SELECTED"; }
else { $Default = "
SELECTED"; }

print "Content-type: text/html\n";
#print "Cache-Control: max-age=300, must-revalidate\n";
print "\n";

print qq~

<html>
<head>
<title>PC Power System Co-op Order for $name (ID #$pin)</title>
<link rel="stylesheet" type="text/css" href="condensed_tour.css">

</head>

<body marginheight="0" marginwidth="0" leftmargin="15" topmargin="25"
rightmargin="15" background="$secureaddress/images/teaser_bg.gif">

<FORM METHOD="POST" ACTION="coopreview.cgi">
<input type="hidden" name="id" value="$id">

<!-- Start Top Border Table -->
<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr><td align="left" valign="top" width="18" height="18"><img
src="$secureaddress/images/bt_t_l.gif" width="18" height="18" align="Top"
border="0"></td><td align="left" valign="top" width="100%" height="18"
bgcolor="#FFFFFF"><font size="-2"> </font></td><td align="left"
valign="top" width="18" height="18"><img
src="$secureaddress/images/bt_t_r.gif" width="18" height="18" align="Top"
border="0"></td></tr>
</table>
<!-- End Top Border Table -->

<!-- Start Main Table -->
<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#FFFFFF">
<tr><td valign="top">

<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_l.gif" width="15" height="15"
border="0"></td><td width=100% rowspan="3" valign="middle">

<div class=heading2>PC Power System Co-op Order</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_r.gif" width="15" height="15"
border="0"></td>
</tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_l.gif"
width="15" height="15" border="0"></td><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_r.gif"
width="15" height="15" border="0"></td></tr></table>

<div><font face="Arial" color=6E030E>
<img src="$secureaddress/images/ssl_stamp.gif" width="130" height="85"
align="Right" border="0">
<b>This is a "secure" server. Your credit card information is
protected!</b></font>

<p>If you are using Netscape, the image of a locked padlock indicates that
any information
you send is protected by world-class encryption techniques from
unauthorized viewing. The same applies to Internet Explorer, where you
will see the image of a locked padlock on your browser window indicating a
secure browsing session that cannot be intercepted or decrypted.

<p align="center">
<font size="5"><b><i>We accept...   </i></b></font>
<img src="$secureaddress/images/visa.gif" align=absmiddle width="49"
height="32" alt="VISA">  
<img src="$secureaddress/images/mc.gif" align=absmiddle width="49"
height="32" alt="MasterCard">  
<img src="$secureaddress/images/discover.gif" align=absmiddle width="49"
height="32" alt="Discover">  
<img src="$secureaddress/images/amex.gif" align=absmiddle width="49"
height="32" alt="AmEx">

<div class=heading1> </div>

<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_l.gif" width="15" height="15"
border="0"></td><td width=100% rowspan="3" valign="middle">

<div class=heading2a>Billing Information</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_r.gif" width="15" height="15"
border="0"></td></tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_l.gif"
width="15" height="15" border="0"></td><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_r.gif"
width="15" height="15" border="0"></td></tr></table>

<div class=heading1> </div>

<table width="90%" align="center" cellpadding="5" bordercolor="#E4E4CB"
cellspacing="5" border="1">
<tr>
<td align="right" width="35%"><font face="Arial">Description:</td><td
width="1%"></td>
<td width="64%"><font face="Arial"><b>PC Power System Co-op</b></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Amount:</td><td
width="1%"></td>
<td width="64%"><font face="Arial"><b>\$$amount</b></td>
</tr>
</table>

<div class=heading1> </div>

<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_l.gif" width="15" height="15"
border="0"></td><td width=100% rowspan="3" valign="middle">

<div class=heading2a>Credit Card Information</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_r.gif" width="15" height="15"
border="0"></td></tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_l.gif"
width="15" height="15" border="0"></td><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_r.gif"
width="15" height="15" border="0"></td></tr></table>

<div class=heading1> </div>

<table width="90%" align="center" cellpadding="5" bordercolor="#E4E4CB"
cellspacing="5" border="1"><tr>
<td align="right" width="35%"><font face="Arial">Credit Card:</td><td
width="1%"></td>
<td width="64%"><select name="cardtype" size="1">
<option VALUE="" $Default>Choose One:</option>
<option VALUE="Visa" $S1>Visa</option>
<option VALUE="MasterCard" $S2>MasterCard</option>
<option VALUE="Discover" $S3>Discover</option>
<option VALUE="American Express" $S4>American Express</option>
</select></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Card Number:</td><td
width="1%"></td>
<td width="64%"><input name="cardnum" type=text size=18 maxsize=16
value="$cardnum"> <font face=Arial size=2 color=6E030E>(No hyphens or
spaces)</font></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Expiration Date:</td><td
width="1%"></td>
<td width="64%"><input name="expdate" type=text size=5 maxsize=4
value="$expdate"> <font face=Arial size=2 color=6E030E>(mmyy)</font></td>
</tr>
</table>

<div class=heading1> </div>

<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_l.gif" width="15" height="15"
border="0"></td><td width=100% rowspan="3" valign="middle">

<div class=heading2a>Customer Information</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_r.gif" width="15" height="15"
border="0"></td></tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_l.gif"
width="15" height="15" border="0"></td><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_r.gif"
width="15" height="15" border="0"></td></tr></table>

<div class=heading1> </div>

<table width="90%" align="center" cellpadding="5" bordercolor="#E4E4CB"
cellspacing="5" border="1"><tr>
<td align="right" width="35%"><font face="Arial">Customer ID:</td><td
width="1%"></td>
<td width="64%"><font face="Arial">$id</td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Name:</td><td
width="1%"></td>
<td width="64%"><input name="name" type=text size=25 maxsize=25
value="$name"> <font face=Arial size=2 color=6E030E>(As it appears on
Credit Card)</font></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Email address:</td><td
width="1%"></td>
<td width="64%"><input name="email" type=text size=25 maxsize=50
value="$email"></td>
</tr>

#######################################################################
# Here is where I would like to insert the same drop down menu as I
# have done with the php code. I would like to be able to display
# the number of Power Legs the member is currently building ('maxtr'
# which I will pull from the database) in a drop down menu so that the
# member can select which Power Leg they would like their traffic to go
# to.
#
# I know I could set up a drop down menu with a static number of Power
# Legs, but I don't want to do that because of the confusion it will
# cause. It will give members the opportunity to accidentally
# select a Power leg that they may not have already set up. And that
# would not be good.
#
# I am fairly comfortable doing everything I need to do EXCEPT for
# the fact that I do not know how to display a 'dynamic' drop down
# menu. Any help you could provide would be greatly appreciated
#
# Thanks in advance - *NAME-DELETED*
#######################################################################

<tr>
<td align="right" width="35%"><font face="Arial">PowerLeg to send Tour
Takers:</td><td width="1%"></td>
<td width="64%"><select name="tropn"><option>1<td>
</tr>

####################################################################
# I don't need any help below here
####################################################################

</table>

<div class=heading1> </div>

<table width="95%" cellpadding="0" cellspacing="0" border="0"
align="center" bgcolor="#E4E4CB">
<tr><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_l.gif" width="15" height="15"
border="0"></td><td width=100% rowspan="3" valign="middle">

<div class=heading2a>Billing Address</div>

</td><td align="left" valign="top" width="15" height="15"><img
src="$secureaddress/images/t_t_r.gif" width="15" height="15"
border="0"></td></tr><tr><td><font size="-5"> </font></td><td><font
size="-5"> </font></td></tr><tr><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_l.gif"
width="15" height="15" border="0"></td><td align="left" valign="bottom"
width="15" height="15"><img src="$secureaddress/images/t_b_r.gif"
width="15" height="15" border="0"></td></tr></table>

<div class=heading1> </div>

<table width="90%" align="center" cellpadding="5" bordercolor="#E4E4CB"
cellspacing="5" border="1"><tr>
<td align="right" width="35%"><font face="Arial">Address:</td><td
width="1%"></td>
<td width="64%"><input name="address" type=text size=25 maxsize=50
value="$address"></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">City:</td><td
width="1%"></td>
<td width="64%"><input name="city" type=text size=25 maxsize=25
value="$city"></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">State:</td><td
width="1%"></td>
<td width="64%"><select name="state" size="1">
<option value="$state" $Default>$state</option>
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregon">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
<option value="District of Columbia">District of Columbia</option>
<option value="Outside USA">Outside USA</option>
</select> <font face=Arial size=2 color=6E030E>(Non US residents select
"Outside USA")</font></td>
</tr>
<tr>
<td align="right" width="35%"><font face="Arial">Zip:</td><td
width="1%"></td>
<td width="64%"><input name="zip" type=text size=10 maxsize=10
value="$zip"></td>
</tr>
</table>

<table width="90%" align="center" border="0"><tr><td>
<div><input type="checkbox" name="authorize" value=1><i>"I hereby
authorize my credit card to be charged
<B>\$$amount</B> for the <span>PC Power System™</span> Co-op. I
understand that my statement will reflect
billing by <B>$MerchantName</B> and that this sale is final and
<b>non-refundable</b>.</font>."</i></td>
</tr>
</table>

<div class=heading2a><input type=submit name=submit value="Place Order">
<input type=reset></div>

</form>

<div class="heading5d">Copyright 2000 - 2001 © PC POWER SYSTEM - All
rights reserved</div>

</td></tr></table>
<!-- End Main Table -->

<!-- Start Bottom Border Table -->
<table width="95%" height="18" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr><td align="left" valign="top" width="18" height="18"><img
src="$secureaddress/images/bt_b_l.gif" width="18" height="18" align="Top"
border="0"></td><td align="left" valign="top" width="100%" height="18"
bgcolor="#FFFFFF"><font size="-2"> </font></td><td align="left"
valign="top" width="18" height="18"><img
src="$secureaddress/images/bt_b_r.gif" width="18" height="18" align="Top"
border="0"></td></tr></table>
<!-- End Bottom Border Table -->
<br>
</body>
</html>

~;

return;
}

##########################################################################
###
# End of program
##########################################################################
###

A:
You can not mix the Perl code with the HTML in Perl as you can in PHP and
that causes the issue for you. What you can do is to create the piece of
content before the printing and then use the variable in your string that
you print out. For example:

$ddownmenu = qq~
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<input type="hidden" name="Action" value="Edit">

~;
while ($myrow = mysql_fetch_array($result)) {

$ddownmenu .= qq~
<table width="250" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr>
<td align=left><font size=-1>Select Power Leg</td>
<td><select name="tropn"><option>1<br>
~;
for ($i=2; $i <= $myrow['maxtr']; $i++) {

$ddownmenu .= qq~
<option>$i<br>
~;
}

$ddownmenu .= qq~
</select>
</td>
</tr>
</table>
<p align="center"><input type=submit name=Submit value="Select
Power Leg"> <input type=reset>
</form>

~;

regards,
Peter

[ back to toc ]