Jump to content

Recommended Posts

although im using nl2br, and \r\n in the code below, my results are still displaying;

 

result1,result2,result2

 

opposed to

 

result1

result2

result3

 

How can i change to this format?

 

$code = $_GET['postcode'];
$message = $_GET['message'];
$shortcode = substr($code,0,2);
$subject = "subject here";
$result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3")
or die(mysql_error());  
echo "<h2>Business Names:</h2>";
$number_of_results = mysql_num_rows($result);
$results_counter = 0;
if ($number_of_results != 0) 
{while ($array = mysql_fetch_array($result)) 
{$email = $array['email'];
$results_counter++;
if ($results_counter >= $number_of_results) {$to .= $email;} 
else {$to .= $email . ',';}}}
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$message .= "\r\n". $to ;
echo nl2br ($message);
mail( "$to", "$subject","$message", "$headers");
echo "<br>" . "Thank you for using our mail form.";
?></body></html>

Link to comment
https://forums.phpfreaks.com/topic/227456-rn-and-nl2br-dont-seem-to-be-working/
Share on other sites

Hi guys, apologies for not including my code last time round.

 

My email;

 

Using html message - 21:11 with strng replace message on line 18

[email protected],[email protected],[email protected]

 

My html output:

<html><body><h2>Business Names:</h2>Using html message -  21:11 with str replace message on line 18<br />
[email protected],[email protected],[email protected]<br>Thank you for using our mail form.</body></html>

 

My php code:

$code = $_GET['postcode'];
$message = $_GET['message'];
$shortcode = substr($code,0,2);
$subject = "subject here";
$result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3")or die(mysql_error());  
echo "<h2>Business Names:</h2>";
$number_of_results = mysql_num_rows($result);
$results_counter = 0;
if ($number_of_results != 0) {while ($array = mysql_fetch_array($result)) {$email = $array['email'];
$results_counter++;
if ($results_counter >= $number_of_results) {$to .= $email;} 
else {$to .= $email . ',';}}}
$headers = 'From: [email protected]' . "\r\n" .    'Reply-To: [email protected]' . "\r\n" .    'X-Mailer: PHP/' . phpversion();
$message .= "\r\n". $to ;echo nl2br ($message);
$variable = str_replace("<br>", "\r\n", $variable);
mail( '$to', '$subject','$message', '$headers');
echo "<br>" . "Thank you for using our mail form.";
?></body></html>

 

I have no error messages to give you. Where I have put $variable = str_replace("<br>", "\r\n", $variable), i have tried swapping $variable for $message, $to, Email, $row etc. etc. but with no change.

 

I have also tried swapping the line;

 

 

else {$to .= $email . ',';}}}

to

else {$to .= $email . "<br>";}}}

and

else {$to .= $email . \r\n;}}}

 

both of which output the html how I would like it, but at the same time prevent the email from reaching me (again no error messages)

That is one confusing post... $result, $message, $variable; Which is it?.

 

$message(before nl2br) => \r\[email protected],[email protected],[email protected]

echo nl2br ($message);

$message(after nl2br) => <br />[email protected],[email protected],[email protected]

which you say is what you get. And it's correct

 

mail( '$to', '$subject','$message', '$headers');

Double quotes will work but you don't need then (see blew's earlier post)

Single quotes, on the other hand, will NOT pass the variables but the string '$to' instead. Get rid of the quotes.

 

Nedal,

Im sorry if I wasnt clear.  I was trying to explain that on different occassions I have tried to use each of the variables mentioned seperately,

within the str_replace. and that neither of the attempts made a difference to my output.

 

Im trying to get my sql results to display in a column rather than displaying on the same line.

I made some minor(?) changes to your code and fixed up the formating:

