Insert snippets to DB and retrieve with same formatting?

hello guys,

i made a small php app for website that i use my self to store programming language eg: php,vb,c++

it works and retrieve data correctly with just one exception.

if insert this code:
[php]

<?php # connect to the database here # search the database to see if the user name has been taken or not $query = sprintf("SELECT * FROM users WHERE user_name='%s' LIMIT 1",mysql_real_escape_string($_POST['user_name'])); $sql = mysql_query($query); $row = mysql_fetch_array($sql); ?>

[/php]

when i retrieve it to display on my page i get this:

[php]

<?php# connect to the database here# search the database to see if the user name has been taken or not $query = sprintf("SELECT * FROM users WHERE user_name='%s' LIMIT 1",mysql_real_escape_string($_POST['user_name'])); $sql = mysql_query($query);$row = mysql_fetch_array($sql);?>

[/php]
as you can see it lose all the formatting.

so the question how can i retrieve these codes formatted the way i inserted them?

I havent tried yet but i dont think

 will help at all.

Thanks in advance. :o

Don’t use sprintf. It’s changing all your formatting.

[php]
$query = “SELECT * FROM users WHERE user_name=’”.mysql_real_escape_string($_POST[‘user_name’])."’ LIMIT 1";
[/php]

No. The above code i only googled and used it as an example of snippets that I might Insert into my DB, but for now any code that i insert such as
[php]
if (A==A)
{
echo “A”;
}
[/php]

then when i display on the browser it looks like this:
if (A==A){echo “A”;}

see? what happening is losing the preformatted way it was. how can i fix that so than when i cut snippets from my notepad++ and insert it into the DB when i display on the browser it looks the same?

this is the real project right here

http://wilson382.info/projects/snippet/

is the file being saved as a .php file?

this shouldn’t display on browser at all. all you should see is A

thanks for repplying.

yes it is a php file and hosted on the server take a look at the link i provided above.

you are right but im not gonna display them as logic code i will only display them as snippets codes.
if u go to the link i provided u can see that the codes are not on the normal standard way of formatting.

the deal with this small php app is that i want to be able to inserts snippets codes of programming languages that are easily forgettable into my DATABASE and then be able to access these codes anywhere.

sorry if i wasn’t cear enough

Ok I checked out the site and I think I found the problem. It’s not inserting the code that’s your problem, it’s how you’re displaying it. What code are you using to display the data from the mysql database?

Im not using any technique to display it I just display it like a regular string data.

I open a table and then echo all the results.
[php]
while($rows = mysql_fetch_assoc($sresult))
{
echo “

”;
echo “”.$rows[‘Name’]."";
echo “”.$rows[‘Programming’]."";
echo “”.$rows[‘Description’]."";
echo “”.$rows[‘Examples’]."";
echo “”;
}
[/php]

Do you think adding
for every end of the line will do it?
also to not use so much space on the display thing i thought about using an iframe similar to
the same used on this forum for php codes eg: (php) (/php)

Try this:
[php]
while($rows = mysql_fetch_assoc($sresult))
{
echo “

”;
echo “”.$rows[‘Name’]."";
echo “”.$rows[‘Programming’]."";
echo “”.nl2br($rows[‘Description’])."";
echo “”.nl2br($rows[‘Examples’])."";
echo “”;
}
[/php]

nl2br() changes newlines into
so it adds them where it should be.

Also the php tags above doesn’t use an iframe. It’s a CSS formatting. The code I just posted should format it so that everything is on newlines. I’m not sure about anything that is tabbed though.

Thanks RaythXC

that’s exactly what i wanted.

btw i removed the nl2br from the description sinc i only need it on the code snippets

also i think i iwll use an iframe with pre defined height so long codes don’t take the whole page.

Thanks man

I actually worked this out by posting a PHP snippet into your database. It’s the same problem I ran into before when first making a forum system.

I noticed i will keep it for future reference as well.

Sponsor our Newsletter | Privacy Policy | Terms of Service