Page 1 of 1
Create dynamic menu from db data
Posted: Thu Jul 21, 2005 10:02 pm
by Jim_Bo
Hi,
I have the following script, what I am wanting to do is have it dynamically create the catagories, and list products under it from data that is stored in the database, and as new catagories and products are added, have it auto create the catagory link and list the products.
example code:
Code: Select all
<body bgcolor="#669900" link="#000000" vlink="#000000" alink="#000000"><div id="masterdiv">
<div class="menutitle" onClick="SwitchMenu('sub1')">Cat 1</div>
<span class="submenu" id="sub1">
- <a href="index.php?action=product1">Name1</a><br>
- <a href="index.php?action=product2">Name2</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub2')">Cat 2</div>
<span class="submenu" id="sub2">
- <a href="index.php?action=product3">Name1</a><br>
- <a href="index.php?action=product4">Name2</a><br>
- <a href="index.php?action=product5">Name3</a><br>
</span>
Cheers
Posted: Thu Jul 21, 2005 11:44 pm
by Burrito
create two tables, one with the main category and the other with the sub categories.
the main category table should be structured like this:
id - int
catname - varchar
link - varchar
the sub category structured like this:
id - int
idfromcat - int
catname - varchar
link - varchar
then when you create subs, you just populate the idfromcat with the row id from the main category table. YOu can then select all of the subs for the main category based on the id from the main category table.
ex:
Code: Select all
while($row = mysql_fetch_assoc($resultfrommain)){
echo "<a href=\"".$row['link']."\">".$catname."</a>";
$resultfromsub = mysql_query("select * from subCategories where idfromcat = ".$row['id'])
or die(mysql_error());
while($row2 = mysql_fetch_assoc($resultfromsub)){
echo " -<a href=\"".$row2['link']."\">".$row2['catname']."</a>";
//etc
}
}
Posted: Fri Jul 22, 2005 12:00 am
by Jim_Bo
Hi,
Thanks will give it ago.
Cheers
Posted: Thu Sep 15, 2005 1:27 am
by Jim_Bo
Hi,
I have given it ago .. but no real luck .. I managed to get only a single main category along with the sub categories to display.
Basically I am trying to get it so it will create a main category for each record in the main category table showing the sub categories for each:
Here is the code I have so far
Code: Select all
<style type="text/css">
.menutitle{
cursor:pointer;
margin-bottom: 3px;
background-color:#CCCCCC;
color:#000000;
width:126px;
padding:2px;
text-align:center;
font-weight:normal;
border:1px solid #000000;
}
.submenu{
margin-bottom: 0.5em;
}
</style>
<script type="text/javascript">
if (document.getElementById){
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}
function SwitchMenu(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span");
if(el.style.display != "block"){
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu")
ar[i].style.display = "none";
}
el.style.display = "block";
}else{
el.style.display = "none";
}
}
}
</script>
<noscript>Your browser does not support script</noscript>
<div id="masterdiv">
<?php
require 'db1.php';
$resultfrommain = mysql_query("SELECT * FROM maincat WHERE id = '1'") or die(mysql_error());
while($row = mysql_fetch_assoc($resultfrommain)){
$id1 = $row['id'];
$catname1 = $row['catname'];
echo "<div class=\"menutitle\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\" bordercolor=\"#000000\" onClick=\"SwitchMenu('sub$id1')\">$catname1</div>";
echo "<span class=\"submenu\" id=\"sub$id1\">";
$resultfromsub = mysql_query("SELECT * FROM subcat WHERE catid = '$id1'") or die(mysql_error());
while($row2 = mysql_fetch_assoc($resultfromsub)){
$link2 = $row2['link'];
$catname2 = $row2['catname'];
echo "- <a href=\"$link2\">$catname2</a><br>";
}
}
echo "</span>";
?>
here is a demo of it working on just one of the main categories found in the db, but it wont work foreach main category found in the db
Posted: Sun Sep 18, 2005 9:47 pm
by Jim_Bo
Hi,
I have managed to get it to display all the main categories but under each main category its only displaying one sub category when most have more than one sub ..
If I echo $name outside of <span> </span> it shows all records under the main categorys but they are always expanded.
Here is the code .. I thought that it should be displaying all the subs as its looping?
Code: Select all
<?php
require 'db1.php';
$resultfrommain = @mysql_query("SELECT * FROM maincat ORDER BY catname ASC") or die(mysql_error());
$num_rows = mysql_num_rows($resultfrommain);
for($i = 0; $i < $num_rows; $i++){
$mainid = mysql_result($resultfrommain,$i,"mainid");
$catname = mysql_result($resultfrommain,$i,"catname");
echo "<div class=\"menutitle\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\" bordercolor=\"#000000\" onClick=\"SwitchMenu('sub$mainid')\">$catname</div>";
$resultfromsub = @mysql_query("SELECT * FROM subcat WHERE catid = '$mainid'") or die(mysql_error());
$num_rows2 = mysql_num_rows($resultfromsub);
for($x = 0; $x < $num_rows2; $x++){
$link = mysql_result($resultfromsub,$x,"link");
$name = mysql_result($resultfromsub,$x,"name");
echo "<span class=\"submenu\" id=\"sub$mainid\">";
echo "- <a href=\"$link\">$name</a><br>";
echo "</span>";
}
}
?>
Thanks
Posted: Sun Sep 18, 2005 11:29 pm
by Burrito
try using the code (or a derivative of it) that I posted above.
you need to select the sub cats based on the main cat chosen and then loop over them underneath the main cat.
here is another example:
Code: Select all
<div>
<span id="one" onClick="location='page.php?subcat=1'">Category 1</span><br>
<span id="two" onClick="location='page.php?subcat=2'">Category 2</span><br>
<span id="three" onClick="location='page.php?subcat=3'">Category 3</span><br>
<span id="four" onClick="location='page.php?subcat=4'">Category 4</span><br>
</div>
now let's say one of those spans has been clicked...you might have something like the below:
Code: Select all
<?
$result = mysql_query("select * from subCats where cat = ".$_GET['subcat'])
or die(mysql_error());
echo "<span id=\"one\" onClick=\"location='page.php?subcat=1'\">Category 1</span><br>";
if(isset($_GET['subcat']) && $_GET['subcat'] == 1)
while($row = mysql_fetch_assoc($result)
{
echo " -<a href=\"".$row['page']."\">".$row['pageTitle']."</a>";
}
}
echo "<span id=\"two\" onClick=\"location='page.php?subcat=2'\">Category 2</span><br>";
if(isset($_GET['subcat']) && $_GET['subcat'] == 2)
while($row = mysql_fetch_assoc($result)
{
echo " -<a href=\"".$row['page']."\">".$row['pageTitle']."</a>";
}
}
echo "<span id=\"three\" onClick=\"location='page.php?subcat=3'\">Category 3</span><br>";
if(isset($_GET['subcat']) && $_GET['subcat'] == 3)
while($row = mysql_fetch_assoc($result)
{
echo " -<a href=\"".$row['page']."\">".$row['pageTitle']."</a>";
}
}
echo "<span id=\"four\" onClick=\"location='page.php?subcat=4'\">Category 4</span><br>";
if(isset($_GET['subcat']) && $_GET['subcat'] == 4)
while($row = mysql_fetch_assoc($result)
{
echo " -<a href=\"".$row['page']."\">".$row['pageTitle']."</a>";
}
}
Posted: Sun Sep 18, 2005 11:53 pm
by Jim_Bo
Hi,
Putting aside the above code for a sec .. I dont understand why the following code doesnt quite work properly as it apears the problem is within:
Code: Select all
<span class=\"submenu\" id=\"sub$mainid\">
As if I use
<span>$name</span>
by itself all sub cats are displayed under the main cats fine but the menu, only when I add
<span class=\"submenu\" id=\"sub$mainid\">
to the senario does it only display one sub cat under each main cat when the main cat it clicked
Code: Select all
$resultfrommain = @mysql_query("SELECT * FROM maincat ORDER BY catname ASC") or die(mysql_error());
$num_rows = mysql_num_rows($resultfrommain);
for($i = 0; $i < $num_rows; $i++){
$mainid = mysql_result($resultfrommain,$i,"mainid");
$catname = mysql_result($resultfrommain,$i,"catname");
echo "<div class=\"menutitle\" onClick=\"SwitchMenu('sub$mainid')\">$catname</div>";
$resultfromsub = @mysql_query("SELECT * FROM subcat WHERE catid = '$mainid'") or die(mysql_error());
$num_rows2 = mysql_num_rows($resultfromsub);
for($x = 0; $x < $num_rows2; $x++){
$link = mysql_result($resultfromsub,$x,"link");
$name = mysql_result($resultfromsub,$x,"name");
echo "<span class=\"submenu\" id=\"sub$mainid\">";
echo "- <a href=\"$link\">$name</a><br>";
echo "</span>";
}
}
I kinda thought I was on the right track to populate the entire menu form the db rather than doing it manually?
Thanks