Jump to content

[SOLVED] Can You Put a While Loop in a Variable?


Fluoresce

Recommended Posts

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>";}
}

?> 

Link to comment
Share on other sites

Hey, Crayon! Nice to meet you!

 

You're the guy from whom I learnt pagination. I read your tutorial on PHPFreaks.com and tried to apply it to my site. After getting stuck, I e-mailed you, but you didn't respond!  >:(;D

 

Anyway . . . It's OK. I've completed the pagination, but now I'm stuck on this.  :'(

 

Please elaborate on your response, keeping in mind that I'm a new, new, newbie.  :-[

 

 

 

 

Link to comment
Share on other sites

Hmm...well I've always had my @phpfreaks.com email posted but for a while I was not a mod and therefore did not have access to it, so maybe it was during that time.  I'm glad you sorted out the pagination thing.

 

As far as sessions go, here's a quick example:

 

page1.php

<?php
  // tell php you are using sessions.  
  // you must have this on any page you wish
  // to access session variables.  It also has to
  // come before any html output, even blank lines
  session_start();

  // example of setting a session variable
  $_SESSION['name'] = 'Fluoresce';

  // example to go to next page
  echo "<a href = 'page2.php'>click me</a>";
?>

 

page2.php

<?php
  session_start();

  echo $_SESSION['name'];
?>

 

 

Link to comment
Share on other sites

Thanks for the reply, Crayon. However, I can't use sessions, as they will interfere with other PHP tracking codes which I have to have at the top of my pages.

 

Is there no other way in which I can pass the results of a while loop to the next page, where I can send it in a variable with the mail() function?

 

Or, should I just restart the whole thing . . . ? How would you give people the option to e-mail your content to friends, keeping in mind that the content is in a database?

Link to comment
Share on other sites

Not sure exactly what you're asking, but if you're wanting to pass what is echoed in the while loop to something else, then I would suggest you just add all that data as a string to a variable, rather than echoing it. Like this:

 

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

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

 

Then just pass the $str to whatever.

Link to comment
Share on other sites

Hi, F1Fan!

 

Please let me tell you exactly what I want to do, then tell me if your suggestion will work . . .

 

On my site, I have coupons which are echoed in a while loop from a database. Every coupon has an "Email Coupon to a Friend" button, which, if clicked, posts the coupon's ID to email-coupons.php:

 

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>";
}

 

Email-coupons.php gets the ID, selects the coupon, echoes it, and also asks for To and From e-mail addresses:

 

<?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>";
}

?>

 

Note the form at the bottom which asks for the addresses.

 

Now, I have to send the 2 addresses, plus the coupon, to another page which will process the mail() function. However, at the moment, the above code only lets me send the 2 addresses. How can I get the coupon to be sent as well, to be processed by the page/code below?

 

<?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>";}
}

?>

Link to comment
Share on other sites

OK, I understand now. My suggestion will still work. Add what would have been echoed to the variable. Then you can re-use that variable. First, right after the loop, you can echo the variable, which would do the same as echoing everything in the loop. Then, pass the variable to the last page through post by adding a hidden input with the variable as the value. Be sure to use the htmlentities($str,ENT_QUOTES) function to mask any unwanted characters.

Link to comment
Share on other sites

Hi, F1Fan. I really appreciate the help, man!

 

Have I done what you said correctly?

 

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

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

echo $str;

echo "<form action=\"email-coupons-manager.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"id\" value=\"" . $str . "\">"; // 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>";

?>

 

Note that I didn't understand how to do the htmlentities thingy. How do you apply that?

Link to comment
Share on other sites

Yes, you did everything but the htmlentities part. Just do this for that line:

 

echo "<input type=\"hidden\" name=\"id\" value=\"" . htmlentities($str,ENT_QUOTES) . "\">"; // This line 

 

This will replace special characters in $str that could mess some things up with HTML entities. Here's some info on it to better understand it:

http://www.w3schools.com/php/func_string_htmlentities.asp

Link to comment
Share on other sites

Wow, F1Fan! I am very grateful to you, Sir! Thank you very much for all your help!  :)

 

If I may, I just wanted to make sure that I understand one last, quick thing . . .

 

Is the code below correct? Is this how, on the last page, I grab the $str variable and everything else and mail them?

 

<?php

$coupon = $_POST['$str, ENT_QUOTES'];
$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($coupon, $to, $from, $header, $subject);

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

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

?>

Link to comment
Share on other sites

Very close. You named your hidden input "id" so that would need to be the key in the $_POST variable for that item, or $_POST['id'].

 

Also, you'll want to use html_entity_decode() to undo the htmlentities from before. Just replace

$coupon = $_POST['$str, ENT_QUOTES'];

with

$coupon = html_entity_decode($_POST['id'],ENT_QUOTES);

Link to comment
Share on other sites

F1Fan, thank you very much for the help! You've been great!

 

F1Fan -->  8) <--F1Fan

 

I've put the pages/codes together. Tomorrow morning, I'll try them. I can't do so now, as I need some sleep. It's 7:09 am here in London!  :o

 

I'll mark the thread "Solved" as soon as I verify that the scripts work.

 

Thank you, once again.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.