/    Sign up×
Community /Pin to ProfileBookmark

Dynamic Navbar

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:

[b]Division 1[/b]
–Department 1
–Department 2
[b]Division 2[/b]
–Department 1
–Department 2
[b]Division 3[/b]
–Department 1
–Department 2

This is what it is doing:

[b]Division 1[/b]
–Department 1
–Department 2
[b]Division 2
Division 3[/b]

Here is my code:

[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’)”> nt <img src=’closed.gif’> nt <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’)”> nt <img src=’closed.gif’> nt <b> $dept_name_u </b> <br> n </div> n<div id=”$dept_name”>”;

$display_block .=”</div>nn”;
}
}

unset($dept);

$display_block .=”</div>nn”;
}
}

echo ($display_block);
?>

</body>
</html>
[/code]

Any help would be greatly appreciated!

Thanks!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SheldonDec 22.2005 — Maybe a javascfript error? But im no god at either PHP or JS so i dont no ?
Copy linkTweet thisAlerts:
@chazzyDec 23.2005 — 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.
Copy linkTweet thisAlerts:
@CWCJimmyauthorDec 23.2005 — I sure would,

Thanks for your help!



[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>

<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>
[/CODE]
Copy linkTweet thisAlerts:
@chazzyDec 23.2005 — 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?
Copy linkTweet thisAlerts:
@CWCJimmyauthorDec 24.2005 — 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!
Copy linkTweet thisAlerts:
@chazzyDec 24.2005 — 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.
Copy linkTweet thisAlerts:
@CWCJimmyauthorDec 27.2005 — Fixed!

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

Success!

Help @CWCJimmy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...