Hi,
I have searched about for this one but havent quite found the low down i'm looking for but here goes.
I'm trying to dynamically populate an unordered list from an SQL database which will have upto 5 or 6 sub-selections per top level list item so... it would do something like this:
link1
link2
link3
select link2 >
link1
link2
-link1
-link2
link3
select link1 >
link1
link2
-link1
--link1
--link2
-link2
link3
if at this stage you were to select a different top level link it would collapse the previous expanded list and start expanding on the new selection.
and so on.... I have been attempting this with only a little success using if/elseif statements and have come up with so far thus :
<?php
/*if no category has been selected load default top level list*/
if (!$_GET['cat'] || $_GET['cat'] == "")
{
echo("<div id=\"welcome\">\n\t\t\t\t<h2>Categories</h2>\n\t\t\t\t<br />\n\t\t\t</div>\n");
$categories = mysql_query("SELECT * FROM materials_categories");
echo("\n\t\t\t<div class=\"detail\">\n\t\t\t\t<ul>");
WHILE($category_item = mysql_fetch_array($categories))
{
$category_name = $category_item['category'];
echo("\n\t\t\t\t\t<li><a href=\"materials.php?cat=$category_name\">$category_name</a></li>");
}
echo("\n\t\t\t\t</ul>\n\t\t\t");
echo("</div>\n\t\t\t");
}
/*if second level category is chosen expand list*/
elseif ($_GET['cat'] AND !$GET_['subcat'])
{
echo("\n\t\t\t\t\t<div id=\"welcome\">\n\t\t\t\t\t\t<h2>Select</h2>\n\t\t\t\t\t\t<br />\n\t\t\t\t\t</div>");
$main_category = $_GET['cat'];
$categories = mysql_query("SELECT * FROM materials_categories");
echo("\n\t\t\t<div class=\"detail\">\n\t\t\t\t<ul>");
WHILE($category_item = mysql_fetch_array($categories))
{
$category_name = $category_item['category'];
echo("\n\t\t\t\t\t<li><a href=\"materials.php?cat=$main_category\">$category_name</a>");
$selection = mysql_query("SELECT * FROM $main_category");
echo("\n\t\t\t\t\t<ul>");
WHILE($selected = mysql_fetch_array($selection))
{
$select = $selected['category'];
echo("\n\t\t\t\t\t\t<li><a href=\"materials.php?cat=$main_category&subcat=$select\">$select</a></li>");
}
}
echo("\n\t\t\t\t\t</ul>\n\t\t\t\t");
echo("\n\t\t\t\t\t</li>\n\t\t\t\t");
echo("\n\t\t\t\t</ul>\n\t\t\t");
echo("</div>\n\t\t\t");
}
/*if third level category is chosen expand list*/
elseif ($_GET['subcat'] AND $GET_['cat'])
{
echo("\n\t\t\t\t\t<div id=\"welcome\">\n\t\t\t\t\t\t<h2>Select</h2>\n\t\t\t\t\t\t<br />\n\t\t\t\t\t</div>");
$main_category = $_GET['cat'];
$sub_category = $_GET['subcat'];
$categories1 = mysql_query("SELECT * FROM materials_categories");
echo("\n\t\t\t<div class=\"detail\">\n\t\t\t\t<ul>");
WHILE($selected1 = mysql_fetch_array($categories1))
{
$select1 = $selected1['category'];
echo("\n\t\t\t\t\t<li><a href=\"materials.php?cat=$main_category\">$main_category</a>");
$categories2 = mysql_query("SELECT * FROM $main_category");
echo("\n\t\t\t\t\t<ul>");
WHILE($selected2 = mysql_fetch_array($categories2))
{
$select2 = $selected2['category'];
echo("\n\t\t\t\t\t\t<li><a href=\"materials.php?cat=$main_category&subcat=$select2\">$select</a>");
$categories3 = mysql_query("SELECT * FROM $sub_category");
echo("\n\t\t\t\t\t\t<ul>");
WHILE($selected3 = mysql_fetch_array($categories3))
{
$select3 = $selected3['category'];
echo("\n\t\t\t\t\t\t\t<li><a href=\"materials.php?cat=$main_category&subcat=$select2&man=$select3\">$select3</a>");
}
echo("\n\t\t\t\t\t\t\t</ul>\n\t\t\t\t\t\t");
echo("\n\t\t\t\t\t\t\t</li>\n\t\t\t\t\t\t");
}
echo("\n\t\t\t\t\t\t</ul>\n\t\t\t\t\t");
echo("\n\t\t\t\t\t\t</li>\n\t\t\t\t\t");
}
echo("\n\t\t\t\t\t</ul>\n\t\t\t\t");
echo("\n\t\t\t\t\t</li>\n\t\t\t\t");
echo("\n\t\t\t\t</ul>\n\t\t\t");
echo("</div>\n\t\t\t");
}
?>
please ignore the messy looking formatting and syntax its only there for a tidy looking client side page source and apologys for the apparently meaningless variables this is only due to me re-writing and uploading this to the server so many times, that it has gotten a little lost on the way :bemused:
if the code snippet is to unbearable i will try to summarize if needed, but i assumed that for some this is like reading a kiddies book but clearly myself not included 😃
many thanks in return for any help here
sean.