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

formating text retrieved from a db

P: n/a
I have a database that stories entries of short stories for a writers
website. I can easily input the data into MYSQL but my trouble lies in
retrieving the data.

the story in just cut and pasted from a WP into the TEXTAREA on the form.
When I look at the story in the DB I can see it retains all the paragraph
spacing etc. but when I try and read that cell when retrieving data all the
text is crammed into one paragrapgh. What am I missing? and how would I
retrive the story text formatted?

Should a parce the test before storing it out after?
Here's the code I use to retrieve. Any ideas would be helpful.

// Selecting first unread record.

$query = "SELECT username, story_title, DATE_FORMAT(date_submitted,
'%m/%d/%y'), category, story, sub_id FROM Submission WHERE date_processed <
1 LIMIT 0,1";

$result = @mysql_query ($query); // Run the query.
if ($result) { // If OK, display the record.
echo '<span><table align="center" cellspacing="5" cellpadding="1" border="0"
width="500"><tr><td align="left" width="100">Name</td><td align="left"
width="250">Title</td><td align="left"
width="100">Date&nbsp;Submitted</td><td align="left"
width="50">Category</td></tr><tr><td colspan="4"><hr></td></tr>';

// Fetch and print record.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td align=\"left\" width=\"100\">$row[0]</td><td align=\"left\"
width=\"250\">$row[1]</td><td align=\"left\" width=\"50\">$row[2]</td><td
align=\"left\" width=\"50\">$row[3]</td></tr></span><tr><td colspan=\"4\"
width=\"450\">$row[4]</td></tr>\n";
}
echo '</table>';
echo '<hr>';

mysql_free_result ($result); // Free up the resources.

Thanks ;

Polar :)
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Polar wrote:
I have a database that stories entries of short stories for a writers
website. I can easily input the data into MYSQL but my trouble lies in
retrieving the data.

the story in just cut and pasted from a WP into the TEXTAREA on the form.
When I look at the story in the DB I can see it retains all the paragraph
spacing etc. but when I try and read that cell when retrieving data all the
text is crammed into one paragrapgh. What am I missing? and how would I
retrive the story text formatted?


This was answered for you in alt.comp.lang.php two days ago:
http://groups.google.com/groups?dq=&...6btnG%3DSearch

Jul 17 '05 #2

P: n/a

"Andy Barfield" <ab**********@yahoo.com> wrote in message
news:40**********************@mercury.nildram.net. ..
Polar wrote:
I have a database that stories entries of short stories for a writers
website. I can easily input the data into MYSQL but my trouble lies in
retrieving the data.

the story in just cut and pasted from a WP into the TEXTAREA on the form. When I look at the story in the DB I can see it retains all the paragraph spacing etc. but when I try and read that cell when retrieving data all the text is crammed into one paragrapgh. What am I missing? and how would I
retrive the story text formatted?
This was answered for you in alt.comp.lang.php two days ago:

http://groups.google.com/groups?dq=&...6btnG%3DSearch


Telling me to RTFM is not the way I expected help.. I read the manual. I
tried. Thought I would get a different approach posting my code.. but thats
okay I'll find another way.

Polar
Jul 17 '05 #3

P: n/a
Polar schrieb:
the story in just cut and pasted from a WP into the TEXTAREA on the form.
When I look at the story in the DB I can see it retains all the paragraph
spacing etc. but when I try and read that cell when retrieving data all the
text is crammed into one paragrapgh. What am I missing?
You're missing some basic HTML knowledge. A "\n" is not enough, you need
a <br>. Use nl2br().
echo "<tr><td align=\"left\" width=\"100\">$row[0]</td><td align=\"left\"
width=\"250\">$row[1]</td><td align=\"left\" width=\"50\">$row[2]</td><td
align=\"left\" width=\"50\">$row[3]</td></tr></span><tr><td colspan=\"4\"
width=\"450\">$row[4]</td></tr>\n";


echo "<tr><td align='left' width='100'>$row[0]</td><td align='left' ...

is much easier to read.

Regards,
Matthias
Jul 17 '05 #4

P: n/a
Polar wrote:
Telling me to RTFM is not the way I expected help.. I read the manual. I
tried. Thought I would get a different approach posting my code.. but thats
okay I'll find another way.


Another poster explained it for you -
<quote>
That's the way plain text is displayed within HTML. You had to add HTML
tags. You may find nl2br() useful.
</quote>

If you were to display a phrase in your browser in simple HTML, such as:
<p>Hello World
how are you today?</p>

Your browser would display:
Hello World how are you today?

Theis is because your browser will treat consecutive whitespace as just
one whitespace character.

So in basic HTML you create your code something like:
<p>Hello World<br />
how are you today?</p>

....and that would display as you expected it to (the <br /> creates a
line break).

