homepage Welcome to WebmasterWorld Guest from 54.167.174.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

5+ Year Member



 
Msg#: 3127164 posted 4:33 pm on Oct 19, 2006 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3127164 posted 4:39 pm on Oct 19, 2006 (gmt 0)

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

5+ Year Member



 
Msg#: 3127164 posted 6:16 pm on Oct 19, 2006 (gmt 0)

Update your code...

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

hope helped you a bit.

coopster

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



 
Msg#: 3127164 posted 7:02 pm on Oct 19, 2006 (gmt 0)

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

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



 
Msg#: 3127164 posted 10:33 pm on Oct 19, 2006 (gmt 0)

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

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

spinnercee

10+ Year Member



 
Msg#: 3127164 posted 3:16 pm on Oct 20, 2006 (gmt 0)

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

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



 
Msg#: 3127164 posted 3:43 pm on Oct 20, 2006 (gmt 0)

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

[php.net...]

spinnercee

10+ Year Member



 
Msg#: 3127164 posted 11:21 pm on Oct 20, 2006 (gmt 0)

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

jatar_k

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



 
Msg#: 3127164 posted 1:07 am on Oct 21, 2006 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved