Children of children?

Feed 25 posts, 3 voices

Mar 25, 2008 13:59
Avatar
963 posts

I have been trying to find a solution to the question about a vertical menu, so that when you click on a level, the menu on the next level below is revealed. (Asked as post #29 in this thread.)

I think I almost have it. Well, I have it working for Home > Level1 > Level2, but not for Level3.

I get this error message for "Level3": Fatal error: Call to a member function children() on a non-object in [path]\frog\app\frontend\classes\Page.php(170)...

Here is my code:

<?php $subPageId = explode('/', $_SERVER['REQUEST_URI']); $level2=$subPageId[2]; $level3=$subPageId[3]; ?>

<h3>NAVIGATION</h3>
<ul>
    <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">home</a></li>
    <?php foreach($this->find('/')->children() as $menu): ?>
    <li><?php echo $menu->link($menu->title, (in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null)); ?>

    <?php if ($level2 != '') : ?>
        <?php $page2 = $this->find($level2); ?>
        <?php if (strpos($_SERVER['REQUEST_URI'],$menu->slug) == true) : ?>
        <ul>
        <?php foreach ($page2->children(array()) as $menu2): ?>
        <li><?php echo $menu2->link(); ?>

            <?php if ($level3 != '') : ?>
            <?php if (strpos($_SERVER['REQUEST_URI'],$menu2->slug) == true) : ?>
            <?php $page3 = $this->find('$level3'); ?>

            <ul>
                <?php foreach ($level3->children(array()) as $menu3): ?>
                <li><?php echo $menu3->link(); ?></li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <?php endif; ?>

        </li>
        <?php endforeach; ?>
        </ul>
        <?php endif; ?>
        <?php endif; ?>

    </li>
    <?php endforeach; ?> 
    </ul>

A couple notes: (1) the first "explode" line gets me the "slug", and the numerical index would need to be adjusted depending on where Frog lives on your server; (2) the if ($level2 != '') bit tests to see if there is a slug, and if so, continues to process that level.

The line that produces the error is this one: <?php foreach ($level3->children(array()) as $menu3): ?>. There must be a problem calling children-of-chilren this way, but I can not work out how to fix this. You can see it is just a variation for what is working well at "Level2".

Can anyone give some help on this? Many thanks!

David.

 
Mar 25, 2008 16:34
Avatar
390 posts

maybe if you use $page3 instead of $level3 ;) in : <?php foreach ($level3->children(array()) as $menu3): ?>

 
Mar 25, 2008 16:51
Avatar
963 posts

Ahh, now I see what you pointed out ... but sadly, I am getting the very same error message. :( Thanks anyway!

Any other ideas?

 
Mar 25, 2008 16:59
Avatar
963 posts

FWIW, this is line 170 of \frog\app\frontend\classes\Page.php:

eval('?>'.$this->part->$part->content_html);

Does that suggest anything??

 
Mar 25, 2008 18:19
Avatar
963 posts

P.s. You can see this code trying to work in the "GB Pages" and "Poet" sections of the this Frog "test site".

 
Mar 26, 2008 02:33
Avatar
83 posts

nice david, very nice!

 
Mar 26, 2008 09:37
Avatar
390 posts

ho ok I see you have to search for $level2.'/'.$level3 and your level 3 can't be found because you add single quote , so php will always search for "$level3" as the content.

maybe trying this plus what I mention before: <?php $page3 = $this->find($level2.'/'.$level3); ?>+<?php foreach ($level3->children(array()) as $menu3): ?>`

 
Mar 26, 2008 09:38
Avatar
390 posts

oups forgot one ` ;) sorry ... ok ok I will finish the forum soon for sure ;) like that we will be able to edit our post

 
Mar 26, 2008 10:01
Avatar
963 posts

Genius! You sorted it, Philippe! I should have caught the ('$level3') problem myself! But I would not have known to do $this->find($level2.'/'.$level3);. So that is great! This is the menu system that aroundthefur wanted, I think, but down to three levels, not just two.

If anyone wants to try it, here is the exact working code that I have in the sidebar of the Home Page:

<?php $subPageId = explode('/', $_SERVER['REQUEST_URI']); $level2=$subPageId[2]; $level3=$subPageId[3]; ?>

<h3>NAVIGATION</h3>
<ul>
    <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">home</a></li>
    <?php foreach($this->find('/')->children() as $menu): ?>
    <li><?php echo $menu->link($menu->title, (in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null)); ?>

    <?php if ($level2 != '') : ?>
        <?php $page2 = $this->find($level2); ?>
        <?php if (strpos($_SERVER['REQUEST_URI'],$menu->slug) == true) : ?>
        <ul>
        <?php foreach ($page2->children(array()) as $menu2): ?>
        <li><?php echo $menu2->link(); ?>

            <?php if ($level3 != '') : ?>
            <?php if (strpos($_SERVER['REQUEST_URI'],$menu2->slug) == true) : ?>
            <?php $page3 = $this->find($level2.'/'.$level3); ?>

            <ul>
                <?php foreach ($page3->children(array()) as $menu3): ?>
                <li><?php echo $menu3->link(); ?></li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <?php endif; ?>

        </li>
        <?php endforeach; ?>
        </ul>
        <?php endif; ?>
        <?php endif; ?>

    </li>
    <?php endforeach; ?> 
</ul>

