...

View Full Version : creating a menu using php/mysql



Snitz
03-29-2007, 09:07 AM
I created a table via mysql called "sections" and inside it there are 4 fields


id
name
catid
incfile


Now on index.php I have ouputted the main menu links with catid=0 and the menu links with catid=3,4 and 6.

This is the menu

Home - id=1, catid=0
About us - id=2, catid=0
Services - id=3, catid=0
Products - id=4, catid=0
Maintenance - id=5, catid=0
Projects - id=6, catid=0
Contact us - id=7, catid=0
Search - id=8, catid=9999 (Not shown on the menu)
Client Login - id=9, catid=9999 (Not shown on the menu)
Software Development - id=10, catid=3 (referring to the id of Services, submenu of services)
Networking - id=11, catid=3 (referring to the id of Services, submenu of services)
Window - id=19, catid=10 (referring to the id of Software Development, submenu of Software Development)

Now this is the index.php code, I was able to output the main menu links and the submenu links but I can't figure out how to bring the sub-submenu links. Can you please help ?



<?php
# connection to the db
include ("includes/conn.php");?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Development Networking</title>
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">




<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="5">
<tr>
<td width="300" valign="top" bgcolor="#999999">

<?php $conn = mysql_query("SELECT * FROM sections WHERE catid = '0' order by id asc ");
$rows = mysql_num_rows($conn);

$ccnt = '0';
while ($rows > $ccnt) {

$connlist[$ccnt] = mysql_fetch_array($conn); ?>

<a href="index.php?id=<?php echo $connlist[$ccnt]['id'] ?>">
<?php echo $connlist[$ccnt]['name'] ;?></a>

<?php
$varconn = $connlist[$ccnt]['id'];
$conn2 = mysql_query("SELECT * FROM sections WHERE catid = '$varconn' order by name asc ");
$rows2 = mysql_num_rows($conn2);
if ($rows2 == '0') { echo "<br>"; } else {?>
<blockquote><?php
$ccnt2 = '0';
while ($rows2 > $ccnt2) {

$connlist2[$ccnt2] = mysql_fetch_array($conn2); ?>
<a href="index.php?id=<?php echo $connlist2[$ccnt2]['id'] ?>">
<?php echo $connlist2[$ccnt2]['name'] . "<br>";?>
</a>
<?php
$ccnt2++;
} ?>
</blockquote>
<?php
$varconn2 = $connlist2[$ccnt2]['id'];
$conn3 = mysql_query("SELECT * FROM sections WHERE catid = '$varconn2' order by name asc ");
$rows3 = mysql_num_rows($conn3);
$ccnt3 = '0';
while ($rows3 > $ccnt3) {
$connlist3[$ccnt3] = mysql_fetch_array($conn3);
?>
<a href="index.php?id"<?php echo $connlist3[ccnt3]['id'] ?>">
<?php
// there is something wrong with the query
echo $connlist3[$ccnt3]['name'] . "<br>";
?>
<?php
$ccnt3++;
} ?>
<?php
}

$ccnt++;
}?></td>
<td valign="top" bgcolor="#000000">&nbsp;</td>
</tr>
</table>
</body>
</html>





<?php
// to close the connection with the database after getting what you asked
mysql_close() ?>

aedrin
03-29-2007, 03:59 PM
You want to store a tree structure. Just search for a tutorial on that, which will more than likely help you on the way.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum