Loop inside Mail htmlyBody

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Loop inside Mail htmlyBody

Post by sHuRuLuNi »

Hello,

Sorry if this has been answered somewhere, my head is just about to explode and I can't think straight anymore ... :)

I have a loop, which brings up Multiple Questions and Answers on the webpage just fine. The process then goes on by sending an email confirmation to the user of his entered data.
While fields like Name, Company, etc. are shown just fine in the email (html mail) I can't figure out how to show the multiple questions and answers in the email, and my problems lies with the fact that in the htmlBody of the mail I use replace, to replace keywords like {name} with the content of $name.

But since Questions and answers are multiple, then I would have to replace a keyword like {questions} with a php loop - and that doesn't work in the htmlBody.

here is an example of the loop on the webpage:

Code: Select all

<?php while($row = mysql_fetch_array($question, MYSQL_ASSOC)) { ?>

<?php if($row["qu_type"] == 3){ ?>
		<tr class='bg-text'>
		  <td align="right">&nbsp;</td>
		  <td align="left">&nbsp;</td>
		  <td align="left"><?php echo $row["qu_text"]; ?></td>
		</tr>

Now, here is an example of the htmlBody:

Code: Select all

 <tr class="text">
				                  <td align="left" width="20%">First Name</td>
				                  <td align="left" width="5%">:</td>
				                <td align="left"><strong>{first}</strong></td>                                </tr>
The keyword {first} of course pulls the content from here:

Code: Select all

$htmlBody= str_replace('{first}', $firstname, $htmlBody);
Is it possible to "pack" a whole loop in something like $loop? Because then of course I would just do "$htmlBody= str_replace('{loop}', $loop, $htmlBody);"
But this doesn't seem to work.


Basically, on the webpage the Questions and Answers are shown just fine, like:

Question 1: Answer 1
Question 2: Answer a, b, c (mutliple checkbox)
Question 3: Answer xyz

And I would just like to have this whole part to show up exactly like that in the email.


Can anyone help me here?

Sorry for my ignorance.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Loop inside Mail htmlyBody

Post by Neilos »

I don't fully understand your question.

Am I right in thinking that you just want to create a list of questions and answers to send in an email to the user. If so then what I would do is;

Create an array of the questions and an array of the answers, then use a foreach loop to iterate through the arrays that bulid a string that you then use in the emails content.

Would that work for you?
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Re: Loop inside Mail htmlyBody

Post by sHuRuLuNi »

Neilos wrote:I don't fully understand your question.

Am I right in thinking that you just want to create a list of questions and answers to send in an email to the user. If so then what I would do is;
Well, it is so: The user comes to the web page, he fills out his personal data, then there is a part with "Bookings", here are some Questions like:

"What is your food preference?"
He can then answer (mutliple checkbox):
1. Vegetarian
2. Kosher
3. No fish

Then there is e.g. another question:

"How old are you?"
Answers:
1. 20-30
2. 31-40
3. 40+

So, he now clicks "Vegetarian" and "31-40" and then on the button "Submit". The form is submitted, the user lands on a "Confirmation page" where his data is shown incl. his Bookings.
At this moment, an email Confirmation is sent to the user, which also contains his data, but NOT his bookings, I can manage to show 1 booking, but not all of them. Since, to show all of them, I would have to use some kind of loop (similar to what is used when the bookings are shown in the Webpage Confirmation.
But I can't "pack" this whole loop into a $something, which is NEEDED to use with replace and a keyword like {something} in the html mail body. I can't put the loop code DIRECTLY in the html mail body, because this is already "inside" a $hmtlBody.

Here below you have the code to what I mean:


The Confirmation Webpage where the Qustions/Answers booked by the user are shown correctly:

Code: Select all

                <tr class="bg-text">
                  <td align="right">Street</td>
                  <td align="left">
                <td align="left"><?php echo mysql_result($quser, 0,"ad_street"); ?>                                </tr>
                <tr class="bg-text">


<?php

$question = mysql_query("SELECT * FROM question WHERE invite_in_id=".$in_id." ORDER BY qu_order");

if(mysql_num_rows($question) > 0){
?>
                <tr class="bg-text">
                  <td align="right">&nbsp;</td>
                  <td align="right">&nbsp;</td>
                  <td align="right">&nbsp;</td>
                </tr>
<?php
}
?>

<?php while($row = mysql_fetch_array($question, MYSQL_ASSOC)) { ?>

<?php if($row["qu_type"] == 3){ ?>
		<tr class='bg-text'>
		  <td align="right">&nbsp;</td>
		  <td align="left">&nbsp;</td>
		  <td align="left"><?php echo $row["qu_text"]; ?></td>
		</tr>
<?php }else{?>
		<tr class='bg-text'>
		  <td align="right">Answers</td>
		  <td align="left">&nbsp;</td>
		  <td align="left"><?php echo $row["qu_text"]; ?></td>
		</tr>

<?php
}

if($row["qu_type"] == 0 || $row["qu_type"] == 1){

$answers = mysql_query("SELECT * FROM answer WHERE question_qu_id=".$row["qu_id"]." AND addressbook_ad_id=".$ad_id);

if(mysql_num_rows($answers) > 0){

$antext = mysql_result($answers, 0,"an_text");

?>
		<tr class='bg-text'>
		  <td align="right"></td>
		  <td align="left">&nbsp;</td>
		  <td align="left"><?php if($antext == ""){echo "No answer"; }else{ echo $antext; }   ?></td>
		</tr>
<?php
}else{
?>
		<tr class='bg-text'>
		  <td align="right"></td>
		  <td align="left">&nbsp;</td>
		  <td align="left">No answer</td>
		</tr>
<?php

}

}

if($row["qu_type"] == 2){

$answers = mysql_query("SELECT * FROM answer WHERE question_qu_id=".$row["qu_id"]." AND addressbook_ad_id=".$ad_id);

if(mysql_num_rows($answers) > 0){

?>
		<?php while($ddrow = mysql_fetch_array($answers, MYSQL_ASSOC)){ ?>
		<tr class='bg-text'>
		  <td align="right"></td>
		  <td align="left">&nbsp;</td>
		  <td align="left"><?php echo $ddrow["an_text"]; ?></td>
		</tr>
		<?php } ?>

<?php
}else{
?>
		<tr class='bg-text'>
		  <td align="right"></td>
		  <td align="left">&nbsp;</td>
		  <td align="left">No answer</td>
		</tr>
<?php
}
}
}
?>

And here is where the html mail is created:

Code: Select all

$htmlBody = '
<html>
<head>
<title>www.xxxxxxxxxxxxx</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<style type="text/css">

....

                              <tr class="text">
				                  <td align="left" width="20%">First name</td>
				                  <td align="left" width="5%">:</td>
				                <td align="left"><strong>{first}</strong></td>                                </tr>
				                <tr class="text">
				                  <td align="left" width="20%">Last name</td>
				                  <td align="left" width="5%">:</td>
				                <td align="left"><strong>{last} </strong></td>                               </tr>
				                <tr class="text">
				                  <td align="left" width="20%">Company</td>
				                  <td align="left" width="5%">:</td>
				                <td align="left"><strong>{firma}</strong></td>                                </tr>';

Now, under "Company" I need a keyword like {bookings} which will show the list of Questions with the answer that the user chose when he filled the form, like:

Q1: Answer 1
Q2: Answer 2
...

The html mail loop is generated with this code:

Code: Select all

/* ########################## MAILLOOP ############################ */
$emailid = $st_id;

...

$htmlBody= str_replace('{first}', $firstname, $htmlBody);
$htmlBody= str_replace('{last}', $name, $htmlBody);
$htmlBody= str_replace('{firma}', $company, $htmlBody);
I need here replace statement for the whole loop which pulls out the questions and answers which the user chose. I need to "pack" this into a $bookings, which then I would use here:

Code: Select all

$htmlBody= str_replace('{bookings}', $bookings, $htmlBody);
I would then put the keyword {bookings} in the code above where all keywords are used, so that the bookings may show.


Sorry if I am failing to describe this correctly. I am fairly good with editing a lot of php code, but not writing myself much.
I am trying.
Last edited by sHuRuLuNi on Tue Jan 04, 2011 4:37 am, edited 1 time in total.
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Re: Loop inside Mail htmlyBody

Post by sHuRuLuNi »

I have now tried as you suggested to do this with a "foreach" statement, but, nope, I can't do it.

I tried this:

Code: Select all

$question = mysql_query("SELECT * FROM question WHERE invite_in_id=".$in_id." ORDER BY qu_order");
while($row = mysql_fetch_array($question, MYSQL_ASSOC)) {
$qlist = $row["qu_text"];
}

$qlist;
$qlist = $antext;

foreach($qlist as $qname => $aname){
	$bookings = "$qname: $aname <br />";
}
But I guess, this is not constructed well, since either I get this in the email:

qu_text:

or I get nothing or only the last Booking (e.g.: "Question 3: Answer 3"), depending of what I change in the code above.



I understand what you mean though: I need to take an array of questions and an array of answers, and "pair" all questions with the correct answers then show them with the foreach, or in my case put them in $bookings.

But I don't know how to do that.

And this gets more complicated since there are 3 separate tables for this: question, answer, options.
So questions are saved in "question" while answers are saved in both "answer" and "options", since there are many types of answers (chebox, dropdown, text area ...)
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Re: Loop inside Mail htmlyBody

Post by sHuRuLuNi »

So ...

I now tried to pack the whole code which works and shows questions and answers on the confirmation web page into a function.

And I made a variable $bookings = Bookings();


Then used replace for $bookings as {bookings}


And put the keyword {bookings} in the html mail body, but NOTHING IS SHOWING UP!


Please guys it has been so much time I am trying to solve this!


Thank you.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Loop inside Mail htmlyBody

Post by Neilos »

Woah your posts are long lol, too much information, lets try to boil this down.

Your problem seems to be in constructing arrays and writing the elements into a string to be used with an email. I'll give you a quick example to show this.

If you are storing the questions and answers in mysql tables instead of using the POST variables then this complicates things slightly but it's nothing we can't handle. A good way of matching up the questions with answers is to use associative arrays and have the table titles the same in all tables where they need to correspond;

Code: Select all

$question_pre = mysql_query("SELECT * FROM question WHERE invite_in_id = '$in_id'");
$answer_pre = mysql_query("SELECT * FROM answer WHERE invite_in_id = '$in_id'");
$option_pre = mysql_query("SELECT * FROM option WHERE invite_in_id = '$in_id'");

$questions_ready = mysql_fetch_array($question_pre, MYSQL_ASSOC);
$answer_part = mysql_fetch_array($answer_pre, MYSQL_ASSOC);
$option_part = mysql_fetch_array($option_pre, MYSQL_ASSOC);

$answers_ready = array_merge($answer_part, $option_part);

$string = 'Question 1: ' . $questions_ready['title1'] . ' Answer 1: ' . $answers_ready['title1'] . '<br />';
$string = $string . 'Question 2: ' . $questions_ready['title2'] . ' Answer 2: ' . $answers_ready['title2'] . '<br />';
$string = $string . 'Question 3: ' . $questions_ready['title3'] . ' Answer 3: ' . $answers_ready['title3'];

Now you can use that string to create an HTML formatted email.

Does this help?
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Re: Loop inside Mail htmlyBody

Post by sHuRuLuNi »

Neilos wrote:Woah your posts are long lol, too much information, lets try to boil this down.

Your problem seems to be in constructing arrays and writing the elements into a string to be used with an email. I'll give you a quick example to show this.

If you are storing the questions and answers in mysql tables instead of using the POST variables then this complicates things slightly but it's nothing we can't handle. A good way of matching up the questions with answers is to use associative arrays and have the table titles the same in all tables where they need to correspond;

Code: Select all



$string = 'Question 1: ' . $questions_ready['title1'] . ' Answer 1: ' . $answers_ready['title1'] . '<br />';
$string = $string . 'Question 2: ' . $questions_ready['title2'] . ' Answer 2: ' . $answers_ready['title2'] . '<br />';
$string = $string . 'Question 3: ' . $questions_ready['title3'] . ' Answer 3: ' . $answers_ready['title3'];


See, here is the problem. I don't have a fixed number of questions with each 1 answer.
Basically, the administrator creates a form for registration, and he can insert as many questions as he likes, he can have 3,4,27 or 100 Questions. And each question can have answers in form of checkbox, radio button, selectbox (dropdown), or text area.

Now, I need some kind of "foreach" statement to read all the Answers that the user who just registered has entered.

Also, in your code, only questions are working but not the answers:

I get in the email:

How old are you:
Question 2: Answer 2
Question 3: Answer 3

Forgot this: Since you merged the arrays there, I wanted to tell you that in order for later to use e.g. $row['answer_title'] bear in mind that in the table "answer" the answers are in column "an_text" while in the table "option" these are in the column "op_text".
sHuRuLuNi
Forum Newbie
Posts: 16
Joined: Mon Jan 03, 2011 9:06 am

Re: Loop inside Mail htmlyBody

Post by sHuRuLuNi »

OK I solved it.


I put the whole code which I posted in the first post (which shows the bookings on the webpage) inside the $htmlBody, and I did that by splitting $htmlBody.
Post Reply