<?php
$code = $_GET['postcode'];
$message = $_GET['message'];
$shortcode = substr($code,0,2);
$subject = "subject here";
$q = "SELECT email FROM treesurgeons WHERE postcode like '%{$shortcode}%' ORDER BY companyName LIMIT 3";
$result = mysql_query($q)or die("Problem with the query: $q<br>" . mysql_error());
echo "<h2>Business Names:</h2>";
$email = array();
while ($array = mysql_fetch_array($result)){
$email[] = $array['email'];
}
$to = implode(',',$email);
$headers = "From: [email protected]\r\nReply-To: [email protected]\r\nX-Mailer: PHP/" . phpversion();
$message .= "\r\n{$to}";
echo nl2br($message);
mail($to, $subject, $message, $headers);
echo "<br>" . "Thank you for using our mail form.";
?>
</body></html>

 

You say

Im trying to get my sql results to display in a column rather than displaying on the same line.

The only mysql results I see in your code are the email addresses. Did you leave something out?

 

Ken

if ($results_counter >= $number_of_results) {$to .= $email;}

else {$to .= $email . ',';}

 

so...

$to => 'email1,email2,email3';

 

thus:

$variable = str_replace(',', '<br>', $to); // for HTML display
-or-
$variable = str_replace(',', "\n", $to);  // to include in an email message, BUT not the email to:

Hello, thankyou for your suggestion, but for some reason, this stil isnt working. 

I have tried;

 

$variable = str_replace(',', '<br>', $to); 

and

$variable = str_replace(',', "\n", $to);

 

I have also tried placing these lines of code below...  echo "<h2>Business Names:</h2>";

 

My code now;

 

$code = $_GET['postcode'];
$message = $_GET['message'];
$shortcode = substr($code,0,2);
$subject = "subject here";
$result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3")
or die(mysql_error());  
echo "<h2>Business Names:</h2>";
$number_of_results = mysql_num_rows($result);
$results_counter = 0;
if ($number_of_results != 0) 
{while ($array = mysql_fetch_array($result)) 
{$email = $array['email'];
$results_counter++;
if ($results_counter >= $number_of_results) {$to .= $email;} 
else {$to .= $email . ',';}}}
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$variable = str_replace(',', '<br>', $to); 
// for HTML display-or- $variable = str_replace(',', "\n", $to);  // to include in an email message, BUT not the email to:
$message .= "\r\n". $to ;
echo nl2br ($message);
mail( "$to", "$subject","$message", "$headers");
echo "<br>" . "Thank you for using our mail form.";
?></body></html>

 

In all circumstances there has been no difference in my html output or within my email.

 

Im guessing that im puting this code in the wrong place !?!

You are putting the results into a variable you never display.

 

If you use my code, modify it as follows:

<?php
$code = $_GET['postcode'];
$message = $_GET['message'];
$shortcode = substr($code,0,2);
$subject = "subject here";
$q = "SELECT email FROM treesurgeons WHERE postcode like '%{$shortcode}%' ORDER BY companyName LIMIT 3";
$result = mysql_query($q)or die("Problem with the query: $q<br>" . mysql_error());
echo "<h2>Business Names:</h2>";
$email = array();
while ($array = mysql_fetch_array($result)){
$email[] = $array['email'];
}
$to = implode(',',$email);
$headers = "From: [email protected]\r\nReply-To: [email protected]\r\nX-Mailer: PHP/" . phpversion();
$message .= "<br>" . implode("<br>",$email) . "<br>";   // line modified
// echo nl2br($message);                                                  // line removed
mail($to, $subject, $message, $headers);
echo "<br>" . "Thank you for using our mail form.";
?>
</body></html>

 

Ken

Hi Ken, thankyou for showing me your code,

the html output is;

<html><body><h2>Business Names:</h2><br>Thank you for using our mail form.</body></html>

 

and the email message reads;

 

message here code

<br>[email protected]<br>[email protected]<br>[email protected]<br>

 

maybe I could change the code somewhere along the line?

 

i was thinking maybe I could do something like,

 

case 0:$value1 = $row['Email'];

break;

case 1:$value2 =$row['Email'];

break;

case 2:$value3 =$row['Email'];

break;

 

and have each sql value as a unique variable.  Do you think this would be easier to work with?

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.