Page 1 of 1

Problem with SQL query (get parent title)

Posted: Sun Dec 10, 2006 11:18 pm
by anandg
I have a horizontal menu. When you click on a horizontal menu option, then a left subnavigation will show up.
I have created a query that will show the menu text of the parent from a left subnavigation option.

For example, the horizontal menu is:
1. Home
2. Collection
3. Contact

Subnavigation for Collection is
2.1 Collection 1
2.2 Collection 2
2.3 Collection 3

If I am at the page 2.2, then I want to show "Collection" as the parent on the page.
But the query below doesn't work.
Can anyone see what is wrong with this query?

Code: Select all

global $gCms;
$db =& $gCms->GetDb();
$vars =& $gCms->variables;

$query = "
SELECT menu_text 
FROM ".cms_db_prefix()."content 
WHERE content_id = 
(
   SELECT parent_id 
   FROM ".cms_db_prefix()."content 
   WHERE content_id = '".$vars['content_id']."'
);";

$row = $db->GetRow($query);
print_r($row);

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 9:11 am
by anandg
I have already tested parts of the query.
The query between the parenthises works:

Code: Select all

SELECT parent_id 
FROM ".cms_db_prefix()."content 
WHERE content_id = '".$vars['content_id']."'
The query outside the parenthises also works. I used this for example:

Code: Select all

SELECT menu_text 
FROM ".cms_db_prefix()."content 
WHERE content_id = 16";
It should work but it doesn't. Can anyone help me?

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 4:57 pm
by rtkd
hmm..., i'm not sure, but i dont think u can have nested select statements (as far as i know that is!), tho that code would make scence.

/*
@ EDIT
*/

i looked it up. u CAN have nested select

try taking out the ; in the nested select statement. WHERE content_id = '".$vars['content_id']."');";
the examples ive seen go without.

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 5:06 pm
by cyberman
anandg wrote: I have created a query that will show the menu text of the parent from a left subnavigation option.
Don't know if I've understand right but I think you can do that with MenuManagers options and without special queries.

Can you post your menu manager command for left submenu?

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 7:07 pm
by anandg
My menu manager command is:

Code: Select all

{* .currentpage - The active/current page  *} 

{if $count > 0}

{* 
show the parent (title) from the current node (page) 
also show the parent (title) if its the parent itself
*}
{foreach from=$nodelist item=node}
    {if $node->current == true and $node->haschildren == true}    
        <h1>{$node->alias}</h1>
    {/if}
{/foreach}

<ul class="clearfix">

{foreach from=$nodelist item=node}

{if $node->current == true and $node->haschildren == false and $node->alias != 'home'}
    <li>{$node->menutext} 
{elseif $node->haschildren == false and $node->alias != 'home'}
    <li><a href="{$node->url}"> {$node->menutext} </a>
{/if}
{/foreach}

</ul>
{/if}

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 7:13 pm
by cyberman
No, I've meaned menu manager call, not the template :) ...

There's a parameter for menu manager available, which can do, that element and only it's children are shown.

Re: Problem with SQL query

Posted: Tue Dec 12, 2006 8:39 pm
by anandg
Do you mean parameters like start_element, start_level, start_element?
Because i've tried some parameters, but that didn't work. Perhaps I tried it the incorrect way.

Could you give me an example?

Re: Problem with SQL query

Posted: Tue Dec 19, 2006 10:49 pm
by anandg
I have tried to find a solution for a couple of days and hours, but still no succes.
Can somebody help me?

The last I tried is this:
{cms_module module='menumanager' collapse='1' template='zenner_minimal_menu' start_level='2'  show_root_siblings='1'}

Which also doesn't work. It doesn't show the parent title. It only shows the title of the current page.

Re: Problem with SQL query

Posted: Wed Dec 20, 2006 8:56 am
by anandg
Finally, I found the solution in the forum!
http://forum.cmsmadesimple.org/index.ph ... 778.0.html

This is the code from the topic that worked:

Code: Select all

// This is only going to step up the tree one level

global $gCms;
$content_id = $gCms->variables['content_id'];
$hm =& $gCms->GetHierarchyManager();

$node =& $hm->getNodeById($content_id);

if ($node != null)
{

    //Grab the parent node
    $parentnode =& $node->getParentNode();
    $ncontent =& $node->getContent();
    $pnode = $ncontent->ParentID();

    //Make sure something came back
    if ($pnode != -1)
    {
        //Get the actual content object
        $content =& $parentnode->getContent();
    } 
    else {
        //Ok no parent lets get the title of this node
        $content = $ncontent;
     }

     if ($content != null) //Just in case
        {
            //Display it's title
            echo $content->Name();
        }   
}

Re: Problem with SQL query (get parent title)

Posted: Wed Dec 20, 2006 6:05 pm
by cyberman
Thanx for your solution :) ...