sh0wtym3 Posted July 21, 2009 Share Posted July 21, 2009 I wrote a script that inserts html into the database. The HTML before insertion is "formatted" when you view it in dreamweaver/notepad/etc. Here is an example: <h1> Sample Header</h1> <b> Title </b> <i> Sentence goes here </i> However, when I select the data from my database and echo it out, it "squishes" together. Here is an example: <h1> Sample Header</h1><b> Title </b><i> Sentence goes here </i> When echoing over 300 lines of code it gets difficult to read. Is there a way to prevent this from occuring? Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2009 Share Posted July 21, 2009 You either need to use <pre></pre> tags around the code or use nl2br to cause the new-line characters to have a <br /> tag added so that the browser will render the content on separate lines. Edit: Either of these methods affect the "presentation" of the code and should be done when you output the code, not when you save it in a database. You should save the code in its' raw "as is" form so that you can do anything you want with it when you output it, such as apply color highlighting. Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879577 Share on other sites More sharing options...
sh0wtym3 Posted July 21, 2009 Author Share Posted July 21, 2009 Thanks! I learned about the <pre> tags before in class but forgot all about them Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879584 Share on other sites More sharing options...
sh0wtym3 Posted July 21, 2009 Author Share Posted July 21, 2009 I'm inserting <pre> tags before and after the HTML output now But it's not preserving any format, its displaying all the code on one single long line now... The column type that the html is being store in is "longtext", if that helps. Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879601 Share on other sites More sharing options...
J.Daniels Posted July 21, 2009 Share Posted July 21, 2009 To format output text from PHP, you need new line characters for line breaks: <?php echo "\n"; ?> Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879617 Share on other sites More sharing options...
sh0wtym3 Posted July 21, 2009 Author Share Posted July 21, 2009 Ok that makes sense. Then before inserting the html, I need to have "\n" placed automatically before all line breaks. Is there a PHP function that will do that for me? Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879625 Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2009 Share Posted July 21, 2009 Your "code" already has line breaks in it if it appears correctly when viewed in dreamweaver/notepad... You would need to be way.... more specific about what your code and data is. The following works for me - <?php // pretend to get some $data from a query - $data = '<h1> Sample Header</h1> <b> Title </b> <i> Sentence goes here </i>'; echo "<pre>"; echo htmlentities($data); echo "</pre>"; ?> Are you sure the new-lines were actually inserted and are present in the database? Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879627 Share on other sites More sharing options...
sh0wtym3 Posted July 21, 2009 Author Share Posted July 21, 2009 Sorry, I'll paste my entire code to avoid any confusion: Here is the index.php page where you can submit and generate the html code: <form action="submit.php" method="post"> <textarea name="test"></textarea> <input type="submit" name="submit" value="Add New Template" /> </form> <form action="generate.php" method="get"> <input type="submit" name="submit" value="Generate Code" /> </form> <?php if (isset($content)) { echo "$content"; session_destroy(); }?> Here is the submit.php page: <?php error_reporting(E_ALL); $conn = mysql_connect("localhost","username","password"); mysql_select_db("database"); $test = $_POST['test']; $value = mysql_real_escape_string($test); $sql = "INSERT INTO templates (content) VALUES ('$value')"; $res = mysql_query($sql, $conn) or die("Couldn't open $db: ".mysql_error()); if ($res) { header ("Location: index.php?s=success"); } else { header ("Location: index.php?s=fail"); } ?> And here is the generate.php page: <?php session_start(); $conn = mysql_connect("localhost","username","password"); mysql_select_db("table"); $sql = "SELECT * FROM templates"; $res = mysql_query($sql, $conn); $row = mysql_fetch_array($res); $content = stripslashes($row['content']); $open = "<"; $close = ">"; $content = ereg_replace("<", $open, $content); $content = ereg_replace(">", $close, $content); $content = "<pre>".$content."</pre>"; $_SESSION['content'] = $content; header ("Location: index.php"); ?> Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879634 Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2009 Share Posted July 21, 2009 The posted code works for me (after you put a session_start() into index.php and use $_SESSION['content'] in that file as well.) Are you sure you are using the same database in all the files? You are selecting "database" one time and "table" another time. Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879659 Share on other sites More sharing options...
sh0wtym3 Posted July 21, 2009 Author Share Posted July 21, 2009 Yeah "table" and "database" are just fake names to protect the identity of the innocent I found the problem, I was using: $content = "<pre>".$content."</pre>"; ...and then: echo "$content"; I removed this line $content = "<pre>".$content."</pre>"; and then used your version: echo "<pre>"; echo "$content"; echo "</pre>"; And that worked fine! Link to comment https://forums.phpfreaks.com/topic/166803-solved-retaining-format-when-inserting-html/#findComment-879664 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.