Membuat Menu Horizontal Pada PHP OOP (Object Oriented Programming)

Hallo gan, berikut ini adalah contoh Object Oriented Programming yang sangat sederhana, yaitu membuat menu horizontal dengan cara memanggil class dari menu tersebut.

Langsung saja ke contoh programnya, perhatikan kode program dibawah. Pada code program yang pertama adalah class dari menu. Pada class tersebuat kita membuat ada dua method atau function. Kode program pertama saya beri nama menu.php

<?php
class menuAtas{

var $data = array();
var $jlh = 0;

function displayMenu(){

echo "<table border='1' align='center' width='100%'><tr>";
for($j=0; $j<count($this->data); $j++){

echo "<td align='center'><a href='".$this->data[$j]['label']."'>".$this->data[$j]['name']."</a></td>";
}
echo "</tr></table>";

}

function addMenu($name, $label){

$this->data[$this->jlh]['name'] = $name;
$this->data[$this->jlh]['label'] = $label;
$this->jlh++;

}

}
?>


Dan kode program yang kedua adalah untuk memanggil class yang ada pada menu.php diatas. Saya beri nama index.php

<?php
include "menu.php";

$form = new menuAtas();
$form->addMenu(
"Home","#.php");
$form->addMenu(
"Data","#.php");
$form->addMenu(
"Profile","#.php");
$form->addMenu(
"Contact","#.php");
$form->displayMenu();
?>

Demikian share tutorial dari saya, semoga bermafaat. GBU


Share on Google Plus

About Mr.J

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Posting Komentar