Re: DB_NestedSet with HTML Menu - Can I use URL filename without ID?
| From: | Mark Reynolds | Date: | Tue, 24 Jul 2007 10:35:02 +0000 |
| Subject: | Re: DB_NestedSet with HTML Menu - Can I use URL filename without ID? | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-27718@lists.php.net to get a copy of this message | ||
Hi Daniel thanks for getting back to me. I've had to work on some other
things over the last few weeks, but I'm now back concentrating on this
problem.
Just as I had hoped and as you had suggested, the things that I'm looking
for already seem to be implemented in the pear package, my problem now is
getting the correct coding to make use of them.
As I'm using DirectTreeRenderer I've displayed a sitemap using the data in
the database and it has displayed exactly as I would expect. I've also used
print_r($structure) to confirm that the array is displayed as it should, and
it is. However when I change the menu type from sitemap to tree I don't get
the desired results. All that happens is the 'tree' only displays level 1
depth. I was hoping to use it in my left navigation to display level 1&2 and
then 3 if the user clicks on 2 for instance (hope that's clear).
I tried to go back to basics to troubleshoot by just using HTML_Menu on its
own (and not directtreerenderer). Again the sitemap displays as expected
with the correct indentations, but 'urhere' and 'prevnext' only display an
empty table:
<table border="1"><tr></tr></table>Hopefully you may have come
across this
before and can suggest something. By the way once this has been sorted out
I'm intending to then get to your idea of including the nodeID and the name
in the url.
Here's the main code of the HTML_Menu page that I just mentioned (I hope you
can help!):
/**********************************************/
require_once('HTML/Menu.php');
require_once('DB/NestedSet.php');
require_once('DB/NestedSet/Output.php');
require_once('DB/NestedSet/Menu.php');
require_once 'HTML/Menu/DirectTreeRenderer.php';
$dsn = 'mysql://user:pw@localhost/site';
$table = array(
'id' => 'id',
'rootid' => 'rootid',
'l' => 'l',
'r' => 'r',
'norder' => 'norder',
'level' => 'level',
'name' => 'name',
'title' => 'title',
'maincontent' => 'maincontent',
'url' => 'url'
);
$nestedSet =& DB_NestedSet::factory('DB', $dsn, $table);
$nestedSet->setAttr(array(
'node_table' => 'tb_nodes',
'lock_table' => 'tb_locks',
));
?>
<html>
<head>
<?php
if(isset($_GET['nodeID'])){
$node_data = $nestedSet->pickNode($_GET['nodeID']);
echo "<title>$node_data->name : $node_data->title</title>";
}
?>
</head>
<body>
<?php
$data = $nestedSet->getAllNodes(true);
foreach ($data as $id => $node) {
$data[$id]['url'] = $_SERVER['PHP_SELF'].'?nodeID=' .
$node['id'] .
'&nodename=' . filename_from_db_to_url($node['name']);
}
$params = array(
'structure' => $data,
'titleField' => 'name',
'urlField' => 'url');
$output =& DB_NestedSet_Output::factory($params, 'Menu');
$structure = $output->returnStructure();
$menu = & new HTML_Menu($structure, 'tree');
$currentUrl = $_SERVER['PHP_SELF'].'?nodeID=' . $_GET['nodeID'];
$menu->forceCurrentUrl($currentUrl);
$menu->show();
?>
</body>
</html>
"Daniel Khan" <datenpunk@php.net> wrote in message
news:0D.40.23636.8B982964@pb1.pair.com...
> Hi Mark,
>
> I am not sure if I got your point but I am doing my best :)
>
> Mark Reynolds schrieb:
> [..]
>
>> So if the $node['level'] is equal to 1 it displays the $node['name'] in
>>
>> the
>> URL.
>>
>> eg. http://www.domain.com/root/
>>
>> or if the $node['level'] is greater than 1 it displays the
>> $node['name']
>> as
>> the filename plus it's root name as the 'directory'. I want to keep the
>> url
>> down to this depth and not include subnodes etc.
>>
>> eg. http://www.domain.com/root/leaf.htm
>>
>> However I've been struggling with the detail/coding for this. So advice
>> would be appreciated on this.
>
> Means
> if($level == 1) { this url }
> if($level == 1) { that url }
>
> So that shouldn't be a problem. You have to code this this way.
>
>> Once the links are displayed correctly there's the important issue of
>> returning the $node[id'] based on the filename from the url, so that the
>> ID
>> can be given to the PickNode function.
>>
>> How can I carry the above out? Is it possible? Due to the lack of
>> documentation I've looked through the relevent pear files but [..]
>
> I think what you are looking for is pickNode():
>
>
> http://pear.php.net/package/DB_NestedSet/docs/latest/DB_NestedSet/DB_NestedSet.html#methodpickNode
> (there is no enduser docu - but the api doc is complete and verbose)
>
> <?php
> $searchNode = 'foo';
> $mynode = $NeSe->pickNode($searchNode, true, true, 'name');
> // if 'name' contains the nodename.
> ?>
>
> Another advice. You can create friendly urls using mod_rewrite and I
> really recommend to use the node id inside the url. As soon as you have 2
> nodes with the same name your code won't work anymore.
> Look at
> - http://www.shopping.at/Baby_kind,564/Baby_und_Kind.html
> -
> http://www.fisherprice.at/fisher-price-roll-a-rounds-Kat762.html
>
> I've used DB_Nested in both and both have the id inside the url.
> The are still search engine friendly..
>
> (http://www.google.at/search?q=site%3Awww.shopping.at)
>
> Hth - just ask if you need more help
>
> --
> Daniel Khan