Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading the br as a line break when retrieving from mysql 3

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
hello.....again,

Im displaying data from a mysqltable. In the table the data looks like this
Code:
hello<BR> what<BR>  is this working or not<BR> I<BR>  hope so<BR>

How do i display the data so that it reads the <br> as line breaks?

at the moment it is displaying the data like this
Code:
hello what is this working or not I hope so
eg no line breaks

Help
Soph
 
try <br/> instead of <BR>. this may not be the answer but is an easy fix just in case it is a browser + xhtml issue.
 
hi jpadie.

Im pulling the data from the table like this
Code:
$result = mysql_query("SELECT * FROM hc21 WHERE userid = '$userid'");

	while($row = mysql_fetch_object($result))
		{
		echo "<table class='main>"; 
			echo "<tr>";
			echo "<td valign='top'>";
		        echo "$row->answer";
                        echo "</td>";
			echo "</tr>";
			echo "</table>";
}
 ?>

How would i convert the <br> that is in the data to <br/> when it is outputted as you suggest?

Thanks
Soph
 
I think you should be able to use a \n in the SQL database and replace it with a <br /> with the php script, and just to let you know - your code snippet has a minor ... problem
Code:
$result = mysql_query("SELECT * FROM hc21 WHERE userid = '$userid'");

    while($row = mysql_fetch_object($result))
        {
        echo "<table class='main[COLOR=red]'[/color]>";//class was improperly closed
            echo "<tr>";
            echo "<td valign='top'>";
                echo "$row->answer";
                        echo "</td>";
            echo "</tr>";
            echo "</table>";
}
?>
also, you have an end bracket and end script without a beginning, which may be just because you pasted only that section, but if not it needs a beginning.
 
i'm surprised that your code pulls results out of the database as the $userid is within single quotes and i'd always thought variables weren't interpolated within single quotes. I'd typically use:
Code:
$result = mysql_query("SELECT * FROM hc21 WHERE userid = '".$userid."'")
  or die ("Error " . mysql_error());
for the translation - i think i'd use str_replace but i'm not convinced this is the problem. have you tried just echoing the text to see what happens (i.e. outside of a database retrieve)

the str_replace works thus:

Code:
echo str_replace("<BR>", "<br/>", $row->answer);
 
no your right that didn't solve the problem.

Its still echos without a line break.

If i just echo
Code:
hello<BR> what<BR> is this working or not<BR> I<BR>  hope so<BR>
outside of the database retrieve its fine and puts in the line breaks.

Im puzzlled.

This is what the data looks like in the database

Soph
 
In the SQL entry, replace '<BR>' with '\n' (without quotes), then in your code, put
Code:
echo str_replace("<BR>", "<br/>", $row->answer);

so it looks like
Code:
$result = mysql_query("SELECT * FROM hc21 WHERE userid = '$userid'");

    while($row = mysql_fetch_object($result))
        {
        echo "<table class='main'>";
            echo "<tr>";
            echo "<td valign='top'>";
                echo str_replace("\\n", "<br/>", $row->answer);
                        echo "</td>";
            echo "</tr>";
            echo "</table>";

That should work...

-> LuckySyringe
 
Soph - what is the actual code output when the lines are not breaking (ie view the source code of the page).
 
thanks LuckySyringe

i have another question

how do i replace '<BR>' with '\n' (without quotes) In my SQL entry.

This is what im using at the mo
Code:
<?php 
include 'inc/x73rf11.php';
$userid = "2";

$answer = stripslashes($_POST['answer']);

$query="UPDATE hc21 SET answer1 = '$answer' WHERE userid = $userid LIMIT 1";
$result=mysql_query($query);
		
		if ($result)
			
			{
				echo "<h1>You have succesfully updated the table</h1>
				
			<br>
			<br>
				<a href='#'>Continue</a>";
			

			}
			
		else if (!$result)
			
			{
				echo "<br><br><br>Problem. Didn't update.
			
			<br>
			<br>
				<a href=\"Javascript:history.go(-1)\">Please go back and try again</a>";
			

			}
			
?>

thanks again

Soph
 
jpadie,

yea i had a look at that the source show no <br> where there should be a <br>

it shows

hello<br>me

like

hello
me

so the browser is just reading it like

hello me


If you know what i mean

Sophx
 
so somewhere either php or mysql is doing some substitution (or i am very rusty). can you try

Code:
echo nl2nr($row->answer);
 
thats nl2br($row->answer);

but if mysql is outputting text<br>text it should be parsed by the browser.

Its not being enclosed in any odd tags at all , or a textarea or anything weird like that ?

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
sophielois, run the script as normal, then on the page where you get hello me, view the source (right click - view source in IE on windows) and check whether you see 'hello<br>me' or 'hello me'. If the case is the latter, there is something wrong with the server side, in case of the former, there is something wrong with the browser.

-> LuckySyringe
 
yea,

like it tried to explain (not very well)

you see

hello
me


in the source code

Soph
 
Ok, so that means its printing hello\nme, now as jpadie and karver said is 'echo nl2br($row->answer);' in your code?
ie:
Code:
    while($row = mysql_fetch_object($result))
        {
        echo "<table class='main'>";
            echo "<tr>";
            echo "<td valign='top'>";
                echo nl2br($row->answer);// here
                        echo "</td>";
            echo "</tr>";
            echo "</table>";
that should take the \n that is printed and turn it into a <br>

-> LuckySyringe
 
ok guys,

echo nl2br($row->answer);

this works. Its now displaying properly!!

How would i add this code so it will display all rows correctly.

I just thought could it be because im working on a subdomain which has ssl installed??

thanks for all the help

Sophxx
 
the thing that is interesting me is how the <BR> is becoming garbled. i can't think of any combination of stripslashes, magic_quotes etc that would do this.

soph - how are you getting the data into the database? is it a textarea or text input box or what (not that this should matter). if it is a textarea are you typing the <br> or just pressing return and assuming that creates an html para break (it doesn't)? and what character set are you using in your form?
 
if you want it to post each result on its own row, just move the '<table>' and '</table>' outside the do while loop.
ie:
Code:
echo "<table class='main'>";
    while($row = mysql_fetch_object($result))
        {
            echo "<tr>";
            echo "<td valign='top'>";
                echo nl2br($row->answer);// here
            echo "</td>";
            echo "</tr>";
        }
            echo "</table>";

-> LuckySyringe
 
How would i add this code so it will display all rows correctly.
i'm not sure i fully understand the question, but i'd recommend addressing the issue of how the content is getting into the database first. this will probably solve the output issue.

If you can't address the input, then just wrap each field in the nl2br() function as it comes out. If you were using arrays rather than objects it would be simple enough to walk the array with the nl2br() function.

the issue has nothing to do with ssl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top