Click to See Complete Forum and Search --> : Dynamic Navbar


CWCJimmy
12-22-2005, 03:06 PM
I'm a first-time poster so please be gentle!

I'm having a bit of a problem. I'm trying to create a click-to-collapse side-navigation bar that is populated by a MySQL database that pulls the name and sorts via number. I've gotten the first teir working, but when running the second teir it will only populate the first item in the first teir. It skips the rest.

This is what I want it to do:

Division 1
--Department 1
--Department 2
Division 2
--Department 1
--Department 2
Division 3
--Department 1
--Department 2

This is what it is doing:

Division 1
--Department 1
--Department 2
Division 2
Division 3


Here is my code:


<html>
<head>
<style>
<!-- div#myToggledDiv {display: none;} -->
</style>

<script type="text/javascript">
function swapMyToggledDiv(div)
{
if (document.getElementById(div).style.display == "block")
{ document.getElementById(div).style.display = "none"; }
else
{ document.getElementById(div).style.display = "block";}
}
</script>

<body>

<?php
include("connect.php");


//Setting up Queries
$get_division = "SELECT div_name, div_num FROM division ORDER BY div_num;";
$get_dept = "SELECT dept_name, dept_num FROM dept ORDER BY dept_num;";
$get_productline = "SELECT prodline_name, prodline_num FROM product_line ORDER BY prodline_num;";

//Running Queries
$division_results = mysql_query($get_division) or die(mysql_error());
$dept_results = mysql_query($get_dept) or die(mysql_error());
$productline_results = mysql_query($get_productline) or die(mysql_error());

if (mysql_num_rows($division_results) < 1 && mysql_num_rows($dept_results) < 1 && mysql_num_rows($productline_results) < 1)
{
$display_block = "<P><em>Sorry, no categories to browse.</em></p>";
}
else
{
while ($div = mysql_fetch_array($division_results))
{
$div_num = $div[div_num];
$div_name_u = strtoupper(stripslashes($div[div_name]));
$div_name = $div[div_name];

$display_block .= "<div onClick=\"swapMyToggledDiv('$div_name')\"> \n\t <img src='closed.gif'> \n\t <b> $div_name_u </b> \n </div> \n <div id=\"$div_name\">\n";

while ($dept = mysql_fetch_array($dept_results))
{
$dept_num = $dept[dept_num];
$dept_name_u = strtoupper(stripslashes($dept[dept_name]));
$dept_name = $dept[dept_name];

if($dept_test = substr($dept_num,0,-1) == $div_num)
{
$display_block .= "<div onClick=\"swapMyToggledDiv('$dept_name')\"> \n\t <img src='closed.gif'> \n\t <b> $dept_name_u </b> <br> \n </div> \n<div id=\"$dept_name\">";

$display_block .="</div>\n\n";
}
}

unset($dept);

$display_block .="</div>\n\n";
}
}

echo ($display_block);
?>

</body>
</html>



Any help would be greatly appreciated!

Thanks!

Sheldon
12-22-2005, 05:35 PM
Maybe a javascfript error? But im no god at either PHP or JS so i dont no :)

chazzy
12-22-2005, 08:29 PM
would you mind showing the results of view-source on this page? at least for the relevant section. attachment would be great.

this is one of those php is reacting weird to a programmer wanting it to be dynamic types of things.

CWCJimmy
12-23-2005, 08:22 AM
I sure would,

Thanks for your help!



<html>
<head>
<style>
<!-- div#myToggledDiv {display: none;} -->
</style>

<script type="text/javascript">
function swapMyToggledDiv(div)
{
if (document.getElementById(div).style.display == "block")
{ document.getElementById(div).style.display = "none"; }
else
{ document.getElementById(div).style.display = "block";}
}
</script>

<body>

<div onClick="swapMyToggledDiv('Hardlines')">
<img src='closed.gif'>
<b> HARDLINES </b>
</div>
<div id="Hardlines">
<div onClick="swapMyToggledDiv('Gifts')">
&nbsp; &nbsp; <img src='closed.gif'>
<b> GIFTS </b> <br>
</div>
<div id="Gifts"></div>

<div onClick="swapMyToggledDiv('Housewares')">
&nbsp; &nbsp; <img src='closed.gif'>
<b> HOUSEWARES </b> <br>
</div>
<div id="Housewares"></div>

</div>

<div onClick="swapMyToggledDiv('Grocery')">
<img src='closed.gif'>
<b> GROCERY </b>
</div>
<div id="Grocery">
</div>

<div onClick="swapMyToggledDiv('Sporting Goods')">
<img src='closed.gif'>
<b> SPORTING GOODS </b>
</div>
<div id="Sporting Goods">
</div>


</body>
</html>

chazzy
12-23-2005, 09:17 AM
Ok, so you know that there's nothing being populated inside the Gifts and Housewares div's right? Is there supposed to be, based on your database?

I see what's going on.

You're only issuing your query once. After you go through the first category, you're not getting the next category's data, so you don't have any new data to populate under the other headings.

Try moving your query inside the first loop and loop around its results for each main heading you have (IE- for each main heading you have, do a query to get the subheadings for each of them) Make sense?

CWCJimmy
12-24-2005, 10:32 AM
Yep, I'm aware that nothing is being populated in Gifts & Housewares, and yes in my database it is supposed to be. I haven't written an output for that yet. I figured I'd have the exact same problem as I do for the 'department' items.

Please correct me if I'm mistaken, I thought I could issue the query once, have it saved to a variable, and use the data over and over from the variable instead of running the query multiple times. Perhaps PHP doesn't like that.

As I've read a bit more about PHP I've found that the arrays have save a sort of identifier as to what has been read and what hasn't, or I should say what element it is on. I thought that it may be an issue of having gone through the array once it's at it's end and won't go through it again. That's why I have the unset($dept); in there, hoping it would fix my prob. But alas it did not.

I will certainly try your suggestion though. I really do appreciate your help!

Thanks!

chazzy
12-24-2005, 10:40 AM
Unfortunately, MySQL doesn't support a hierarchical query structure. Ideally you'd want to do something like that (it involves joining a table on itself and ordering the results based on their parent row). They actually had an article on it some time ago.

For this though, you'll need one query per parent row.

CWCJimmy
12-27-2005, 09:57 AM
Fixed!

It works now. I kicked the query into the loop and it works now. Thanks Chazzy!