Jump to content

[SOLVED] nl2br?


Irksome

Recommended Posts

OK it's been a while since I've done any PHP so I'm trying to create a quick news script that contains user, title and text.

 

Now the text field needs to be compatible with paragraphs, but it currently isn't. Everything comes out on one line.

 

I know it's nl2br I need, but I've read through a few tutorials and code snippets but I just can't figure out how to apply it to my script.

 

My code for my addnews.php page is here:

 

<?php
session_start();

include("config.php");

  if($submit)

  {//begin of if($submit).

      $title = mysql_real_escape_string($_POST['title']);
      $user = mysql_real_escape_string($_POST['user']);
      $text2 = mysql_real_escape_string($_POST['text2']);

$user = Trim(stripslashes($_POST['user'])); 
$text2 = Trim(stripslashes($_POST['text2'])); 

// nl2br
$Body = "";
$Body .= "Comments: ";
$Body .= $text2;
$Body .= "\n";

	//run query

         $result = mysql_query("INSERT INTO scnews (title, dtime, user, text2)

                       VALUES ('$title',NOW(),'$user','$text2')",$connect);

          //success message.

          echo "<b>News entry added successfully!<br>";

          echo "<meta http-equiv=Refresh content=0;url=index.php>";

  }//end of if($submit).


else

  {//begin of else

      ?>
      <br>
      <h3>Add news article</h3>
      <form method="post" action="<?php echo $PHP_SELF ?>">
      Title: <input name="title" size="40" maxlength="255">
      <br>
      Logged on user: <b><?php echo $_SESSION['username']; ?></b>
<input type="hidden" name="user" size="40" maxlength="255" value="<?php echo $_SESSION['username']; ?>"/>
      <br>
      Text:<br>
<textarea name="text2" id="text2" class="ed"></textarea>
      <br>
      <input type="submit" name="submit" value="Add News Article">
      </form>
      <?

  }//end of else

?>

 

If anyone could sort of point me in the right direction here it would be greatly appreciated.

 

Thanks.

Link to comment
Share on other sites

Oh I see, so would I apply the same as in the above bit of code to the page that displays the news?

 

The code for that page is here:

 

<html>
<body>
test page<br>
<br>

<?php
session_start();

include 'config.php';

        $result = mysql_query("SELECT * FROM scnews ORDER BY newsid DESC",$connect);

        //lets make a loop and get all news from the database

        while($myrow = mysql_fetch_assoc($result))

             {//begin of loop

               //now print the results:

               echo '<b><font face=tahoma size=3 color="#5f88c0">';
               echo $myrow['title'];
               echo "</b></span></font><font face=tahoma size=2><br><br>";
               echo $myrow['text2'];
               echo "<br><hr width=220 align=left>Posted By: <b>";
               echo $myrow['user'];
               echo "</b> On <i>";
               echo $myrow['dtime'];
               echo "</i><br>";

             }//end of loop
?>
</body>
</html>

Link to comment
Share on other sites

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.