简体   繁体   中英

MySQL output with line breaks in php

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>";
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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM