0

I have a quiz database in MySQL where both problem statement and solution (as correct_sol) are stored as text with utf8_general_ci collation. My problem is whenever I recall this information, the results don't have any line breaks. I am using the following code:

    $quiz_solution = $wpdb->get_results("query");

    foreach($quiz_solution as $item){
        $returnVal .= "Solution: $item->correct_sol <br>";
    }
    return $returnVal;

Not sure if getting the results as an Array is causing the problem.

4
  • 1
    You display the data probably in html, so you need to use nl2br Commented Oct 14, 2013 at 8:33
  • modify you concatenation to "Solution: ".$item->correct_sol." <br>"; Commented Oct 14, 2013 at 8:33
  • 1
    How do you store the linebreaks? <br>'s? \n's? \r's? Commented Oct 14, 2013 at 8:33
  • @Hendriq yes nl2br works, thanks. Commented Oct 14, 2013 at 8:40

2 Answers 2

3

Newlines are not rendered by the browser unless you use the <br /> tag.

You can convert your newlines into break tags using nl2br().

$returnVal .= "Solution: " . nl2br($item->correct_sol) . " <br>";
Sign up to request clarification or add additional context in comments.

1 Comment

..or unless you have any container element with the style white-space: pre on. (such as <pre> elements by default), or you output your content with content-type: text/plain
1

where your going to print this

use \n when you are returning as type/text output use <br /> when you are returning as type/html output

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.