In PHP, you would use something like nl2br($row['comment_field']); which
replaces all new line characters (\n) with line break characters <br />

I think that this is explained in the manual, hence the other poster's
advice.

regards,

Andy

Jul 17 '05 #5

P: n/a

"Andy Barfield" <ab**********@yahoo.com> wrote in message
news:40**********************@mercury.nildram.net. ..
Polar wrote:
Telling me to RTFM is not the way I expected help.. I read the manual. I
tried. Thought I would get a different approach posting my code.. but thats okay I'll find another way.


Another poster explained it for you -
<quote>
That's the way plain text is displayed within HTML. You had to add HTML
tags. You may find nl2br() useful.
</quote>

If you were to display a phrase in your browser in simple HTML, such as:
<p>Hello World
how are you today?</p>

Your browser would display:
Hello World how are you today?

Theis is because your browser will treat consecutive whitespace as just
one whitespace character.

So in basic HTML you create your code something like:
<p>Hello World<br />
how are you today?</p>

...and that would display as you expected it to (the <br /> creates a
line break).

In PHP, you would use something like nl2br($row['comment_field']); which
replaces all new line characters (\n) with line break characters <br />

I think that this is explained in the manual, hence the other poster's
advice.

regards,

Andy


Thanks Andy .. that all is in the manual. I see that and I can do the
simple text messages if I hardcode them... my problems is I am pulling the
text for a cell of the database. I apprecieate the time you take wth me.. I
think I've gotten along fine for only starting php coding 2 weeks ago.
Last time I tried coding something was 1993 in C++ ( I think ;) ) It's
been awhile.

with my code I can pull out the name title etc and put them in the HTML
table fine. but when I put the nl2br() around the $row[5] of the query it
just brings back the text on mass with nl2br() around it.
the story text output looks like this...

nl2br(Far beyond the purple haze of the distant ridge, hidden from view to
the naked eye, lay a range of mountains. They have a lofty name, a lofty
reputation and a lofty attitude. But, on every clear day, they effect his
life in a way not too many other things could. He met someone there, once. A
fleeting moment. A butterflies kiss in a lifetime, but a moment as strong at
the mountains, and as lasting. Each clear day he runs the gauntlet of his
emotions. He pauses. He watches. He remembers. A tune on the radio plays
sweetly in the background.... )

the part of the script looks like this.

// Fetch and print the record.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td align=\"left\" width=\"25\">$row[0]</td><td align=\"left\"
width=\"75\">$row[1]</td><td align=\"left\" width=\"250\">$row[2]</td><td
align=\"left\" width=\"50\">$row[3]</td><td align=\"left\"
width=\"50\">$row[4]</td></tr></span><tr><td colspan=\"5\"
width=\"500\">nl2br($row[5])</td></tr>\n";
}
echo '</table>';
echo '<hr>';
Now in the Database if I look at it with phpMyAdmin I see the text... it
has the formating with blank lines between the paragraphs etc. there is no
/n codes I see. If I manually put in <P> in front of all the paragraphs in
the db then my script shows the text all formatted fine. But I don't want
to go in and manually edit stories with 100 paragraghs..

Your ideas are most welcome.

Polar :)
Jul 17 '05 #6

P: n/a
"Polar" <po****@shaw.ca> wrote in message
news:kcYAc.775857$Pk3.576385@pd7tw1no...
I have a database that stories entries of short stories for a writers
website. I can easily input the data into MYSQL but my trouble lies in
retrieving the data.

the story in just cut and pasted from a WP into the TEXTAREA on the form.
When I look at the story in the DB I can see it retains all the paragraph
spacing etc. but when I try and read that cell when retrieving data all the text is crammed into one paragrapgh. What am I missing? and how would I
retrive the story text formatted?

Should a parce the test before storing it out after?
Here's the code I use to retrieve. Any ideas would be helpful.


You could use the html <pre> preformatted tag around the text.

Or you need to do some processing of the text before being displayed

You'll want to use htmlspecialchars

<?php

$foo = '
<script language="JavaScript">
<!--
alert("Hello World");
-->
</script>
';
echo $foo;
echo htmlspecialchars($foo);

?>

Remember $foo could just as easily be one of the fields entered into your
database.

Also your output statement is pretty "hairy", I prefer to use (s)printf for
embedding data into tables.

printf("<tr><td align=\"left\" width=\"100\">%s</td><td align=\"left\"
width=\"250\">%s</td><td align=\"left\" width=\"50\">%s</td><td
align=\"left\" width=\"50\">%s</td></tr></span><tr><td colspan=\"4\"
width=\"450\">%s</td></tr>\n" ,
$row[0],
$row[1],
$row[2],
$row[3],
nl2br(htmlspecialchars($row[4]))
);
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.