473,748 Members | 4,065 Online
Bytes | Software Development & Data Engineering Community
+ 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 8474
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*****@exampl e.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($arrR esult) {
$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,$nd Root);
}
}
}

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,$nd Item);
}
}

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($v al);
}
return $ndItem;
}

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

$arrFakeResult = array();
$arrFakeResult[] =
array("id"=>1," parent_id"=>0," title"=>"projec t","href"=>"pro ject/");
$arrFakeResult[] =
array("id"=>2," parent_id"=>0," title"=>"faq"," href"=>"faq/");
$arrFakeResult[] =
array("id"=>3," parent_id"=>0," title"=>"contac t","href"=>"con tact.php");
$arrFakeResult[] =
array("id"=>4," parent_id"=>1," title"=>"aims", "href"=>"projec t/aims/");
$arrFakeResult[] =
array("id"=>5," parent_id"=>1," title"=>"object ives","href"=>" project/obj/");
$arrFakeResult[] =
array("id"=>6," parent_id"=>4," title"=>"easy", "href"=>"projec t/aims/easy.php");
$arrFakeResult[] =
array("id"=>7," parent_id"=>4," title"=>"scalab le","href"=>"pr oject/aims/sclable.php");
$arrFakeResult[] =
array("id"=>8," parent_id"=>4," title"=>"robust ","href"=>"proj ect/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"=>"projec t/obj/xml.php");

$objMM = new MenuMaker($arrF akeResult);
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,$nd Item);
}
}
}

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
1939
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) $query = "blahblahblah".$variable1."blahblahblah" ."blahblahblah".$variable2."blahblahblah"; OR 2) $query = "blahblahblah."$variable1."blahblahblah" ."blahblahblah."$variable2."blahblahblah";
2
3192
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
2648
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 directly. so I need a way doing this. so I check to see if the ifp value is null and if so then assign it a value. is this correct?
4
3043
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
5254
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, but I am unable to store a variable for it. Here is a rough copy of what I am making: <html> <head> <title>Add New MySQL User</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head>
7
2504
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 text" are successfully adding data to mysql. I just try first using the month as drop down. The month data type I used in mysql database is varchar. Im new in PHP please help me. Thanks <?php /* Created on: 10/12/2006 */ ?> <html> <body> <?php...
3
2167
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 that the user inputs through a drop-down menu in the databases GUI front-end (bugzilla actually). For example, a product is selected in a drop-down menu, and, depending on the product chosen, a second drop-down menu becomes active, containing a list...
4
6499
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 retrieved using list/menu box. When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category. However when i check multiple checkboxes and...
0
3128
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 Cardinality Field
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.