user3397472
user3397472

Reputation: 65

Php trying to insert a break line into a while statement

Code

Look at the second echo under the second do

do {
    echo "<strong>Topic:". $row_AllTopics['topic'] . "</strong><br>";

    mysql_select_db($database_Select, $Select);
    $query_Comments = sprintf("SELECT * FROM comments where ((event_id=%s) AND (topic_id=%s)) ", $_POST['event_id'], $row_AllTopics['id']);
    $Comments = mysql_query($query_Comments, $Select) or die(mysql_error());
    $row_Comments = mysql_fetch_assoc($Comments);
    $totalRows_Comments = mysql_num_rows($Comments);

    do {
        // This is the echo in question.
        echo $row_Comments['comment'] . "Posted by: ". $row_Comments['poster'] . "\n";
    } while ($row_Comments = mysql_fetch_assoc($Comments));


} while ($row_AllTopics = mysql_fetch_assoc($AllTopics));

Output

(This text is all centered align on the screen )

Topic:computer
linuxPosted by: andy linuxPosted by: andy Topic:computer
Posted by: Topic:ji
poPosted by: andy Topic:drive
makPosted by: andy Topic:new
nicePosted by: andy Topic:golf
holePosted by: andy plPosted by: andy

Upvotes: 0

Views: 45

Answers (3)

Black0CodeR
Black0CodeR

Reputation: 787

"\n" will cause a line-break in the source code. While Your browser will be rendering html so you need to use an HTML Line-Break ( <br> OR <br /> ) Which will give the expected results.

So, Your code will be like this:

BEFORE: echo $row_Comments['comment'] . "Posted by: ". $row_Comments['poster'] . "\n";

AFTER: echo $row_Comments['comment'] . "Posted by: ". $row_Comments['poster'] . "<br />";

Upvotes: 0

roun512
roun512

Reputation: 149

You can use nl2br() function in PHP by:

echo nl2br($row_Comments['comment'] . "Posted by: ". $row_Comments['poster'] . "\n");

This will convert \n to <br/>.

Upvotes: 0

user3567510
user3567510

Reputation:

Instead of \n, try using <br>.

Upvotes: 1

Related Questions