Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: php new line

  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post php new line

    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

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Just place a conditional statement in there that uses the modulus of 10, e.g.
    PHP Code:
    // run through the resultset
    while( $rowmysql_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 == ) {
        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".

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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
    Dedicated Servers - sales@hivelocity.net - 1-888-869-HOST(4678)
    Award winning Managed Hosting - Dedicated Server Hosting
    Managed Dedicated Servers. Reseller Discounts. 24/7 Impressive Tech Support.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What you're saying is there is no guarentee as to the incrementing value correct?
    PHP Code:
    $iCount 0;
    while( 
    $rowmysql_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 == ) {
        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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •