[PHP] Not sure what's going wrong here
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 shits and giggles...
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 Code:
<?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 } ?>
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-
Re: [PHP] Not sure what's going wrong here
Do you have a reference script explaining what $subs mean?
Re: [PHP] Not sure what's going wrong here
$subs is just the variable that the mysql fetch array is assigned to... here this might help:
PHP Code:
$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);
Re: [PHP] Not sure what's going wrong here
PHP Code:
$query = ("SELECT * FROM menuhead");
$result = mysql_query($query)
} else {
die("Error 4973: Please inform the web administrator of this error");
$headers = mysql_fetch_array($result);
$query2 = ("SELECT * FROM menusub");
$result2 = mysql_query($query2)
} else {
die("Error 4974: Please inform the web administrator of this error");
$subs = mysql_fetch_array($result2);
Try that.
Re: [PHP] Not sure what's going wrong here
Quote:
Originally Posted by
YoungJeezy
PHP Code:
$query = ("SELECT * FROM menuhead");
$result = mysql_query($query)
} else {
die("Error 4973: Please inform the web administrator of this error");
$headers = mysql_fetch_array($result);
$query2 = ("SELECT * FROM menusub");
$result2 = mysql_query($query2)
} else {
die("Error 4974: Please inform the web administrator of this error");
$subs = mysql_fetch_array($result2);
Try that.
I'm sorry.. but how can you expect that to work when you are opening and closing brackets that never existed to begin with? You really haven't changed anything in your suggestion either... my method of error throwing works just fine.
Re: [PHP] Not sure what's going wrong here
Yes, the error handling is perfectly fine.
But I would do it otherwise, the while loop. First make a query for your main loop, the headers. Only select the data you need (so not using *). Then every loop, you do another query for the sub's, but you use a WHERE clause to limit the results to your current header. Then print them all on screen.
Re: [PHP] Not sure what's going wrong here
How do want us to help, when half the code is missing and you havn't commented at all?
Oh and one more thing, why are using TWO tables for the same purpose?!? Head and sub menu, this could all be done a lot faster and with less code and result that would not cause such problems....
Say the table includes names and whatever you want, add a new column to tell your script whether to be head or sub menu, its 0 or 1, yes or no. SELECT * WHERE something = whatever AND headorsub = 0 and so on. Two tables can't be any advantage for you, not if you structure you data the most efficient way!!