Solved

PHP Code to Fetch Variables from MySQL & Retain Formatting

Posted on 2009-03-31
4
215 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
  • 2
  • 2
4 Comments
 
LVL 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now