Jump to content

Question about Array()?


3D-kreativ

Recommended Posts

Hi,

 

I create a menu like this:

 

$wsMenu = Array (

'start' => '.',

'karta' => '?p=karta',

'bilder' => '?p=bilder',

);

 

foreach($this->iMenu as $key => $value) {

$menu .= "<a href='{$value}' class='menu'>{$key}</a> | ";

}

 

But I would like to add some 'id' + names into the link and my question is how to put this extra information for each link in the Array() and how would it be in the foreach loop ?

 

Preciate som help! Thanks!

Link to comment
https://forums.phpfreaks.com/topic/207239-question-about-array/
Share on other sites

The code is from a front- and page control system with classes, so iMenu comes from $wsMenu.

 

I'll take your word for it.

 

Try this:

 

$wsMenu = Array (
    'start' => array(
        'id=start_link',
        'foo=bar',
    ),
    'karta' => '?p=karta',
    'bilder' => '?p=bilder',
);

foreach($this->iMenu as $text => $href)
{
    if (is_array($href))
    {
        $href = '?' . implode('&', $href);
    }

    $menu .= "<a href='{$href}' class='menu'>{$text}</a> | ";
}

 

The only problem is it's limited to pages with multiple params starting with "?". You may want to extend it further to allow a prefix link for the params.

Thanks!

 

But I get error from this code?

 

$wsMenu = Array (

'start' => array('id=start','.'),

'karta' => array('id=map', 'p=karta')

'bilder' => array('id=buildings', 'p=bilder')

);

 

Could you explain what you mean with "The only problem is it's limited to pages with multiple params starting with "?". You may want to extend it further to allow a prefix link for the params."

.. What's the error?

 

Wait a minute. Thinking about it, and maybe I'm missing something, but can't you just store the URL with the parameters..?

 

$wsMenu = Array (
   'start' => '?id=start&foo=bar',
   'karta' => '?p=karta',
   'bilder' => '?p=bilder',
);

 

I'm also a little unsure about what "." is meant to mean?

Ahhh, sorry you didn't explain it very clearly. I thought you were referring to an ID parameter. That's easy then:

 

$wsMenu = Array (
    'start' => array(
        'href' => '?p=start',
        'id' => 'id1'
    ),
    'karta' => array(
        'href' => '?p=karta',
        'id' => 'id2'
    ),
    'bilder' => array(
        'href' => '?p=bilder',
        'id' => 'id3'
    ),
);

foreach($this->iMenu as $text => $link)
{
    $menu .= '<a href="'.$link['href'].'" class="menu"';
    
    if (!empty($link['id']))
    {
        $menu .= ' id="'.$link['id'].'"';
    }
    
    $menu .= '>'.$text.'</a> | ';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.