Again, you may need to adjust the $subPageId[n] numbers in the first line to match the structure of the URL on your own site. This is presently in action on the "GB Pages" and "Poets" tabs here. (That host is doing file-storage maintenance today, so the site may be inaccessible from time to time.)

Thanks, Philippe! And I hope that helps...

David.

 
Mar 26, 2008 10:03
Avatar
963 posts

@Philippe - yes, "Edit Post" would be nice! :)

On the live site, my explode line has $level2=$subPageId[1]; $level3=$subPageId[2]; not [2] and [3] as above. Sorry about that!

 
Mar 26, 2008 11:34
Avatar
390 posts

remember the forum at philworks.com/forum !!! I haven all debug it yet and I have to add the "notification system" working

hope to have time soon. I'm currently really busy.

and thank you for your help in the forum david

 
Mar 26, 2008 15:36
Avatar
83 posts

waaaaaah, awesome, just awesome! that is really what i needed! nice little community! thanks david and phillippe

 
Mar 26, 2008 16:06
Avatar
83 posts

i just copied and paste it but it wont work, i also turned 2,3 into 1,2 on the first line! :>

 
Mar 26, 2008 16:25
Avatar
963 posts

What is the URL of your site? It may be you need different values for those variables.

Also, it won't work for "Archive" type pages, because they have a different URL structure -- only for "static" pages.

 
Mar 26, 2008 16:31
Avatar
83 posts

frog.bugeyes.de is my url and i have only static pages ;>

 
Mar 26, 2008 16:39
Avatar
963 posts

Do you have IM of some kind? Gchat maybe?

I see the error is in line 2! Seltsam! What is the URL for one of your subpages?

Did you put that code in the Home Page sidebar? or somewhere else?

 
Mar 26, 2008 16:41
Avatar
83 posts

i have icq: 195073908

url of subpage is about

and yes i put it in the homepage sidebar!

 
Mar 26, 2008 17:05
Avatar
963 posts

OK. I see the problem. You aren't using .htaccess, are you? The "?" in the URL is causing problems.

Can you enable .htaccess? Here are the instructions from the readme.txt file Frog's root folder:

optional (to remove the ? in the url)

  • edit file _.htaccess and set your base dir
  • rename _.htaccess to .htaccess
  • open config.php and define USE_MOD_REWRITE to true

If you enable that, you should be fine! Hope that works!

(I couldn't get online ICQ to work! You might find it useful to have a gmail account! :) )

David.

 
Mar 26, 2008 17:08
Avatar
963 posts

You also have the option to use ".html" as the URL suffix. You should probably change this in config.php, too, so it reads like this:

// add a suffix to pages (simluating static pages '.html')
define('URL_SUFFIX', '');

That ''); is two single ' marks with nothing between them, by the way!

D.

 
Mar 26, 2008 17:16
Avatar
83 posts

i did it, nothing happens! :>

 
Mar 26, 2008 17:20
Avatar
83 posts

u may need the default installation password at my url to please check it!

 
Mar 26, 2008 17:27
Avatar
963 posts

Hmmm.... Can you send me an email at dajare {at] gmail [dot} com? I will be out for a while now, though.

 
Mar 27, 2008 05:59
Avatar
963 posts

Update: There were complications of two kinds:

  1. The "Articles" section had been removed, so the main Home Page code was throwing errors (not the menu code in the sidebar).
  2. The ".html" extension was also causing errors in the menu code. :|

There are two solutions, if this technique is to work:

  1. Remove the URL suffix, as in post #19 above, in this thread. Or,
  2. Try to deal with the .html suffix in the code.

I tried to solve #2 - and with this additional line (2nd line below), it works at "Level 2":

<?php if ($level2 != '') : ?>
<?php $item2 = substr($level2,0,-5); ?>

That strips the URL suffix, and then you use <?php $page2 = $this->find($item2); ?> to get the menu items. But, that does not work at "Level 3", and I cannot work out what to do there!

So -- to use this technique, either (a) get rid of URL suffixes!, or (b) get Philippe to work out a complete solution to #2. :)

David.

 
Mar 27, 2008 06:51
Avatar
83 posts

thanks so much, i ony need lvl 2, so its ok this way!

 
Mar 27, 2008 11:24
Avatar
963 posts

If anyone else cares :) there was a further problem with this technique, in that when you did get down to a "Level 3" page, it threw off the truncating technique described in post #23 above. I have managed to find a way around that! So, here (for the record!) is the complete working code on aroundthefur's site:

<?php $subPageId = explode('/', $_SERVER['REQUEST_URI']); $level2=$subPageId[1]; $level3=$subPageId[2]; ?>

<h3>NAVIGATION</h3>
<ul>
    <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">home</a></li>
    <?php foreach($this->find('/')->children() as $menu): ?>
    <li><?php echo $menu->link($menu->title, (in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null)); ?>

    <?php if ($level3 != '') {$item2 = $level2;} else {$item2 = substr($level2,0,-5);} ?>
        <?php if ($level2 != '') : ?>
        <?php $page2 = $this->find($item2); ?>
        <?php if (strpos($_SERVER['REQUEST_URI'],$menu->slug) == true) : ?>
            <ul>
            <?php foreach ($page2->children(array()) as $menu2): ?>
            <li><?php echo $menu2->link(); ?></li>
            <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    <?php endif; ?>

    </li>
    <?php endforeach; ?> 
</ul>

The if ... else line detects which kind of variable to use. FWIW! David.