PHP - create multilevel HTML menu from php array concatenating same values ​​based on value name

I've searched SO a bit and couldn't find this unique use case, but maybe I don't know how to express it. I want to use a PHP array (see format below) to create a dynamic HTML menu that duplicates the same string values ​​in a submenu when a match exists.

It might be too complicated, but I would like to know if this is possible and if anyone has a sample code. I don't mind changing the structure of the values ​​of the 'verse' array to make it more mutable and make it work (perhaps turning it into an array, since it has 1: many potential values).

I couldn't get the HTML below to format SO correctly, but the gist of the request is that all articles with the corresponding book / section line will be put together and nested. If there are 2 entries for Psalms 121 (which are in the following array), then they must be nested together in the same submenu.

Required end structure (note that since 2 lines corresponded to chapter 121 of Psalms, they were merged together:

Hebrews
    Chapter 12
        Verse 26-28
    Chapter 9
        Verse 23-24

Psalms
    Chapter 121
         Verse 3-4
         Verse 7-10
    Chapter 16
         Verse 11

Exodus
    Chapter 33
         Verse 14

      

Thank you in advance for your help!

PHP array:

<?php

$articles = array(

array(
    'contentid' => '109',
    'full' => 'Song by Artist (Ref 109)',
    'verse' => 'Hebrews 12:26-28'),

array(
    'contentid' => '110',
    'full' => 'Song by Artist (Ref 110)',
    'verse' => 'Psalms 121:3-4'),

array(
    'contentid' => '111',
    'full' => 'Song by Artist (Ref 111)',
    'verse' => 'Hebrews 9:23-24'),

array(
    'contentid' => '112',
    'full' => 'Song by Artist (Ref 112)',
    'verse' => 'Psalms 16:11; Exodus 33:14; Psalms 121:7-10'));

?>

      

I would like this to draw an HTML menu that looks like this:

<!-- Hebrews -->
                    <div class="submenu">
                        <a href="#">**Hebrews**</a>
                        <!-- Level 2 menu -->
                        <div>
                            <div>


                                <div class="submenu">
                                    <a href="#">Chapter *12*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 109)</span>Verse *26-28*</a>
                                    </div>
                                    </div>
                                </div>

                                <div class="submenu">
                                    <a href="#">Chapter *9*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 111)</span>Verse *23-24*</a>
                                        </div>
                                    </div>
                                </div>

<!-- Psalms -->

                    <div class="submenu">
                        <a href="#">**Psalms**</a>
                        <!-- Level 2 menu -->
                        <div>
                            <div>

                                <div class="submenu">
                                    <a href="#">Chapter *121*</a>
                                    <!-- Level 3 menu -->
                                    <div>
                                        <div>
                                            <a href="#"><span>Song by Artist (Ref 110)</span>Verse *3-4*</a>
                                            <a href="#"><span>Song by Artist (Ref 112)</span>Verse *7-10*</a>                                       </div>
                                    </div>
                                </div>

      

      Chapter * 9 * Artist Song (Ref. 111) Verse * 23-24 *
+3


source to share


1 answer


$articles = array
(
    array
    (
        'contentid' => '109',
        'full' => 'Song by Artist (Ref 109)',
        'verse' => 'Hebrews 12:26-28'
    ),

    array
    (
        'contentid' => '110',
        'full' => 'Song by Artist (Ref 110)',
        'verse' => 'Psalms 121:3-4'
    ),

    array
    (
        'contentid' => '111',
        'full' => 'Song by Artist (Ref 111)',
        'verse' => 'Hebrews 9:23-24; Psalms 121:3-4'
    ),

    array
    (
        'contentid' => '112',
        'full' => 'Song by Artist (Ref 112)',
        'verse' => 'Psalms 16:11; Exodus 33:14; Psalms 121:7-10; Psalms 121:3-4'
    )
);



$langs = array();

foreach($articles as $article)
{
    foreach(explode("; ", $article["verse"]) as $verseData)
    {
        $verse = explode(" ", $verseData);
        $lang = $verse[0];
        $fullVerse = $verse[1];
        $verse = explode(":", $verse[1]);
        $chapter = $verse[0];

        if(empty($langs[$lang]))
            $langs[$lang] = array();

        if(empty($langs[$lang][$fullVerse]))
            $langs[$lang][$fullVerse] = array();

        $langs[$lang][$fullVerse][] = array
        (
            "full" => $article["full"]
        );
    }
}

foreach($langs as $lang => $data)
{
    generateHtml($lang, $data);
}

function generateHtml($lang, $data)
{
    echo "<!-- ". $lang ." -->\n";
    echo "<div class=\"submenu\">\n";
    echo "<a href=\"#\">**". $lang ."**</a>\n";
    echo "<!-- Level 2 menu -->\n";
    echo "<div>\n";
    echo "<div>\n";

    foreach($data as $fullVerse => $verses)
    {
        $verse = explode(":", $fullVerse);
        $chapter = $verse[0];
        $verse = $verse[1];

        echo "<div class=\"submenu\">\n";
        echo "<a href=\"#\">Chapter *". $chapter ."*</a>\n";
        echo "<!-- Level 3 menu -->\n";
        echo "<div>\n";
        echo "<div>\n";

        foreach($verses as $verseIndex => $value)
        {
            if(count($verses) > 1)
                $value["full"] .= " (". ($verseIndex + 1) .")";

            echo "<a href=\"#\"><span>". $value["full"] ."</span>Verse *". $verse ."*</a>\n";
        }

        echo "</div>\n";
        echo "</div>\n";
    }

    echo "</div>\n";
    echo "</div>\n";
    echo "</div>\n";
}

      



You can use \t

to add tab space. My suggestion is that you already generate it like this. make it one line by deleting everything \n

.

0


source







All Articles