...

View Full Version : php new line



jonathan1212
02-04-2010, 02:31 AM
helo to everybody here,
i need your help
how can i do it to new line?

$qstr = "SELECT * FROM `post`";

$res = mysql_query($qstr, $dbh) or die(mysql_error());

while ($row = mysql_fetch_assoc($res)) {

echo 'id='.$row["id"].'  &nbsp';

}


when the value exceed to 10,20,30,, so on.....
it will automatically make a new line.

look like this...
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
so on....

pls help

bdl
02-04-2010, 02:52 AM
Just place a conditional statement in there that uses the modulus of 10, e.g.


// run through the resultset
while( $row= mysql_fetch_assoc($res) ) {
// output your value
echo "{$row['id']} ";

// if the id value modulus 10 is 0, output a BR element
if ( $row['id'] % 10 == 0 ) {
echo "<br/>";
}
}


I'm assuming this is output in HTML, since you're using &nbsp;, so I've used a BR element in there. If you truly want a newline (as if you're working with a text file), then you'd want to output "\n".

hivelocitydd
02-04-2010, 06:35 PM
The above code looks good .. but I doubt if the $row['id'] is set to '0' it will make an additional new line in the starting. Normally the chance is less if you are using auto increment. But there can be.

In that case this will create a new line at the start of display .. if its not a matter ... the code looks perfect for you

Fou-Lu
02-04-2010, 06:55 PM
What you're saying is there is no guarentee as to the incrementing value correct?


$iCount = 0;
while( $row= mysql_fetch_assoc($res) ) {
// output your value
echo "{$row['id']}&nbsp;";

// if the id value modulus 10 is 0, output a BR element
if ( ++$iCount % 10 == 0 ) {
echo "<br/>";
}
}


I would actually avoid using <br /> and opt for a PHP_EOL. This is not an html break, but you can convert you're output by capturing it and printing it through an nl2br function. This will let you swap out a graphical ui like a browser for a text interface like the cli with little alterations (a simple check on environment really).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum