Forum

Thread tagged as: Question

Taking two navigations and mixing together to output in alphabetical order

Hi Guys,

Any pointers on this?
I have this code;

        $url = perch_page_url(array(
            'hide-extensions'       => false,
            'hide-default-doc'      => true,
            'add-trailing-slash'    => false,
            'include-domain'        => false,
        ), true);

        //This code splits the current page URL into strings after each '/'
        //We then use this to choose the right products for each page as each path/URL is different. So this is passed into the perch_pages_navigation function to get the products for that section .E.G /land-products/electrical-resistivity
        $url_paths = explode('/', substr($url, 1), 2);
        $url_nav = "/".$url_paths[1];

        echo '<ul class="related-products">';

        $products = perch_pages_navigation(array(
            'from-path'             => $url_nav,
            'flat'                  => false,
            'template'              => 'product_teaser.html',
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));

        $products_rentals_only = perch_pages_navigation(array(
            'from-path'             => '*',
            'flat'                  => false,
            'template'              => 'product_teaser.html',
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));

        echo '</ul>';

I have two lots of perch_pages_navigation - pulling from two different sections. What I would like to do is put them into one array and then sort them alphabetically and use that template (product_teaser.html) to output….

Any ideas or can you push me in the right direction?

Terry Upton

Terry Upton 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to use the skip-template option to return an array.

'skip-template' => true,

Whoops. Sorry Drew. I pasted in the wrong code. This is my code; How do I get the arrays into one and then reorder that and then output that new array into the product_teaser.html' template?

        echo '<ul class="related-products">';
        $pages = perch_pages_navigation(array(
            'from-path'             => $url_nav,
            'flat'                  => false,
            'skip-template'         => true,
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));
        $pages_2 = perch_pages_navigation(array(
            'from-path'             => $url,
            'flat'                  => false,
            'skip-template'         => true,
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));

        array_merge($pages, $pages2);

Drew McLellan

Drew McLellan 2638 points
Perch Support

This isn't really a Perch question, but you'll need to catch the output of array_merge() - at the moment you're merging and throwing the result away.

If anyone is interested then I have solved it like this;

    <?php

        $url = perch_page_url(array(
            'hide-extensions'       => false,
            'hide-default-doc'      => true,
            'add-trailing-slash'    => false,
            'include-domain'        => false,
        ), true);

        //This code splits the current page URL into strings after each '/'
        //We then use this to choose the right products for each page as each path/URL is different. So this is passed into the perch_pages_navigation function to get the products for that section .E.G /land-products/electrical-resistivity
        $url_paths = explode('/', substr($url, 1), 2);
        $url_nav = "/".$url_paths[1];


        //Get any pages from the products section
        $products = perch_pages_navigation(array(
            'from-path'             => $url_nav,
            'flat'                  => false,
            'skip-template'         => true,
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));

        //Get any pages from the individual rentals section
        $products_rentals = perch_pages_navigation(array(
            'from-path'             => '*',
            'flat'                  => false,
            'skip-template'         => true,
            'include-hidden'        => true,
            'siblings'              => false,
            'include-parent'        => false,
            'hide-extensions'       => true,
            'hide-default-doc'      => true,
        ));

        echo '<ul class="related-products">';
            if(!empty($products_rentals)) {
            //Merge this data from these two arrays into a single array and then sort it by the pageNavText.
            $final_array = array_merge($products, $products_rentals);

            foreach($final_array as $page) {
                $sort_array[$page['pageNavText']] = $page;
            }
            ksort($sort_array);

            //Now go through each product and present it out in the template
            foreach($sort_array as $page) {

                perch_pages_navigation(array(
                    'from-path'             => $page['pagePath'],
                    'template'              => 'product_teaser.html',
                    'flat'                  => false,
                    'include-parent'        => true,
                    'add-trailing-slash'    => true,
                ));
            }
            } else {
                perch_pages_navigation(array(
                    'from-path'             => $url_nav,
                    'flat'                  => false,
                    'template'              => 'product_teaser.html',
                    'include-hidden'        => true,
                    'siblings'              => false,
                    'include-parent'        => false,
                    'hide-extensions'       => true,
                    'hide-default-doc'      => true,
                ));
            }
        echo '</ul>';


        perch_content_create('Product Accessories', array(
            'template'      => 'products/related_accessories.html',
            'multiple'      => false,
            'edit-mode'     => 'singlepage',
        ));
    ?>

Drew, thanks for input. If you have any recommendations to improve on the above code then feel free :-)

Thanks, Terry