Building TreeMenu from a database

Discuss everything related to ApPHP TreeMenu. Any questions related to the ApPHP TreeMenu developing or using should be posted to this forum.

Moderators: alexandrleonenko, Milton

Milton
Senior
Senior
Posts: 31
Joined: Jan 14th, '10, 14:17

Building TreeMenu from a database

Postby Milton » Oct 22nd, '10, 22:53

Suppose you have two tables in your MySQL database: directors and movies.
Directors table:
int id,
varchar name
Movies table:
int id,
int directorId,
varchar name
There is a foreign key relationship between the fields Movies.directorId and Directors.id. An entry in the Movies table will be permitted only if the corresponding Movies.directorId field matches a value in the Directors.id field.

Code: Select all

<?php
function CreateDirectorNodes($treeMenu)
{
    $query = "SELECT * FROM Directors";
    $result = mysql_query ( $query );
    $n = mysql_num_rows ( $result );
    for ( $i=0; $i<$n; $i++ )
    {
       $row = mysql_fetch_array($result);
       $node=$treeMenu->AddNode($row["name"]);
       CreateMovieNodes($node,$row["id"]);
     }
}
function CreateMovieNodes($node,$directorId)
{
    $query = "SELECT * FROM Movies where directorId=".$directorId;
    $result = mysql_query ( $query );
    $n = mysql_num_rows ( $result );
    for ( $i=0; $i<$n; $i++ )
    {
       $row = mysql_fetch_array($result);
       $node->AddNode($row["name"]);
     }
}
define ("TREEMENU_DIR", "");
require_once(TREEMENU_DIR.'treemenu.class.php');
$treeMenu = new TreeMenu();
$treeMenu->SetStyle("paneled");
$mysql_host = <MYSQL_HOST>;
$mysql_database = <MYSQL_DATABASE>;
$mysql_user = <MYSQL_USER>;
$mysql_password =<MYSQL_PASSWORD>;

$link = mysql_pconnect ($mysql_host,$mysql_user,$mysql_password);
mysql_select_db ( $mysql_database ) or die ("No chance");
CreateDirectorNodes($treeMenu);
$treeMenu->ShowTree();
mysql_close ( $link );
?>

The result can be seen below.
Attachments
screenshot.JPG
screenshot.JPG (23.02 KiB) Viewed 5886 times

mgates
Newbie
Newbie
Posts: 5
Joined: Oct 21st, '10, 21:15

Re: Building TreeMenu from a database

Postby mgates » Oct 22nd, '10, 23:00

Could you also show us your mysql database? Maybe an example sql code that will create the necessary tables / db we can go off from?

Milton
Senior
Senior
Posts: 31
Joined: Jan 14th, '10, 14:17

Re: Building TreeMenu from a database

Postby Milton » Oct 23rd, '10, 09:22

mgates wrote:Could you also show us your mysql database? Maybe an example sql code that will create the necessary tables / db we can go off from?

CREATE TABLE `Directors` (
`id` INT NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )
)
CREATE TABLE `Movies` (
`id` INT NOT NULL ,
`directorId` INT NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` ),
FOREIGN KEY ( `directorId`) REFERENCES Directors(id)
)

mgates
Newbie
Newbie
Posts: 5
Joined: Oct 21st, '10, 21:15

Re: Building TreeMenu from a database

Postby mgates » Oct 23rd, '10, 15:04

What about for description and stuff that goes on the right side of the tree?

administrator
Site Admin
Site Admin
Posts: 5997
Joined: Jan 7th, '09, 23:18
Contact:

Re: Building TreeMenu from a database

Postby administrator » Oct 24th, '10, 07:41

Do you mean this?

Code: Select all

$treeMenu->AddNode("Title", "text.txt");

Milton
Senior
Senior
Posts: 31
Joined: Jan 14th, '10, 14:17

Re: Building TreeMenu from a database

Postby Milton » Oct 24th, '10, 16:21

mgates wrote:What about for description and stuff that goes on the right side of the tree?

For example, if page 'infofilm.php' shows information about a film, the code might be like this:

Code: Select all

function CreateMovieNodes($node,$directorId)
{
    $query = "SELECT * FROM Movies where directorId=".$directorId;
    $result = mysql_query ( $query );
    $n = mysql_num_rows ( $result );
    for ( $i=0; $i<$n; $i++ )
    {
       $row = mysql_fetch_array($result);
       $node->AddNode($row["name"],"infofilm.php?id=".$row["id"]);
     }
}

mgates
Newbie
Newbie
Posts: 5
Joined: Oct 21st, '10, 21:15

Re: Building TreeMenu from a database

Postby mgates » Oct 24th, '10, 17:28

Can we have the info in the mysql database itself, rather than the php file?

For example if it was simple text, or maybe images if that's possible?

Milton
Senior
Senior
Posts: 31
Joined: Jan 14th, '10, 14:17

Re: Building TreeMenu from a database

Postby Milton » Oct 24th, '10, 18:43

mgates wrote:Can we have the info in the mysql database itself, rather than the php file?

For example if it was simple text, or maybe images if that's possible?

For example, you want the contents of 'Movies.description' column to be displayed on the right side. Just do the following:

Code: Select all

function CreateMovieNodes($node,$directorId)
{
    $query = "SELECT * FROM Movies where directorId=".$directorId;
    $result = mysql_query ( $query );
    $n = mysql_num_rows ( $result );
    for ( $i=0; $i<$n; $i++ )
    {
       $row = mysql_fetch_array($result);
       $newNode=$node->AddNode($row["name"]);
       $newNode->SetInnerHTML($row["description"]);
     }
}

Hope that helps.

mgates
Newbie
Newbie
Posts: 5
Joined: Oct 21st, '10, 21:15

Re: Building TreeMenu from a database

Postby mgates » Oct 29th, '10, 00:20

It does help, thanks. :D

Few questions.

1) How do I change this

Code: Select all

index.php?nodeid1=1_1&node1_1=e&node1_1_1=e&node1_1_2=c&node1_2=e&node1_3=c&node1_4=c


Into this:

Code: Select all

my_page.php?act=34&id=56


I'm using this:

Code: Select all

$treeMenu->SetHttpVars(array("id"));


Clicking on some categories change the index.php?etc and some don't.

2) How could I implement a method to edit or add values to my table live and so it updates in the mysql db at the same time?

3) How can I have the tables update automatically, without having to write a new function for each sub category?

4) How would I use OpenNewWindow with this? I tried adding this but no window opens when clicking New Window node.

Code: Select all

CreateMainNodes($treeMenu);
$newWindow=$treeMenu->AddNode("New Window","http://www.google.com");
$newWindow->OpenNewWindow(true);
$treeMenu->ShowTree();
mysql_close ( $link );

administrator
Site Admin
Site Admin
Posts: 5997
Joined: Jan 7th, '09, 23:18
Contact:

Re: Building TreeMenu from a database

Postby administrator » Nov 21st, '10, 06:59

Clicking on some categories change the index.php?etc and some don't.


1. Which exactly? Can you post here a link, so we'll look on this?
2. To implement your requirements, you have to customize script and add your own function (if you are familiar with PHP)


Return to “ApPHP TreeMenu”