I know this is probably a small thing but I just noticed this recently. I have an admin page to submit (update) the body of text. Now it keeps the spaces and new paragraphs but on the actual webpage it is not showing the needed spaces.

I looked into the database and the spaces are there...

I update them using this..

$newsBody = mysql_real_escape_string($_POST['newsBody']);

Do i need something for the spaces to be put back in on the webpage people will be viewing?

Thank you...

    You can use [man]nl2br[/man] if you just want to convert newlines to <br> tags. If you want to keep the exact spacing as well, you could instead simply wrap the output in <pre>...</pre> HTML tags.

      Ok, I orginially did the <pre></pre> and what happened was the spaces did infact come up but it blew my site up because it didnt wrap....

      I used to use the Nl2br a few months back but someone who was helping me on previous code said that using mysql_real_escape_string($_POST['newsBody']); was the same function just newer... I now assume its wrong lol...

      So i did this code up and yet get the same outcome with no spaces for output.

      if ("Update The Newsletter!" == $HTTP_POST_VARS["submit"]) {
      	 $imageName = mysql_real_escape_string($_POST['imageName']);
      	 $newsBody1 = mysql_real_escape_string($_POST['newsBody']);
      	 $newsBody = nl2br($newsBody1);
      	 $title = mysql_real_escape_string($_POST['title']);
      
       $sql = "UPDATE news SET image = '$imageName', text = '$newsBody', title = '$title' WHERE id='1001'";
       $result = mysql_query($sql);
       }

      I never took out

      $newsBody1 = the mysql_real_escape_string($_POST['newsBody']);

      giving me....

      if ("Update The Newsletter!" == $HTTP_POST_VARS["submit"]) {
      	 $imageName = mysql_real_escape_string($_POST['imageName']);
      	 $newsBody1 = $_POST['newsBody'];
      	 $newsBody = nl2br($newsBody1);
      	 $title = mysql_real_escape_string($_POST['title']);
      
       $sql = "UPDATE news SET image = '$imageName', text = '$newsBody', title = '$title' WHERE id='1001'";
       $result = mysql_query($sql);
       }

      but again same outcome... with no spaces... what am i doing wrong?

        zrocker wrote:

        I used to use the Nl2br a few months back but someone who was helping me on previous code said that using mysql_real_escape_string($_POST['newsBody']); was the same function just newer... I now assume its wrong lol...

        Totally wrong. Compare [man]nl2br[/man] with [man]mysql_real_escape_string[/man].

          Alright I understand the difference now but i cant locate how i have used nl2br before... I have read over the pages you referred me too. My question i know is not hard to answer. But do you apply the nl2br to the output (website) or the input (database)... I have tried both and still dont get the needed spaces... I get those spaces and new lines in the Form they fill out in the admin part but not in the output area (website)...

          Can someone please take the code i have an help me with this... Like i said i have this code in some of my older files but can not locate them... Thank you for your help...

            mysql_real_escape_string(): For escaping data being used in MySQL query to prevent SQL injection errors/attacks. Has no influence on formatting of any text being inserted into DB.

            nl2br(): For massaging text being output to a browser (or other HTML-based client) to convert newline characters to <br> tags. Normally should only be applied at the time the text is being output (echo, print, etc.).

            echo nl2br(htmlentities($textRetrievedFromDatabase));
            

            Multiple consecutive space characters will still be collapsed by the browser just as with any other HTML text unless you either use a <pre> tag (which was not suitable for the text in question, from your earlier reply). You could style it with CSS using white-space: pre-wrap; but unfortunately the "pre-wrap" value is not well supported by existing browsers.

            Without knowing exactly what your formatting needs are, it's hard for me to come up with any other solutions. Probably the most flexible would be to actually input the text with HTML mark-up so that you can control the display via your CSS stylesheet along with default HTML formatting options. This could be done by simply directly entering the HTML, using a BBCode interface (similar to this forum's mark-up tags), or using a "rich text" textarea editor such as TinyMCE et al.

              Thank you Nog for taking the time and explaining that to me. I have felt that I did this with the output but i assume i was doing something wrong but now since your help i was able to do what was needed... I have once done this but just forgot... thanks again!

              Here is what i ended up doing... incase others research this...

              $sql1 = "select * from news where id=1001";
              	$result1 = @mysql_query($sql1);
              	while ( $row = mysql_fetch_array($result1) ) { 
              
              $news1 = $row["text"];
              $news = nl2br($news1); 
              $title = $row["title"]; 
              $date=$row["timestamp"];
              $date_record = date("m/d/y",strtotime($date));
              
              echo"$news";
              }
                Write a Reply...