473,395 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple menu, MySQL query question

Hello, I'm creating simple menu.

MySQL:

+----+-----+----------------------+
| id | sid | Title |
+----+-----+----------------------+
| 1 | 0 | Main Menu 1
| 2 | 0 | Main Menu 2
| 3 | 1 | SubMenu 1 of menu 1
| 4 | 1 | SubMenu 2 of menu 1
| 5 | 1 | SubMenu 3 of menu 1
| 6 | 2 | SubMenu 1 of menu 2
+----+-----+----------------------

What should be my query? I'm trying to select all titles, and display them:

Main Menu 1
- SubMenu 1 of menu 1
- SubMenu 2 of menu 1
- SubMenu 3 of menu 1
Main Menu 2
- SubMenu 1 of menu 2

TNX

Jul 17 '05 #1
4 8454
Hey,
First, I dont know how you structuring SID, but if you just have it setup so
you can only have 1 sub-menu, then according to normal forms, then that
d-base design is wrong. I would retitle "sid" to "parent", which refers to
the "id" of the parent menu. ("0" would be the main parent of course)

As far as displaying them, unless you want just 1 parent and its immediate
underlings, then you are going to want to retrive the entire table and sort
it in PHP, so the SQL you would use is "SELECT * FROM menu_table;" Like I
said, you would have to use PHP to sort everything and format it.
-Eric Kincl

dr. zoidberg wrote:
Hello, I'm creating simple menu.

MySQL:

+----+-----+----------------------+
| id | sid | Title |
+----+-----+----------------------+
| 1 | 0 | Main Menu 1
| 2 | 0 | Main Menu 2
| 3 | 1 | SubMenu 1 of menu 1
| 4 | 1 | SubMenu 2 of menu 1
| 5 | 1 | SubMenu 3 of menu 1
| 6 | 2 | SubMenu 1 of menu 2
+----+-----+----------------------

What should be my query? I'm trying to select all titles, and display
them:

Main Menu 1
- SubMenu 1 of menu 1
- SubMenu 2 of menu 1
- SubMenu 3 of menu 1
Main Menu 2
- SubMenu 1 of menu 2

TNX


Jul 17 '05 #2
On Mon, 10 Nov 2003 22:57:02 +0100, "dr. zoidberg" <so*****@example.wrong>
wrote:
Hello, I'm creating simple menu.

MySQL:

+----+-----+----------------------+
| id | sid | Title |
+----+-----+----------------------+
| 1 | 0 | Main Menu 1
| 2 | 0 | Main Menu 2
| 3 | 1 | SubMenu 1 of menu 1
| 4 | 1 | SubMenu 2 of menu 1
| 5 | 1 | SubMenu 3 of menu 1
| 6 | 2 | SubMenu 1 of menu 2
+----+-----+----------------------

What should be my query? I'm trying to select all titles, and display them:

Main Menu 1
- SubMenu 1 of menu 1
- SubMenu 2 of menu 1
- SubMenu 3 of menu 1
Main Menu 2
- SubMenu 1 of menu 2


MySQL doesn't have any hierarchical query extensions to SQL, so you can't
really get that in a single query. The closest that comes to mind, assuming
only one level of submenus, might be:

mysql> select main.id main_id, main.title main_title,
-> sub.id sub_id, sub.title sub_title
-> from menu main
-> inner join menu sub on (main.id = sub.sid)
-> where main.sid = 0
-> order by main.id, sub.id;
+---------+-------------+--------+---------------------+
| main_id | main_title | sub_id | sub_title |
+---------+-------------+--------+---------------------+
| 1 | Main Menu 1 | 3 | SubMenu 1 of menu 1 |
| 1 | Main Menu 1 | 4 | SubMenu 2 of menu 1 |
| 1 | Main Menu 1 | 5 | SubMenu 3 of menu 1 |
| 2 | Main Menu 2 | 6 | SubMenu 1 of menu 2 |
+---------+-------------+--------+---------------------+
4 rows in set (0.01 sec)

For a more general solution you might want to denormalise some fields, making
updating the menu more expensive, but reading it trivial. See this post for
some ideas:

http://groups.google.com/groups?hl=e...4ax.com&rnum=3

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3
Eric Kincl wrote:
Hey,
First, I dont know how you structuring SID, but if you just have it setup so
you can only have 1 sub-menu, then according to normal forms, then that
d-base design is wrong. I would retitle "sid" to "parent", which refers to
the "id" of the parent menu. ("0" would be the main parent of course)

As far as displaying them, unless you want just 1 parent and its immediate
underlings, then you are going to want to retrive the entire table and sort
it in PHP, so the SQL you would use is "SELECT * FROM menu_table;" Like I
said, you would have to use PHP to sort everything and format it.


This is probably a good solution. Getting to the "use PHP to sort
everything and format it" issue, I recommend a recursive function.

hmmm.. I seem to have written the whole dang thang :/
well, that was fun :)

