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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    107
    Thanks
    15
    Thanked 0 Times in 0 Posts

    controll mysql query numbers

    Just wondering,

    is there a way , in which, for example

    you could do a "while row=" condition

    but only echo 4 before starting a new line?

    if any one gets what i mean?

    so say i wanted to fetch the users names from my database

    i could do

    Ben, James, Katie, Amy,
    Sarah, Tom, Adam, Ian,
    Alex,Tara,Zack,Slater,
    Kelly,Jessee,Samuel,Willem

    so like "show 4 then begin new line"

    rather than it looking like Ben, James, Katie, Amy, Sarah, Tom, Adam, Ian, Alex,Tara,Zack,Slater, Kelly,Jessee,Samuel,Willem


    thanks allot in advance

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    why not just use a variable counter then divide that counter by 4 if the division is a whole number then echo a line break

    PHP Code:



    $sql 
    "SELECT name FROM users";

    $result mysql_query($sql);
    $num_rows mysql_num_rows($result);

    $i=1;
    while (
    $row mysql_fetch_assoc($result)) 
    {
    echo (
    $row["name"]);
    if(
    is_int($i/4))
        echo 
    ", <br />";
    elseif(
    $i!=$num_rows)
        echo 
    ", ";

    $i++;

    Last edited by timgolding; 03-20-2009 at 07:36 PM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    However this might not look so neat if say the first few names in the database were a lot longer than the 5th to 8th. Say you had
    "Coillcumhann, Bernadette, Wilhelmina, Emmanual, Ted, Tom, Tim, Zoe"

    That would look like

    Coillcumhann, Bernadette, Wilhelmina, Emmanual,
    Ted, Tom, Tim, Zoe

    Which would kind of defeat the point..

    I would just put the names in a div and let the html decide how many names to put on each line

    PHP Code:
    $sql "SELECT name FROM users";

    $result mysql_query($sql);
    $num_rows mysql_num_rows($result);

    echo 
    '<div style="width:26em;">';

    $i=1;
    while (
    $row mysql_fetch_assoc($result)) 
    {
     echo 
    $row["name"];
     if(
    $i!=$num_rows)
           echo 
    ", ";
     
    $i++;
    }

    echo 
    "</div>"
    Last edited by timgolding; 03-20-2009 at 07:53 PM.
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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