Problem with displaying formatted text.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Problem with displaying formatted text.

    I use a form to write an article and then save it into the mysql table.

    I use this to save it:

    $Db_profile = safe_sql($_POST['x_profile']);

    and the function safe_sql is:

    Code:
    // Make variable SQL safe
    function safe_sql( $value )
    {
         $value = strip_tags(trim($value));
    		 
        // Stripslashes
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }
        // Quote if not integer
        if (!is_numeric($value)) {
            $value = mysql_real_escape_string($value);
        }
        return $value;
    } // End of Function
    I save it with a normal insert:
    Code:
    $sql = "UPDATE clients SET 
    		 profile   = '$Db_profile',
    		 last_date = '$today'
    
    		 WHERE user_id = \"{$_SESSION['expert']}\" ";
    	
    		 mysql_query($sql) or die("could not execute PROFILE update query". mysql_error());
    When I come to display the article all the sentences just get pushed together,the line breaks have al gone.

    I use this to display it:

    Code:
    <div class="hot04">
     <p><?php echo "$profile"; ?></p>
    </div> <!-- End div:hot04 -->

    The strange thing is, when I look at the table data it still contains the line breaks so it must be my display method that is causing the problem.

    What am I doing wrong ?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    HTML won't take a newline character (\n) and create a new line with it - it will in the source code, but not the live code. You need to convert these into a html tag (<br />). Luckily, there is a standard PHP function that does this for you (nl2br()). Just simply use this function on your output.

    Code:
    $text = "These\n will\n appear\n on\n new\n lines";
    
    echo nl2br($text);

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Hi Marcus,

      Thanks,

      So I can just do:

      <p><?php echo "nl2br($profile )"; ?></p>

      or maybe a 2 step would be better

      $Sh_profile = nl2br($profile) ;

      later
      <p><?php echo "$Sh_profil e"; ?></p>

      Do I need the double quotes ?
      ( I am never quite sure )

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the first code won't work because of the quotation marks (PHP doesn't look for functions in a string)
        in the third code you can omit the quotation marks, because all you want to print is the variable.

        which of the two ways you implement depends on your personal liking.

        Originally posted by jeddiki
        Do I need the double quotes ? (I am never quite sure )
        If you're not sure about something, refer to the PHP manual, it usually gives good explanation and plenty of examples.
        PHP: echo - Manual

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Markus (with a K) ;]

          Double quotes: functions will not be parsed inside them, variables, however, will be parsed.

          So, the second option you show will work.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            note:
            Code:
            echo myFunction();
            only works, if the function has a (printable) return value.

            Comment

            • jeddiki
              Contributor
              • Jan 2009
              • 290

              #7
              Thanks for your help,

              So it seems that the textarea and the mysql dtatbase both like
              the line break /n/r style and will record them invisibly,
              but the HTML page needs to have these output converted <br> or <br />

              One more this that is confusing me.
              When using the texarea to write an article how can I add bold
              and italics so that they will save in the database and then
              display correctly when I output them to an HTML page ?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by jeddiki
                When using the texarea to write an article how can I add bold and italics so that they will save in the database and then display correctly when I output them to an HTML page ?
                probably the best is to add some tags, that will be converted to the appropriate HTML tags (be it <span>, <b> or <i>). make sure you sanitize your input before putting it to DB or the HTML page.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by jeddiki
                  One more this that is confusing me.
                  When using the texarea to write an article how can I add bold
                  and italics so that they will save in the database and then
                  display correctly when I output them to an HTML page ?
                  Check out BBCode.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by Markus
                    Check out BBCode.
                    note: PHP provides an extension to parse BBCode.

                    Comment

                    • jeddiki
                      Contributor
                      • Jan 2009
                      • 290

                      #11
                      Thanks, I am checking out how I can get the bbCode extension for my server.

                      Looks like what I need :)

                      Comment

                      Working...