Hi all,

I am developing a simple PHP menu class - the goal is to use PHP and a
mySQL database to quickly generate a menu tree. I require some assistance
with the database abstraction I am trying to use:

My entire menu is stored in a "menu" table containing these fields:

id - id of record (AUTOINC)
label - name of menu item
link - if leaf, where does it link?
parent - parent of item in menu tree (0 = root level)

Now I have written a PHP class with some functions to help me manipulate
this table. Here is the code:


class Menu {

var $hostname = "localhost";
var $user = "me";
var $pass = "dfsdjfj";
var $db = "menu";

// contructor
function Menu()


function query($query) {

$connection = mysql_connect($this->hostname, $this->user, $this->pass) or
die ("Cannot connect to database");

$ret = mysql_db_query($this->db, $query, $connection) or die ("Error in
query: $query");


return $ret;


function create_leaf($link, $label, $parent = 0) {

        $this->query("INSERT INTO menu(link, label, parent) VALUES ('$link',
'$label', '$parent')"); 


function create_branch($label, $parent = 0)

        $result = $this->query("INSERT INTO menu(label, parent) VALUES ('$label',

function get_children($id = 0)

        $result = $this->query("SELECT id, label, link FROM menu WHERE parent =
        $children = array();
        $count = 0;
        while (list($id, $label, $link) = mysql_fetch_row($result))
                $children[$count][0] = $id;     
                $children[$count][1] = $label;  
                $children[$count][2] = $link;   
        return $children;


function get_child_leaves($id = 0)


function get_child_branches($id = 0)




Here are my problems:

1. As you can see, I am using a function called query() to hold all the
database-specific functions. If I change to PG, I need only change the code
here and the rest of the class will work as normal. 

When I use create_leaf() or create_branch(), I would like the query()
function to return to me the last INSERT id via mysql_insert_id() function
(not happening currently). And when I use the get_children() and other
get_* functions, I would like to have the resultset returned as an array
(happening fine). 

How can I tell query() to return different data depending on the type of
query being executed ie. for INSERT return id, for SELECT return result

2. Is it necessary to close() the SQL connection at the end of query()? I
know this is preferred - however, I was thinking - if I didn't close it, I
could write another function get_id() which could use the result identifier
$ret and get the INSERT id from it. It is because I am closing the DB
connection at the end of query() that other mysql_ functions cannot be run
from outside it.

3. What should be put in the constructor here?

4. I would like to release this class to SourceForge as an open-source
project. Even if you cannot help me with the questions above - are there
any ideas/features/improvements you can suggest that would make it more
useful and generic? I am kinda new to PHP :) and your ideas would be very


I took an IQ test, and the results were negative.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to