0

I am relatively new to php/mysql. I am trying to do carriage returns for this part:

echo "<td colspan='5'>" . nl2br($row['dish_description']) . "</td>";

"dish_description" contains some text which includes "\n" e.g. "Served with salad and mint sauce. \n Contains beef." In the MySQL database however I cannot display the carriage return.

    <?php
    require_once 'login.php';
    $con = mysql_connect($db_hostname, $db_username, $db_password);
    mysql_select_db($db_database) or die("Unable to select database". mysql_error());

    $result = mysql_query("SELECT * FROM `dishes` ORDER BY `dishes`.`dish_id` ASC LIMIT 0 , 200");

    echo '<table align="center">
    ';

    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td style='width: 60px'></td>";
    echo "<th style='width: 100px'>Dish No</th>";
    echo "<td style='width: 70px'>" . $row['dish_id'] . "</td>";
    echo "<th style='width: 100px'>Dish Name</th>";
    echo "<td style='width: 120px'>" . $row['dish_name'] . "</td>";
    echo "<th style='width: 120px'>Dish Price</th>";
    echo "<td>£" . $row['dish_price'] . "</td>";
echo "</tr>";

echo "<tr>";
    echo "<td style='width: 75px'></td>";
    echo "<th style='width: 100px'>Vegetarian</th>";
    echo "<td style='width: 70px'>" . $row['dish_vegetarian']   ."</td>";
    echo "<td style='width: 100px'></td>";
    echo "<td style='width: 70px'></td>";
    echo "<th style='width: 120px'>Contains Nuts</th>";
    echo "<td style='width: 120px'>" . $row['dish_nuts']   ."</td>";
echo "</tr>";
echo "<tr>";
    echo "<td style='width: 75px'></td>";
    echo "<th style='width: 100px'>Information</th>";
    ***echo "<td colspan='5'>" . nl2br($row['dish_description']) . "</td>";***
echo "</tr>";
echo "<tr height='10px'>";
    echo "<td style='width: 75px'></td>";
    echo "<td style='width: 100px'></td>";
echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
?>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ish
  • 3
  • 1
  • What happens if you include the is_xhtml flag in your call to nl2br? (http://php.net/manual/en/function.nl2br.php) – MrVimes Jun 16 '13 at 21:45
  • I'm not too sure on how to use it. I'm somewhat new to php/mysql – ish Jun 16 '13 at 21:53
  • Your problem is fixed now, but if you want, you could try replacing nl2br($row['dish_description']) with nl2br($row['dish_description'],true) I'm not saying this was the cause. I was just curious if it would happen to fix it. It uses the more 'correct'
    instead of
    – MrVimes Jun 16 '13 at 22:30

1 Answers1

0

In my opinion if you echo it as HTML you can just add <br> tags to your records in database.

Iwo Kucharski
  • 3,735
  • 3
  • 50
  • 66
  • This would work, but I personally wouldn't want to store html in a sql database. What if other non-html applications want to show the data? – MrVimes Jun 16 '13 at 21:46
  • @MrVimes - you can use CR or LF like char(10) / char(13) . More info [here](http://blog.sqlauthority.com/2007/08/22/sql-server-t-sql-script-to-insert-carriage-return-and-new-line-feed-in-code/) or [here](http://stackoverflow.com/questions/31057/how-to-insert-a-line-break-in-a-sql-server-varchar-nvarchar-string). – Iwo Kucharski Jul 16 '13 at 14:27