PDA

View Full Version : not sure where this is going wrong, while - nested while


-fedexer-
06-03-2008, 08:17 PM
Well i have had to go an opposite direction from where i wanted to be heading, i am currently designing a site for a group at my school, but none of them know how to make changes etc so i am literally creating my own CMS for the crack >.>

I am currently working on having the navigation load from 2 database tables, one for the headers, and one for the sub items of those headers.

<?php while($headers = mysql_fetch_array($result)){ ?>
<tr>
<td><table>
<tr>
<th><?php if($headers['headerLink'] != NULL) {
echo "<a href=\"{$headers['headerLink']}\">{$headers['headerName']} {$headers['headerId']}</a>";
}else{
echo $headers['headerName']; echo " ".$headers['headerId'];
}?></th>
</tr>
<?php while($subs = mysql_fetch_array($result1)){
if($subs['relatedHeaderId'] == $headers['headerId']){
echo "<tr><td><a href=\"{$subs['subLink']}\">{$subs['subName']}</a></td></tr>";
}
}
?>
</table></td>
</tr>
<?php } ?>

Data retreival:


$query = sprintf("SELECT * FROM menuhead");
$result = mysql_query($query) or die("Error 4973: Please inform the web administrator of this error");
$headers = mysql_fetch_array($result);

$query1 = sprintf("SELECT * FROM menusub");
$result1 = mysql_query($query1) or die("Error 4974: Please inform the web administrator of this error");
$subs = mysql_fetch_array($result1);


The headers all get loaded fine , it's when it comes to the sub-items, being called by the second while loop nested inside the first that causes the problem, the sub items of the first header will appear, but thereafter no sub items are shown, i for the life of me can't see where it is going wrong and am looking for assistance in tracking down the problem.

Thanks guys
-fedexer-

lux
06-18-2008, 08:35 AM
this is untested but i think it should work


<?php
$query = sprintf("SELECT * FROM menuhead");
$result = mysql_query($query) or die("Error 4973: Please inform the web administrator of this error");


while($headers = mysql_fetch_array($result)){ ?>
<tr>
<td><table>
<tr>
<th><?php if($headers['headerLink'] != NULL) {
echo "<a href=\"".$headers['headerLink']."\">".$headers['headerName']." ".$headers['headerId']."</a>";
}else{
echo $headers['headerName']." ".$headers['headerId'];
}?></th>
</tr>
<?php
$query1 = sprintf("SELECT * FROM menusub");
$result1 = mysql_query($query1) or die("Error 4974: Please inform the web administrator of this error");

while($subs = mysql_fetch_array($result1)){
if($subs['relatedHeaderId'] == $headers['headerId']){
echo "<tr><td><a href=\"".$subs['subLink']."\">".$subs['subName']."</a></td></tr>";
}
}
?>
</table></td>
</tr>
<?php } ?>


sorry for not getting to you sooner

davidj
06-18-2008, 12:52 PM
i was going to say that!

lux
06-18-2008, 01:36 PM
who likes my new signature? haha

-fedexer-
06-21-2008, 06:18 PM
Sorry i should have said a long time ago that i had fixed it, but i guess i forgot >.>

davidj
06-21-2008, 11:46 PM
nice you got it fixed