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
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    echo <tr> every 2nd time through loop

    How can I echo the <tr> for a new row every 2nd time through the if loop? I have tried to add a for loop using ($i = 1; $i <=$columns; $i++) but couldn't figure it out. I want the table to be 2 columns wide.

    PHP Code:
    <?php

    $columns 
    2;

    echo 
    "<table border=\"2\">";
    if (
    $handle opendir('images/')) {

    while (
    false !== ($file readdir($handle))) {

    if (
    $file != "." && $file != "..") {

    echo 
    "<td><img src=\"images/$file\"></td>";

    echo 
    "<tr>";

    }
    }

    closedir($handle);
    }

    echo 
    "</table>";

    ?>
    Last edited by Len Whistler; 01-28-2007 at 03:16 AM.
    Leonard Whistler

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    
    $columns = 2;
    $count = 0;
    
    echo "<table border=\"2\">";
    if ($handle = opendir('images/')) {
    
    while (false !== ($file = readdir($handle))) {
    
    if ($file != "." && $file != "..") {
    
    echo "<td><img src=\"images/$file\"></td>";
    
    $count++;
    
    if ($count == 2)
    { 
     echo "</tr><tr>"; 
     $count = 0;
    }
    
    }
    }
    
    closedir($handle);
    }
    
    echo "</table>";
    
    ?>
    I highlighted the areas I modified, I'm pretty sure this is what you wanted, if not you should have the idea now

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Thanks Erockrm1, It works perfectly
    Leonard Whistler


  •  

    Posting Permissions

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