?
Solved

PHP Code to Fetch Variables from MySQL & Retain Formatting

Posted on 2009-03-31
4
Medium Priority
?
222 Views
Last Modified: 2013-12-12
Hello,

I am trying to build a website that will allow a search to be conducted of various comments that were made. It will be for internal use only. I am using PHP. It will search by name and then return the comments the user made.

I need it to retain the exact formatting as entered. Inside the MySQL database there are line breaks, etc.

I am writing the PHP to output it to screen. It destroys all of the formatting, however, and just concatenates it all into a large lump. I must have the formatting retained. It is fine in the MySQL database so getting it on screen shouldn't be too hard.

My code looks like:

while ($row= mysql_fetch_array($result))
{      
echo "<p><b>Comments:</b><br>" .$row['Quarter3Eval']. "</b></p>";
}


If the comment initially were:

This is line A.
This is line B.
This is line C.

It smashes it together and outputs: "This is line A. This is line B. This is line C."

How can I get the output to the screen without that loss of format?

Thank you,
Joseph Irvine
0
Comment
Question by:jkeagle13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24036226
please try this:
while ($row= mysql_fetch_array($result)) 
{      
  echo "<b>Comments:</b><br>" . str_replace($row['Quarter3Eval'], "\n", "<br/>\n") . "</b>";
}

Open in new window

0
 

Author Comment

by:jkeagle13
ID: 24036252
Hello,

It is not throwing an error, however, nor is it displaying anything. Now it just shows up blank under comments.

Is there some additional line of code needed?

Thanks,
Joseph
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24036282
sorry, my fault, I put the arguments in the wrong order...

  echo "<b>Comments:</b><br>" . str_replace("\n", "<br/>\n", $row['Quarter3Eval']) . "</b>";

Open in new window

0
 

Author Closing Comment

by:jkeagle13
ID: 31565176
Hello!

Thanks! You are definitely a lifesaver! I had spent almost an hour looking for how to do that online. I am a PHP newbie who, unfortunately, had a major project due that required extensive PHP.

I do appreciate your help last-minute!

Thanks!
Joseph
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question