2

I have data that contains links for a navigation bar. It should be structured exactly like an unordered list, with a header and then all of the corresponding links below that header. I cannot seem to build this correctly. This would be some sample data from the database.

HEADING     LIST         LINK
Favs        google          http://...
Favs        yahoo           http://...
Favs        stackoverflow   http://...
Site        first link      http://...
Site        second link     http://...

This data should then group all of the headings into one and then display the links associated with them. Is this even possible or maybe there is a better way?

I plan to use the "HEADING" and "LIST" to dynamically build a <UL> type of menu.


Well, this isn't working as I had hoped. Here is the array that is being built from the database. Notice how sidebar[0] and sidebar[1] rpeat the value "Favs". This will repeat the same value on my form which I don't want. All of the duplicate names should be grouped together. Is this possible?

Array
(
    [date] => Sun, 25 Oct 2009
    [sidebar] => Array
        (
            [0] => Array
                (
                    [Favs] => Array
                        (
                            [author_sidebar_link] => google.com
                        )

                )

            [1] => Array
                (
                    [Favs] => Array
                        (
                            [author_sidebar_link] => yahoo.com
                        )

                )

            [2] => Array
                (
                    [Offsite] => Array
                        (
                            [author_sidebar_link_title] => something
                        )

                )

            [3] => Array
                (
                    [Something] => Array
                        (
                            [author_sidebar_link] => something else
                        )
                )
        )
)

3 Answers 3

3

You could use a multi-dimensional array like this:

<?php
$menu = array(
     'Favs' => array(
         array('LIST' => 'google', 'LINK' => 'http://...'),
         array('LIST' => 'yahoo', 'LINK' => 'http://...'),
         array('LIST' => 'stackoverflow', 'LINK' => 'http://...')
     ),
     'Site' => array(
         array('LIST' => 'first link', 'LINK' => 'http://...'),
         array('LIST' => 'second link', 'LINK' => 'http://...')
     )
);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Asaph, thank you. What I was hoping to do was to make these dynamic in that I would not have to hardcode into an array, the LIST and LINKS. Is this possible?
1
$menu = array('Favs' => array(
                              'Google' => 'http://',
                              'Yahoo' => 'http://'
                        ),
              'Site' => array(
                              'First' => 'http://',
                              'Second' => 'http://'
                        )
             );
foreach($menu as $category => $items){
    echo '<h3>' . $category . '</h3>';
    echo '<ul>';
    foreach($items as $name => $url){
        echo '<li><a href="' . $url . '">' . $name . '</a></li>';
    }
    echo '</ul>';
}

2 Comments

Hi JoostK, are you stll here? This isn't working as expected.
Yes I'm still alive, don't worry :) Just tested in TextMate, works fine. In response of your edit, how do you build that structure? Could you provide that piece of code?
0

you can use this code

<?php
$_list = array();
foreach($_data as $k => $v){
 $_list[$v['HEADING']][$v['LIST']] = $v['LINK'];
}

foreach($_list as $k => $v){
 echo "<ul>".$k;
 foreach($v as $kk => $vv){
  echo "<li><a href='".$vv."'>".$kk."</a></li>";
 }
 echo "</ul>";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.