10 Replies - 622 Views - Last Post: 27 December 2012 - 04:35 PM Rate Topic: -----

#1 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

display navigation menu

Posted 27 December 2012 - 09:24 AM

Hi. I want to display menu on my navigation bar using arrays (i know it's probbably lame but who care). I tried this, but it tells me that there are invalid arguments.
public function menu($type){
 foreach($type as $key => $value){
  echo"<li><a href=".$type[$value].">".$type[$key]."</a></li>";
 }
}



i have two arrays $array1 and $array2 which contains few static names like 'link1' 'link2' and their data '?page=1' '?page=2' etc. I want to use function function menu(type) which will display menu based on array of type, for example: menu(array1) will display menu based on array1 which is:
<li><a href='?page=1'>link1</a></li>
<li><a href='?page=2'>link2</a></li>



Am I doing it right (actually there is an error with expression or something) and in good way? or it's better to show menu from database instead array?

Is This A Good Question/Topic? 0
  • +

Replies To: display navigation menu

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: display navigation menu

Posted 27 December 2012 - 09:31 AM

View PostMuphet, on 27 December 2012 - 11:24 AM, said:

I tried this, but it tells me that there are invalid arguments.

Can you provide the exact error message?

What happens if you use the following ?

public function menu($type){
  if(!empty($type)) {
    foreach($type as $key => $value){
      echo"<li><a href=".$type[$value].">".$type[$key]."</a></li>";
    }
  } else {
    echo "<pre>";
    print_r($type);
    echo "</pre>';
  } 
}


Was This Post Helpful? 0
  • +
  • -

#3 trichardson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 9
  • Joined: 02-December 12

Re: display navigation menu

Posted 27 December 2012 - 09:34 AM

The code for your function looks good.

Barring an error message provided I would venture to say that the array you are providing is invalid. Check to make sure your menu array was constructed properly before passing it to your function.

Cheers.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,445
  • Joined: 12-December 12

Re: display navigation menu

Posted 27 December 2012 - 09:37 AM

<li><a href='needsomepagehere.html?page=1'>link1</a></li>
<li><a href='ormaybe.php?page=2'>link2</a></li>

You should enclose quotes or apostrophes around these hrefs as well:

echo "<li><a href='somepage.php".$type[$value]."'>".$type[$key]."</a></li>";

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: display navigation menu

Posted 27 December 2012 - 10:00 AM

one more thing, what is $type[$value] supposed to be? $type[$key] at least ainít invalid, but I doubt it is intended.
Was This Post Helpful? 0
  • +
  • -

#6 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: display navigation menu

Posted 27 December 2012 - 12:31 PM

@trichardson
i posted exact error genius.
"Warning: Invalid argument supplied for foreach() in E:\tools\WebServ\httpd\core.php on line 33"
which is "foreach($type as $key => $value)"

@dormilich
I wanted to display key within value of different array, thats why I used $type[$key].

@no2pencil
error in this line: foreach($type as $key => $value){ //Invalid argument supplied for foreach()
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: display navigation menu

Posted 27 December 2012 - 12:35 PM

Let see what $type contains :

public function menu($type){
  if(!empty($type)) {
    echo "<pre>";
    print_r($type);
    echo "</pre>";
  } 
}


This post has been edited by Dormilich: 27 December 2012 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: display navigation menu

Posted 27 December 2012 - 12:37 PM

View PostMuphet, on 27 December 2012 - 08:31 PM, said:

Invalid argument supplied for foreach()

I would deem this message obvious. to me it seems as $type is not an array*.


View PostMuphet, on 27 December 2012 - 08:31 PM, said:

@dormilich
I wanted to display key within value of different array, thats why I used $type[$key].

$type[$key] is not a problem, though $value would be easier.




* - or a Traversable object, but that is hardly used in procedural style
Was This Post Helpful? 1
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3617
  • View blog
  • Posts: 12,445
  • Joined: 12-December 12

Re: display navigation menu

Posted 27 December 2012 - 12:51 PM

Yes, $value is already the value of the element, and presumably you want the key to be displayed:

echo "<li><a href='somepage.php" . $value . "'>" . $key . "</a></li>";


$type[$key] <==> $value // .. are the same.
Was This Post Helpful? 0
  • +
  • -

#10 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 220
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: display navigation menu

Posted 27 December 2012 - 04:27 PM

I know this isn't what the OP is necessarily asking, but take a look at printf as it allows you to seperate your variables and your markup. For example your list items would be like so:

    printf('<li><a href="%s">%s</a></li>', $value, $key);



This removes the concatenation and echo command, also down the road allows you to call functions from within the command itself.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: display navigation menu

Posted 27 December 2012 - 04:35 PM

regarding printf(), esp. if there is a larger amount of text, HEREDOC type strings come in very handy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1