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
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    While Loop iteration problem

    I am trying to insert "<tr>" and "</tr>" at each two categories that i display.

    Here's my code
    PHP Code:
    <table>
    <tr><td width="220px"></td><td width="220px"></td></tr>
    <?php
    $catnames 
    = @getcat();
    $i=0;
    while (
    $category = @mysql_fetch_array ($catnamesMYSQL_ASSOC)) {
        if((
    $i 2)==0) { echo "<tr><!--begin-->\n"; }
        echo 
    "<td valign='top'>\n";
        echo 
    "<ul>\n";    
        echo 
    "<li><h3>" $category['cat_name'] . "</h3></li>\n";
        
    $subcatnames getsubcat($category['cat_id']);
        while (
    $subcategory = @mysql_fetch_array ($subcatnamesMYSQL_ASSOC)) {
        echo 
    "<li><a href='jobs/job.php?action=search&id=" $subcategory['cat_id'] . "'>" $subcategory['cat_name'] . "</a></li>\n";
        }
        echo 
    "</ul>\n";
        echo 
    "</td>\n";
        if((
    $i 1)>= 1) { echo "</tr>\n"; }
        
    $i++; 
    }
    //End of while


    ?>
    </table>
    This is what it outputs on my webpage.

    Code:
    <table>
    <tr><td width="220px"></td><td width="220px"></td></tr>
    <tr><!--begin-->
    <td valign='top'>
    <ul>
    <li><h3>Architecture</h3></li>
    <li><a href='jobs/job.php?action=search&id=10'>Electrical</a></li>
    <li><a href='jobs/job.php?action=search&id=11'>Plumbing</a></li>
    <li><a href='jobs/job.php?action=search&id=12'>HVAC</a></li>
    <li><a href='jobs/job.php?action=search&id=13'>Structural</a></li>
    </ul>
    </td>
    <td valign='top'>
    <ul>
    <li><h3>Engineering</h3></li>
    <li><a href='jobs/job.php?action=search&id=17'>General:Engineering</a></li>
    </ul>
    </td>
    <tr><!--begin-->
    <td valign='top'>
    <ul>
    <li><h3>Design</h3></li>
    <li><a href='jobs/job.php?action=search&id=14'>Interior Design</a></li>
    <li><a href='jobs/job.php?action=search&id=15'>Graphic Design</a></li>
    <li><a href='jobs/job.php?action=search&id=16'>Furniture</a></li>
    </ul>
    </td>
    <td valign='top'>
    <ul>
    <li><h3>Automotive</h3></li>
    <li><a href='jobs/job.php?action=search&id=18'>General:Automotive</a></li>
    </ul>
    </td>
    <tr><!--begin-->
    <td valign='top'>
    <ul>
    <li><h3>Millwork</h3></li>
    <li><a href='jobs/job.php?action=search&id=6'>High End Residential</a></li>
    <li><a href='jobs/job.php?action=search&id=7'>Store Fixtures</a></li>
    <li><a href='jobs/job.php?action=search&id=8'>Hospitality</a></li>
    <li><a href='jobs/job.php?action=search&id=9'>Commercial</a></li>
    </ul>
    </td>
    </table>
    Why doesn't the final "</tr>" display properly?

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Put the final </tr> outside of the while loop?

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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