Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

why "\n" for new line is not working?

     

shams

4:33 pm on Oct 19, 2006 (gmt 0)

5+ Year Member



hi,
this code should list mysql databases in a the list:
<?php
$link = mysql_connect('my_db', 'user', 'pass') or die(mysql_error());
$db_list = mysql_list_dbs($link);

while ($row = mysql_fetch_object($db_list)) {
echo $row->Database."\n";
}
?>
but this is output in one line:

nformation_schema amarok mitradb mydb mydb.back.save mysql punbb test y2004 y2005 y2006

benevolent001

4:39 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo $row->Database."\n";

Hi i have just started reading php book and here it what is says , hope it clears all your doubts


One mistake often made by new PHP programmers (especially those from a C background) is
to try to break lines of text in their browsers by putting end-of-line characters (ď\nĒ) in the
strings they print. To understand why this doesnít work, you have to distinguish the output of
PHP (which is usually HTML code, ready to be sent over the Internet to a browser program)
from the way that output is rendered by the userís browser. Most browser programs will
make their own choices about how to split up lines in HTML text, unless you force a line
break with the <BR> tag. End-of-line characters in strings will put line breaks in the HTML
source that PHP sends to your userís browser (which can still be useful for creating readable
HTML source), but they will usually have no effect on the way that text looks in a Web page.

rokec

6:16 pm on Oct 19, 2006 (gmt 0)

5+ Year Member



Update your code...

echo(row->Database."<br>");

hope helped you a bit.

coopster

7:02 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Or you can embed them in HTML <pre> tags.
echo '<pre>'; 
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database."\n";
}
echo '</pre>';

jatar_k

10:33 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



or you can make sure the source and the output looks pretty

echo $row->Database,"<br>\n";

spinnercee

3:16 pm on Oct 20, 2006 (gmt 0)

10+ Year Member



I've also noticed that '\n' does not generate a newline where "\n" does [PHP 4.3.11-win32] -- this drove me crazy for a while.

To help with that, I created some literal variable strings that make outputing HTML and ASCII in PHP a bit easier:

$cr = chr(13); // 0x0D [\r]
$lf = chr(10); // 0x0A [\n]
$crlf = $cr . $lf; // [\r\n]
$br = '<br>'; // HTML line break

jatar_k

3:43 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



that has to do with the nature of how strings are handled with single and double quotes

[php.net...]

spinnercee

11:21 pm on Oct 20, 2006 (gmt 0)

10+ Year Member



/* Howcum you get to use links? :) */

jatar_k

1:07 am on Oct 21, 2006 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



because as far as php goes, that site is the authority
 

Featured Threads

Hot Threads This Week

Hot Threads This Month