wright67uk Posted February 12, 2011 Share Posted February 12, 2011 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: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\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> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2011 Share Posted February 12, 2011 Is message being sent in the url? Then there are no new lines to convert to br. This function doesn't ADD lines where they don't exist. You'll need to replace every comma with a br or new line. Quote Link to comment Share on other sites More sharing options...
blew Posted February 12, 2011 Share Posted February 12, 2011 try $variable = str_replace("<br>", "\r\n", $variable); and other thing... here: mail( "$to", "$subject","$message", "$headers"); you dont need to put double quotes in the variables... Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 12, 2011 Author Share Posted February 12, 2011 ive used str_replace but nothing changed! ive been staring at this for hours!?!? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2011 Share Posted February 12, 2011 echo $message, what does it say? And post any new code. Quote Link to comment Share on other sites More sharing options...
blew Posted February 13, 2011 Share Posted February 13, 2011 man , is the error in the $message? if so, just use <br>... post all ur code again, including the errors... Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 13, 2011 Author Share Posted February 13, 2011 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 email1@yahoo.co.uk,email2@hotmail.com,email3@hotmail.co.uk My html output: <html><body><h2>Business Names:</h2>Using html message - 21:11 with str replace message on line 18<br /> email1@yahoo.co.uk,email2@hotmail.com,email3@hotmail.co.uk<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: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\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) Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2011 Share Posted February 13, 2011 I assume you are using your real email in the code? Check your junk folder. And turn on error reporting. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 13, 2011 Author Share Posted February 13, 2011 Yep, im using my real email in the code, just changed it for forum use, to stop naughty bots. And yep im checking my junk boxes too. Quote Link to comment Share on other sites More sharing options...
Nedals Posted February 13, 2011 Share Posted February 13, 2011 That is one confusing post... $result, $message, $variable; Which is it?. $message(before nl2br) => \r\nemail1@yahoo.co.uk,email2@hotmail.com,email3@hotmail.co.uk echo nl2br ($message); $message(after nl2br) => <br />email1@yahoo.co.uk,email2@hotmail.com,email3@hotmail.co.uk 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. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 14, 2011 Author Share Posted February 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 14, 2011 Share Posted February 14, 2011 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: me@me.com\r\nReply-To: me@me.com\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 Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 14, 2011 Author Share Posted February 14, 2011 No that was it, I just want to change my email message from email1,email2,email3 to email1 email2 email3 Quote Link to comment Share on other sites More sharing options...
Nedals Posted February 14, 2011 Share Posted February 14, 2011 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: Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 15, 2011 Author Share Posted February 15, 2011 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: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\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 !?! Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 15, 2011 Author Share Posted February 15, 2011 Do you think that I should some how break down each of my results into seperate variables so that they are easier to work with? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2011 Share Posted February 15, 2011 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: me@me.com\r\nReply-To: me@me.com\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 Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 15, 2011 Author Share Posted February 15, 2011 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>email1@1.com<br>email2@2.com<br>email3@3.com<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? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2011 Share Posted February 15, 2011 What do you want to appear on the screen & what should appear in the email? Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.