Solved

PHP Code to Fetch Variables from MySQL & Retain Formatting

Posted on 2009-03-31
4
221 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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…

724 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