2

I have the following output from my database. How can I output the existing line breaks of the text??

while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>" ;
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['kategorie'] . '</td>';
    echo '<td>' . $row['titel'] . '</td>';
    echo '<td>' . $row['betroffen'] . '</td>';
    echo '<td style="width:"600px">' . $row['beschreibung'] .  '</td>';
    echo '<td>' . $row['prioritaet'] . '</td>';
    echo '<td>' . $row['status'] . '</td>';
    echo '<td>' . $row['eingereicht'] . '</td>';
    echo '<td>' . $row['umsetzung'] . '</td>';
    echo '<td style="width:"400px">' . $row['kommentar'] . '</td>';
    echo '<td>' . $row['entwickler'] . '</td>';
    echo '<td><a href="edit.php?id=' . $row['id'] . '">bearbeiten</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">löschen</a></td>';
    echo "</tr>"; 
} 

// close table>
echo "</table>";
4
  • It has outputed properly. Look at your HTML source. Commented Jul 9, 2014 at 7:33
  • nl2br() ? Commented Jul 9, 2014 at 7:34
  • How to use nl2br in my code? Commented Jul 9, 2014 at 7:35
  • Which column holds the new line...? Commented Jul 9, 2014 at 7:37

2 Answers 2

2

There are a few ways to do this. One is with str_replace another (more common way) is to use nl2br.

For both you have 2 Options in how you use them. 1.) You use single echo Statements as you currently do. In this case you use the commands on the values themselves.

For using str_replace (be Aware that you Need here to differentate between the OS you are using the code on...some use \n some \n\r ,.....

echo '<td>' . str_replace("\n", "<br>", $row['umsetzung']) . '</td>';

For completeness sake I'll also put in nl2br (although dinesh already has posted that)

echo '<td>' . nl2br($row['umsetzung']) . '</td>';

2.) You gather the Output into a variable and then echo that variable (while using the commands on it).

$myOutput = ""
$myOutput = $myOutput . "<tr>" ;
$myOutput = $myOutput . '<td>' . $row['id'] . '</td>';
$myOutput = $myOutput . '<td>' . $row['kategorie'] . '</td>';
......
$myOutput = $myOutput . '<td><a href="delete.php?id=' . $row['id'] . '">löschen</a></td>';
$myOutput = $myOutput . "</tr>"; 
echo nl2br($myOutput);

This method is also possible with str_replace instead of nl2br.

Sign up to request clarification or add additional context in comments.

Comments

0
nl2br($string)

Suppose you want line breaks in title, that is a string. You can do this as follows:-

$trs = '';
while($row = mysql_fetch_array( $result )) {
  extract($rows);
  $trs .= "<tr>$id</td>"
  $trs .= "<tr>$kategorie</td>"
  $trs .= "<tr>nl2br($titel)</td>"
}

echo $trs;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.