473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8457
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.