It's an XML implementation, If you want me to show you how to make it
output HTML, just ask.

<?php
class MenuMaker {

var $objDom;
var $arrResult;

function MenuMaker($arrResult) {
$this->arrResult = $arrResult;
$this->objDom = domxml_new_doc("1.0");
$ndRoot =
$this->objDom->append_child($this->objDom->create_element("menu"));
foreach($this->arrResult AS $arrTopRow) {
if($arrTopRow["parent_id"] == 0) {
$this->handleMenuItem($arrTopRow,$ndRoot);
}
}
}

function handleMenuItem($arrRow,$nd) {

// display the thing...
$ndItem = $this->printMenuItem($arrRow,$nd);

//{{{ create a temporrary stack of immediate children
$arrTmp = array();
foreach($this->arrResult AS $arrSubRow) {
if($arrRow["id"] == $arrSubRow["parent_id"]) {
$arrTmp[] = $arrSubRow;
}
}
//}}}

// execute the immdiate children in a row (ordered)
foreach($arrTmp AS $arrSubRow){
$this->handleMenuItem($arrSubRow,$ndItem);
}
}

function printMenuItem($arrRow,$nd) {
$ndItem = $nd->append_child($this->objDom->create_element("item"));
foreach($arrRow AS $key=>$val) {
$ndTitle =
$ndItem->append_child($this->objDom->create_element($key));
$ndTitle->set_content($val);
}
return $ndItem;
}

function getXml() {
return $this->objDom->dump_mem(true);
}
}

$arrFakeResult = array();
$arrFakeResult[] =
array("id"=>1,"parent_id"=>0,"title"=>"project","h ref"=>"project/");
$arrFakeResult[] =
array("id"=>2,"parent_id"=>0,"title"=>"faq","href" =>"faq/");
$arrFakeResult[] =
array("id"=>3,"parent_id"=>0,"title"=>"contact","h ref"=>"contact.php");
$arrFakeResult[] =
array("id"=>4,"parent_id"=>1,"title"=>"aims","href "=>"project/aims/");
$arrFakeResult[] =
array("id"=>5,"parent_id"=>1,"title"=>"objectives" ,"href"=>"project/obj/");
$arrFakeResult[] =
array("id"=>6,"parent_id"=>4,"title"=>"easy","href "=>"project/aims/easy.php");
$arrFakeResult[] =
array("id"=>7,"parent_id"=>4,"title"=>"scalable"," href"=>"project/aims/sclable.php");
$arrFakeResult[] =
array("id"=>8,"parent_id"=>4,"title"=>"robust","hr ef"=>"project/aims/robust.php");
$arrFakeResult[] = array("id"=>9,"parent_id"=>5,"title"=>"object
oriented","href"=>"project/obj/oo.php");
$arrFakeResult[] =
array("id"=>10,"parent_id"=>5,"title"=>"xml","href "=>"project/obj/xml.php");

$objMM = new MenuMaker($arrFakeResult);
header("Content-Type: text/xml");
echo $objMM->getXml();

?>

Jul 17 '05 #4
Actually, I was originally gonna make it output it in HTML, but HTML is
so limited. Anyway, this is the reason for using a temporary stack. A
dead-end format (HTML) would require me to group things level by level.
XML does not require this. Now that it is XML, you can remove the extra
code for the temporary stack.

The handleMenuItem is now smaller/simpler.

function handleMenuItem($arrRow,$nd) {

// display the thing...
$ndItem = $this->printMenuItem($arrRow,$nd);

foreach($this->arrResult AS $arrSubRow) {
if($arrRow["id"] == $arrSubRow["parent_id"]) {
$this->handleMenuItem($arrSubRow,$ndItem);
}
}
}

PS. did I mention that the number of sub-levels is essentially unlimited :)

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maciej Nadolski | last post by:
Hi! I`ve got a simple question but I`m puzzled:( When I create variable: for example $query for query to MySQL its obvieus that I want to use variables. Now should I do something like that: 1)...
2
by: Jorntk | last post by:
How can I make a drop down menu that are dynamically generated base on the value selected in another drop down menu? values in both menu will need to be from mysql query.
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
4
by: yogesh | last post by:
mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 compiler version ... #include <stdio.h> #include <mysql.h> #include <string.h> int main() {
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
7
by: dongletran06 | last post by:
Hi, Please help me find out what wrong with my codes in inputting from my form to mysql database using drop down menu. Below is the codes I used. Only the drop down is not working but the "input...
3
by: SQLjunky | last post by:
Hello, Contrary to what my screen name indicates, i'm actually quite new to SQL. Having said that, I'll get right into my problem. I'm trying to query an mssql database to return to the value...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
0
by: stacionari | last post by:
HI, My query is like this: SELECT tag.* FROM tag LEFT JOIN book_item ON tag.Tag_ID = book_item.BookItem_ID WHERE tag.Tag = "tags" GROUP BY tag.tag_ID Indexes: Table 'tag': Keyname Type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.