[trick] Membuat Tree Menu dengan PHP MYSQL

BIsmillah…

ok langsung saja, kali ini kita coba bahas ttg menu tree pake database ..

siapkan database:

menu

 

 

masuk ke code :

1. fungsi menu :


<?php

function get_menu($data, $parent = 0) {
 static $i = 1;
 $tab = str_repeat(" ", $i);
 if ($data[$parent]) {
 $html = "$tab<ul id='menu-tree' class='filetree'>";
 $i++;
 foreach ($data[$parent] as $v) {
 $child = get_menu($data, $v->id_menu);
 $html .= "$tab<li>";
 $html .= '<a href="'.$v->url.'">'.$v->title.'</a>';
 if ($child) {
 $i--;
 $html .= $child;
 $html .= "$tab";
 }
 $html .= '</li>';
 }
 $html .= "$tab</ul>";
 return $html;
 }
 else {
 return false;
 }
 }

?>

 

2.  menu.php


<?php

include"koneksi.php";
include"fungsi_menu.php";

$sql=mysql_query("select * from menu ORDER BY menu_order");

while ($row = mysql_fetch_object($sql)) {
 $data[$row->parent_id][] = $row;
 }
 $menu = get_menu($data);
 echo "$menu";
?>

 

3. menu tree.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Menu Vertikal</title>
<script type="text/javascript"
 src="jquery-1.5.1.min.js"></script>
<script type="text/javascript"
 src="jquery.treeview.js"></script>
<link rel="stylesheet" type="text/css"
 href="jquery.treeview.css" />
<script type="text/javascript">
 $(document).ready(function() {
 $("#menu-tree").treeview();
 });
</script>
<style type="text/css">
 body {
 font-family: Verdana, helvetica, arial, sans-serif;
 font-size: 68.75%;
 background: #fff;
 color: #333;
 }
</style>
</head>
<body>

 <?php include"menu.php";?>

</body>
</html>

4. koneksi.php


<?php
mysql_connect("localhost","root","");
mysql_select_db("rsia");
?>

 

dan hasilnya seperti ini :

hasil_menu

 

 

untuk link download silahkan disini 

Leave a comment

8 Comments

  1. Keren Om makasih semoga Blog ini selalu memberi ilmu ilmu php dari newbie sampai master🙂

    Reply
  2. andik

     /  28/03/2014

    Ko error gan…tlg di cek

    Notice: Undefined offset: 1 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 3 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 5 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 6 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 7 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 9 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 10 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 11 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 12 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 13 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 15 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 16 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 17 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 19 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 20 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Notice: Undefined offset: 21 in F:\xampp\htdocs\jcckotagajah\javasmenu\fungsi_menu.php on line 6

    Reply
    • Ane Udh Cek Gan, Ng Ada Error Tuh Gan, Coba Di Download Lengkap Aja gan. kalau ng agan bisa cantumin email di komen, nanti saya kirim ke emailnya agan..

      Reply
  3. leart

     /  10/12/2014

    ini errornya:

    Warning: mysql_connect(): Access denied for user ‘root’@’localhost’ (using password: NO) in C:\xampp\htdocs\menu_tree\koneksi.php on line 2

    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\menu_tree\menu.php on line 8

    Notice: Undefined variable: data in C:\xampp\htdocs\menu_tree\menu.php on line 11

    mohon bantuannya.
    tks

    Reply
    • warning pertama, cek koneksi.php nya gan, bisa jadi password nya bukan null atau lainnya.

      warning kedua liat query selectnya gan

      Reply
  4. Mantap…
    kalo ada yang error “Notice: Undefined index:” itu dari versi php nya

    if ($data[$parent]) {

    ganti menjadi

    if (isset($data[$parent])) {

    Reply
  5. Bagaimana agar bisa menunya collepse tidak langsung Expand Gan?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: