MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 22nd, 2003, 03:29 PM
cezar cezar is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 1 cezar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question PHP, Mysql Database Driven Menu

Dear All,

I have problem with creating a DHTML horizontal menu that is database driven.
My Menu is created using 2 javascript file, and I call those 2 files from a PHP file header.php

In this header.php, I open up connection and choose database, and then do sql query and get the data using 'for loop', and then I passed the values to javascript using 'input = hidden tag'

but then i realized that because the content of the menu is from database, so i dont know exactly how many titles and sub-titles in the menu. The question is how should i construct a for loop in the javascript ? The conditions ?

This is how I construct my database:
Fields: Id, title*, url, parentId**

*This are what is shown in the menu
** ParentId = 0 means it's the main title
ParentId = 1 means it's the it's the sub-title for main title 1


This is what I include in the header.php beside the images
PHP Code:
<?php

//Create SQL for connecting to mysql
$sqlConn mysql_connect("localhost""username""pass") or die("Couldn't connect to database");

//Connect to database
$dbConn mysql_select_db("menu"$sqlConn) or die("Couldn't connect to database");

//SQL for search for RootNode
$sql "select parentId from nodes";

//Get the number of titles that are needed in the configuration file
$sqlParent ="select * from nodes where parentId = 0";
$totalParent mysql_query($sqlParent) or die("Select parent query failed");
$totalParentResults mysql_num_rows($totalParent);
?>
<input type = "hidden" name= "totalParent" value=<?=$totalParentResults?>>

<?
//Execute the SQL for number of total nodes
$totalId mysql_query($sql) or die("SELECT query failed");

//Put the number of parentId to $num_results
$totalIdResults mysql_num_rows($totalId);
?>
<input type = "hidden" name="totalIdResults" value =<?=totalIdResults?>>

<?
//
for ($i 0$i < ($totalIdResults-1); $i++)
{
    
//Query to get data from each parentId
    
$sql$i "select * from nodes where parentId =".$i;

    
//Execute Query
    
$node$iResult mysql_query($sql$i) or die ("Select parentId ".$i" query failed");

    
//Get data from the query
    
$node$i mysql_fetch_array($node$iResult);

    
//Loop to put all the data into a certain variable to be passed to the javascript file
    
for ($x=1$x < ($total_results-1); $x++)
    {
        
$node$i_$x $node$i['title'];
        <
input type "hidden\" name = \"node\".$i_$x value=$node$i_$x>
        $url$i_$x = $url$i['url'];
        <input type = \"hidden\" name = \"url\".$i$x value=$url$i_$x>
    }
}
?>


And this is how it looks like after I get the values in the javascript:
Quote:
Menu1=new Array("Home","","",5,18,60); // This is the main title 1
Menu1_1= new Array("Traditional Home Business Directory", "traditional.htm", "",0,20,150); //Sub-title 1
Menu1_2= new Array("Web Business Directory", "directory.htm", "",0); //Sub-title 2

Menu2=new Array("Our Company","","",5,18,120); //Main title 2
Menu2_1= new Array("Traditional Home Business Directory", "traditional.htm", "",0,20,150); //Sub-title 1
Menu2_2= new Array("Web Business Directory", "directory.htm", "",0); //Sub-title 2


Anybody can help me, or can think of a better way to do it ?
I really appreciate any help.

Thanks,

Cezar

Reply With Quote
  #2  
Old June 22nd, 2003, 07:31 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
I've recently used the exact same menu system and implemented it using ASP.

I write out the menu items directly to the page from the ASP (it looks like your doing something involving hidden fields? Looks like you might be making it harder than necessary)

Here's what I did (done server-side):
1. Perform the query to get the menu items
2. Get the number of records so that you can setup the menu array properly.
3. Begin a loop to go thru and write out each menu item (and sub's)
4. Before you perform the writes tho, you need to perform a count on the number of subs.
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > PHP, Mysql Database Driven Menu


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway