2

I am exercising some PHP OOP and therefore I am creating a class to create a simple navigation menu ( with extensions in the future ) now I have build this class that works kinda.. with 1 menu item tough.. I don;t know how to use arrays in my class to use the class like

<?php
$menu = new Navigation("Test", "mainmenu");

$menu->setMenuItem("home", "test");
echo $menu->display();

?>

as you see I should be able to give each menu item with the setMenuItem(); method. but since it does not use Arrays at the moment I only get the first value

The class itself is as follows:

<?php
class Navigation implements navigationInterface {

    public $menu = null;
    public $name = null;
    public $klasse = null;

    public function __construct($name, $klasse) {

        $this->name = $name;
        $this->klasse = $klasse;

    }

    public function getName() {
        return $this->name;
    }

    public function getClass() {
        return $this->klasse;
    }

    public function setMenuItem($items) {
        $this->menuItem = $items;
    }

    public function getMenuItem() {
        return $this->menuItem;
    }

    public function display() {

        $menu = '<nav class="' . $this->getName() . '">';
        $menu .= '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem() . '.php">' . $this->getMenuItem() . '</a></li>';
        $menu .= '</nav>';

        return $menu;

    }
}
?>

who can show me how to use arrays within the class in combination with a loop to create a menu with all given values?

Thanks in advance.

4 Answers 4

6
<?php
class Navigation{

    public $menu = null;
    public $name = null;
    public $klasse = null;

    public function __construct($name, $klasse) {

        $this->name = $name;
        $this->klasse = $klasse;

    }

    public function getName() {
        return $this->name;
    }

    public function getClass() {
        return $this->klasse;
    }

    public function setMenuItem($items) {

        $this->menuItem = $items;
    }

    public function getMenuItem($menu) {
        return $menu;
    }

    public function display() {

        $menu = '<nav class="' . $this->getName() . '">';
        if(is_array($this->menuItem))
        {
        foreach($this->menuItem as $val)
        {
        $menu .= '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem($val) . '.php">' . $this->getMenuItem($val) . '</a></li>';
        }
        }
        else{
            $menu .= '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem($this->menuItem) . '.php">' . $this->getMenuItem($this->menuItem) . '</a></li>';

        }


        $menu .= '</nav>';

        return $menu;

    }
}
?>

<?php
$menu = new Navigation("Test", "mainmenu");

$menu_items=array("home","test");
$menu->setMenuItem($menu_items);

echo $menu->display();

?>
Sign up to request clarification or add additional context in comments.

Comments

1

You actually have two types, not one:

  1. MenuItemList
  2. MenuItem

The MenuItemList would take care of managing the list. It could use an array internally. A code example for something very similar could be found in a previous answer: Array Object In Php.

Next to that the display() method does not belong into the two. Instead you should make your template that keen it knows how to output a menu list:

echo '<ul>';
foreach ($menu as $item) {
   echo '<li class="menutitem ', $item->getClass(), '"><a href="index.php?page=', ...;
}
echo '</ul>';

This would also allow you to keep some procedural knowledge which often is more straight forward with the templating while knowing that you menu model just works and is properly encapsulated.

2 Comments

What about MenuItemList::__toString()?
@MadaraUchiha: That could output a plain-text representation for debugging purposes. However you could create a HTML decorator for that menulist that would output a HTML list with it's __toString() method. But the menuitems should not care about how they are output in specific. That's the job of some other part of the code.
0

At top of your class

$menuItems = array();

then in setMenuItem()

function setMenuItem($name, $value)
{
  $this->menuItems[] = array($name, $value);
}

then

function display()
{
  // Your other code here
  foreach($this->menuItems as $item)
  {
    echo '<li><a href="'.$item[1].'">'.$item[0].'</a></li>';
  }
  // Your other code here
}

Hope that helps.

Edit: You could change the way they are stored, and prevent duplicates by using the nav "Name" as the key it is stored under, but I've kept it simple for ease of understanding.

Heres a more advanced sample

function setMenuItem($name, $value)
{
  if(!in_array(array_keys($this->menuItems)), $name) {
    $this->menuItems[$name] = $value;
  } else {
    echo "That's already been added!"; //Handle this properly though
  }
}

then

function display()
{
      // Your other code here
      foreach($this->menuItems as $name=>$value)
      {
        echo '<li><a href="'.$value.'">'.$name.'</a></li>';
      }
      // Your other code here
}

6 Comments

your way does not really work, also the getMenuItem is gone now.. is that correct?
getMenuItem will need to be changed to work with this method. What do you mean "it doesn't work"? An error? (Bear in mind, I'm typing with no error checking, I could well have typo's, I'll have a closer look)
I had only 'Array' as the only menu item.
I have 2 examples in my code, make sure you are only using the top one to begin with. If you mix them up you will see "array" printed.
i've used them separated but with no result
|
0

do like this :

<?php
class Navigation implements navigationInterface {

    public $menu = array();
    public $name = null;
    public $klasse = null;

    public function __construct($name, $klasse) {

        $this->name = $name;
        $this->klasse = $klasse;

    }

    public function getName() {
        return $this->name;
    }

    public function getClass() {
        return $this->klasse;
    }

    public function setMenuItem($name , $value) {
        $this->menu[$name] = $value;
    }

    public function getMenuItem($name) {
        return $this->menu[$name];
    }

    public function display() {

        if(!empty($this->menu)){

            $menu .= "<ul>";

            foreach($this->menu as $key => $value){
                $menu .= "<li><a href='".$value."'>$key</a></li>";
            }

            $menu .= "</ul>";
        }
        return $menu;
    }
}
?>

I think this is what you need

2 Comments

with your example I only get the last value as menu item
which of the functions do you refer to? the getmenuItem() function you can get each menuItem by its name, and the display() function, returns all the menuItems

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.