...

View Full Version : While Loop iteration problem



macleodjb
12-10-2008, 11:07 PM
I am trying to insert "<tr>" and "</tr>" at each two categories that i display.

Here's my code


<table>
<tr><td width="220px"></td><td width="220px"></td></tr>
<?php
$catnames = @getcat();
$i=0;
while ($category = @mysql_fetch_array ($catnames, MYSQL_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 ($subcatnames, MYSQL_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.



<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?

masterofollies
12-11-2008, 03:17 AM
Put the final </tr> outside of the while loop?

abduraooft
12-11-2008, 08:46 AM
http://www.codingforums.com/showthread.php?t=129466 may help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum