Welcome to WebmasterWorld Guest from 100.25.214.89

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How Do You Put a While Loop into a Variable?

     
9:37 pm on Sep 27, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


I want to provide visitors the option to e-mail the coupons on my web site to their friends. However, I'm completely stuck! Can anyone help, please? Here's what I've done . . .

The coupons are selected from a MySQL database and echoed in a while loop, thus:

while($row = mysql_fetch_assoc($result)) {

echo "<table>";
echo "<tr>";
echo "<td rowspan=\"2\"><img src=\"images/" . $row['advlogo'] . "\" alt=\"\" /></td>";
echo "<td><a href=\"" . $row['gotourl'] . "\">" . $row['anch'] . "</a><br />";
echo (empty($row['code']) ? $row['instr'] : $row['code']);
echo "<br /> Expires: ";
echo (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
echo "<br /> About <a href=\"" . $row['store'] . "\">" . $row['adv'] . "</a><br />";
echo "Category: <a href=\"" . $row['category'] . "\">" . $row['cat'] . "</a></td>";
echo "<td rowspan=\"2\"><a href=\"" . $row['gotourl'] . "\">" . $row['blockanch'] . "</a></td>";
echo "</tr>";
echo "<tr>";
echo "<td>

<form action=\"email-coupons.php?id=" . $row['id'] ."\" method=\"post\">
<input type=\"image\" src=\"images/emailcpn.png\" />
</form>

</td>";
echo "</tr>";
echo "</table>";

} // end while

Note the form towards the end. It lets me post coupon IDs to email-coupons.php.

On email-coupons.php, I get the ID, select the coupon and, as before, echo it in a while loop. I also ask for the To and From e-mail addresses, thus:

<?php

$id = intval($_GET['id']);
$conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);

$select = "SELECT
adv,
instr,
code,
bmurl,
anch,
DATE_FORMAT(expdate, '%M %D, %Y') AS formated_date,
expun
FROM tadv, tinstr, tc
LEFT JOIN texpun on tc.expunid=texpun.expunid
WHERE
tc.instrid=tinstr.instrid AND
tc.advid=tadv.advid AND
tc.id=$id";

$query = mysql_query($select);

while($row = mysql_fetch_assoc($query)) {
echo "<table>;
echo "<tr>";
echo "<td>";
echo "Offer: <a href=\"" . $row['bmurl'] . "\">" . $row['anch'] . "</a><br />";
echo "Store: " . $row['adv'];
echo "<br /> Expires: ";
echo (empty($row['formated_date']) ? $row['expun'] : $row['formated_date']);
echo "<br /> Instructions: ";
echo (empty($row['code']) ? $row['instr'] : $row['code']);
echo "</td>";
echo "</tr>";
ech "</table>";

echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
echo "Send to:<br />";
echo "<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea>";
echo "From:<br />";
echo "<input size=\"25\" name=\"from\" />";
echo "<input type=\"submit\" name=\"send\" value=\"Send\" />";
echo "</form>";
}

?>

This configuration lets me send the To and From e-mail addresses to email-coupons-manager.php - but not the coupon itself! How can I put the coupon into a variable to send it, keeping in mind that it's echoed in a while loop?

Here's what I've got on email-coupons-manager.php:

<?php

$to = $_POST['to'];
$from = $_POST['from'];
$header = "From: $from";
$subject = "Check Out this Coupon!";

if ($to == '') {print "<p>You have not entered the recipient's e-mail.</p>";}

elseif ($from == '') {print "<p>You have not entered your e-mail.</p>";}

else {

$send = mail($to, $from, $header, $subject);

if($send) {print "<p>Thank you!</p>";}

else {print "<p>An error occurred.</p>";}
}

?>

2:49 am on Sept 28, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


The best thing to do would be to pass the coupon ID to email-coupons-manager.php and get the coupon out of the DB again. A hidden form field in email-coupons.php is a good way to do this.

echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"id\" value=\"".$row['coupon_id']."\">"; // This line
echo "Send to:<br />";
echo "<textarea name=\"to\" rows=\"1\" cols=\"35\"></textarea>";
echo "From:<br />";
echo "<input size=\"25\" name=\"from\" />";
echo "<input type=\"submit\" name=\"send\" value=\"Send\" />";
echo "</form>";

Then in email-coupons-manager.php take $_POST['id'] and load the coupon info just as you do in the other pages.

3:25 pm on Sept 29, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 21, 2008
posts:139
votes: 1


Thanks for the response, MattAU!

I managed to put the loop output into a string instead of echoing it. I then used your method to pass the string to the next page.

Thanks for the help! This problem's resolved.

8:34 pm on Sept 29, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


Glad I could help :)

If you're going to put HTML in the value of a hidden form tag, I recommend that you encode it first. This will stop browsers getting confused, displaying things wrong and giving you unexpected results when the form is posted. base64_encode is good for this.

echo '<input type="hidden" name="my_string" value="'.base64_encode($my_string).'">';

Then on the processing page:

$my_string = base64_decode($_POST['my_string']);

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members