class help/ navigation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
killagoat
Forum Newbie
Posts: 7
Joined: Wed Dec 16, 2009 3:49 pm

class help/ navigation

Post by killagoat »

Hi,

I new here. I bought a php book called PHP and MySQL web development by Luck Welling, and I've been practicing with it for a couple weeks. I'm on the section with classes now to dynamically build webpages. I started getting off track and trying to set up a navigation menu that uses images... later when I learn how to build in javascript with php I want to add in rollovers.

I set it up to display 5 images through an array with a empty link. The problem I'm having is trying to figure out how to put in the url for the link dynamically through a array or something of that sort.

If you get a change check the code and let me know how I could put the url into the link which is already running through the while loop.

Later I would like to use the code mentioned in the book which checks to see if the url match and display a different selected image.

Let me know what you think:

Code: Select all

<?php
 
class buildpage {
 
// attributes
 
public $page_title = 'Some site';
 
public $content = '<p>Default Content</p>';
 
public $nav_images = array (
 
    'Home' => 'reg_home.png',
    
    'About' => 'reg_about.png',
    
    'Calculator' => 'reg_calculator.png',
    
    'Feedback' => 'reg_feedback.png',
    
    'Contact' => 'reg_contact.png'
 
);
 
// operations
 
public function display() {
 
    echo "<html>\n\n<head>\n\n";
    
    $this -> display_title();
    
    echo "\n\n</head>\n\n<body>\n\n";
    
    $this -> display_header();
    
    $this -> display_nav($this -> nav_images);
    
    echo $this -> content;
    
    $this -> display_footer();
    
    echo "\n\n</body>\n\n</html>";
 
}
 
public function display_title() {
 
    echo '<title>' .$this -> page_title. '</title>';
 
}
 
public function display_header() {
 
    echo '<h2>Page Header</h2>';
 
}
 
public function display_nav($nav_images) {
 
    while(list($img, $src) = each($nav_images)) {
    
    echo '<a href="#"><img src="'.$src.'" alt="'.$img.'" border="0" /></a>';
 
    }
 
}
 
public function display_footer() {
 
    echo '<h2>Page Footer</h2>';
 
}
 
} # end class
 
?>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: class help/ navigation

Post by flying_circus »

You can use a multi-dimensional array. Here is the basic idea:

Code: Select all

<?php
  class Test {
  # Vars
    public $nav_images = array();
    
  # Methods
    public function init() {
      $this->nav_images[] = array("name" => "Home",
                                  "src" => "reg_home.png",
                                  "href" => "home.php");
      $this->nav_images[] = array("name" => "About",
                                  "src" => "reg_about.png",
                                  "href" => "about.php");
      $this->nav_images[] = array("name" => "Calculator",
                                  "src" => "reg_calculator.png",
                                  "href" => "calculator.php");
      $this->nav_images[] = array("name" => "Feedback",
                                  "src" => "reg_feedback.png",
                                  "href" => "feedback.php");
      $this->nav_images[] = array("name" => "Contact",
                                  "src" => "reg_contact.png",
                                  "href" => "contact.php");
    }
    
    public function display_nav($nav_images) {
      foreach($nav_images as $nav) {
        echo '<a href="'.$nav['href'].'"><img src="'.$nav['src'].'" alt="'.$nav['name'].'" border="0" /></a>';
        echo '<br />';
      }
    }
  }
 
  $test = new Test;
  $test->init();
  $test->display_nav($test->nav_images);
?>
killagoat
Forum Newbie
Posts: 7
Joined: Wed Dec 16, 2009 3:49 pm

Re: class help/ navigation

Post by killagoat »

That code worked great! Thank you. I was trying to use a multidimensional array, but couldn't figure it out. At least was somewhat on the right track. Appreciate the help. lol now I can move on in the book. :)
Post Reply