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
    New to the CF scene
    Join Date
    May 2013
    Location
    Russia - Kazan
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    while mysql_fetch_array

    Hello all thanks for the nice forum i am newbie to php trying to understand it and here what i need help with :
    PHP Code:
    while($row mysql_fetch_array($qury))
                   echo 
    " <tr> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> </tr>";
                 
                   
     echo  
    "</thead>"
         
    ."</table>"

    it's fetch rows from the table all working fine but what i am trying to do is every 4 record after it echo html tags
    Code:
    <tr>
    and
    Code:
    </tr>
    and in the end </tr>
    thanks

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You can just introduce a counter and some "if" statements to get the desired effect:

    PHP Code:
    $counter=0;
    while(
    $row mysql_fetch_array($qury)){
        if(
    $counter==4){
            echo 
    "<tr>";
        }
        echo 
    " <tr> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> </tr>";
        if(
    $counter==4){
            
    $counter 0;
            echo 
    "</tr>";
        }
        
    $counter++;
    }
    echo  
    "</thead></table>"
    That should work, but the result won't be a valid table structure according to W3C standards unless you make some changes to the rest of your code.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Junsee
    Guest
    Quote Originally Posted by Rowsdower! View Post
    You can just introduce a counter and some "if" statements to get the desired effect:

    PHP Code:
    $counter=0;
    while(
    $row mysql_fetch_array($qury)){
        if(
    $counter==4){
            echo 
    "<tr>";
        }
        echo 
    " <tr> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> <th>$row[button]</th> </tr>";
        if(
    $counter==4){
            
    $counter 0;
            echo 
    "</tr>";
        }
        
    $counter++;
    }
    echo  
    "</thead></table>"
    That should work, but the result won't be a valid table structure according to W3C standards unless you make some changes to the rest of your code.

    The above is perfect, I just wanted to add an alternative
    you can use modulus and looks like this


    PHP Code:
    echo  "<table>";
    echo 
    "<tr>"

    $counter=0
    while(
    $row mysql_fetch_array($qury)){ 

        echo 
    "<td>".$row['button']."</td>";
        echo 
    "<td>".$row['button']."</td>";
        echo 
    "<td>".$row['button']."</td>";
        echo 
    "<td>".$row['button']."</td>";

        
    $counter++; 
        if(
    $counter%== 0){ 
            echo 
    "</tr>"
            echo 
    "<tr>"
        } 

    echo 
    "</tr>";
    echo  
    "</table>"
    What this does is say, if you divide $counter by 4, and the remainder is 0 (which is a maths way of saying equal to) then run this code.

    Also I have put in td, which is the table standard. The th is really for table headers. No difference just arming you with knowledge
    Last edited by Junsee; 05-28-2013 at 02:45 PM.


  •  

    Posting Permissions

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