Solved

PHP Code to Fetch Variables from MySQL & Retain Formatting

Posted on 2009-03-31
4
212 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
The viewer will learn how to count occurrences of each item in an array.
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now