By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,873 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,873 IT Pros & Developers. It's quick & easy.

Trying to store and retrieve formatted text from MYSQL using PHP - Newbee seeking advice

P: n/a
I am trying to store formatted text (windows format) into a MySQL
database and then retrieve it. The field in the database is a
varchar. I cut and paste the test into a form field formatted, then
call the PHP program to add the record using POST. When I get the
text out, it has lost it's formatting. Formatting in the text is
achieved by multiple spaces not tabs, but even multiple spaces are
converted to a single space and the carrage return/line feeds are
lost. Below is the basic code I am calling when I click POST in the
form.

I would really appreciate advice on how I can make this work. I have
looked through many sites, and have not seen anything with deals with
this sort of problem.

Thanks in advance.

James.

-----------------
Code Below.
-----------------

<?PHP
$date=$_POST['date'];
$title=$_POST['title'];
$scores=$_POST['scores'];

$db=mysql_connect("localhost","root") or die ("cannot connect");
mysql_select_db("mydb") or die ("can not select database");
$query="INSERT INTO scores (scoresdate, scorestitle, scorestext)
VALUES ('$date', '$title', '$scores')";
printf($query);
$result=mysql_query($query) or die ("Invalid Query : " .
mysql_error());
echo "Thank you!";
?>
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
When I get the
text out, it has lost it's formatting. Formatting in the text is
achieved by multiple spaces not tabs, but even multiple spaces are
converted to a single space and the carrage return/line feeds are
lost.


HTML ignores multiple spaces and only displays 1
Jul 17 '05 #2

P: n/a
I noticed that Message-ID:
<7d**************************@posting.google.com > from James Turner
contained the following:
I would really appreciate advice on how I can make this work. I have
looked through many sites, and have not seen anything with deals with
this sort of problem.


The formatting is probably still there, it just does not work in html.
You need to change all occurrences of a space with &nbsp; and all
newlines with <br>.

So assuming your output is in $string

print nl2br(str_replace(" ","&nbsp;",stripslashes($string)));

But the html won't be pretty...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3

P: n/a

"James Turner" <ja**********@westnet.com.au> wrote in message
news:7d**************************@posting.google.c om...
I am trying to store formatted text (windows format) into a MySQL
database and then retrieve it.

<snip>

Does serialize() not cut the mustard?

Garp
Jul 17 '05 #4

P: n/a
> I am trying to store formatted text (windows format) into a MySQL
database and then retrieve it. The field in the database is a
varchar. I cut and paste the test into a form field formatted, then
call the PHP program to add the record using POST. When I get the
text out, it has lost it's formatting. Formatting in the text is
achieved by multiple spaces not tabs, but even multiple spaces are
converted to a single space and the carrage return/line feeds are
lost. Below is the basic code I am calling when I click POST in the
form.

You can use the <pre> tag for that (preformatted text):
<pre>Your text here</pre>
Your formatting (spaces/line feeds) will be retained, but most browsers
will render the text in a fixed-pitch font, so that may be a disadvantage.

Regards,
Henk Burgstra
Jul 17 '05 #5

P: n/a
Henk Burgstra <eg****@xs4all.nl> wrote in
news:pa****************************@xs4all.nl:
I am trying to store formatted text (windows format) into a MySQL
database and then retrieve it. The field in the database is a
varchar. I cut and paste the test into a form field formatted, then
call the PHP program to add the record using POST. When I get the
text out, it has lost it's formatting. Formatting in the text is
achieved by multiple spaces not tabs, but even multiple spaces are
converted to a single space and the carrage return/line feeds are
lost. Below is the basic code I am calling when I click POST in the
form.

You can use the <pre> tag for that (preformatted text):
<pre>Your text here</pre>
Your formatting (spaces/line feeds) will be retained, but most
browsers will render the text in a fixed-pitch font, so that may be a
disadvantage.


If it's "formatted with multiple spaces" then it almost certainly *needs*
to be displayed in a fixed-pitch font to retain its alignment.

Remember that "<" and "&" still have to be "entified" even in <pre> text;
it would be a good idea to run it through htmlspecialchars().
Jul 17 '05 #6

P: n/a
Thanks everyone for your assistance. The simple HTML tag did the
trick. Why I did not think of that myself, I don't know.

Thanks again,

James.

Eric Bohlman <eb******@earthlink.net> wrote in message news:<Xn*******************************@130.133.1. 4>...
Henk Burgstra <eg****@xs4all.nl> wrote in
news:pa****************************@xs4all.nl:
I am trying to store formatted text (windows format) into a MySQL
database and then retrieve it. The field in the database is a
varchar. I cut and paste the test into a form field formatted, then
call the PHP program to add the record using POST. When I get the
text out, it has lost it's formatting. Formatting in the text is
achieved by multiple spaces not tabs, but even multiple spaces are
converted to a single space and the carrage return/line feeds are
lost. Below is the basic code I am calling when I click POST in the
form.

You can use the <pre> tag for that (preformatted text):
<pre>Your text here</pre>
Your formatting (spaces/line feeds) will be retained, but most
browsers will render the text in a fixed-pitch font, so that may be a
disadvantage.


If it's "formatted with multiple spaces" then it almost certainly *needs*
to be displayed in a fixed-pitch font to retain its alignment.

Remember that "<" and "&" still have to be "entified" even in <pre> text;
it would be a good idea to run it through htmlspecialchars().

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.