Jump to content

[SOLVED] Retaining "format" when inserting HTML


sh0wtym3

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Yeah "table" and "database" are just fake names to protect the identity of the innocent  :D

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.