Advertisement
carlos1993

Generation of Multi Dimentional Menus Using SQL DB Data.

Jun 19th, 2013
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.36 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Author: Carlos Arturo Alaniz
  5.  * Last Modified Date: 06/19/2013
  6.  * Languages: PHP/HTML/HTML
  7.  *
  8.  * Description:
  9.  * This class and script creates a multi dimentional menu
  10.  * using a SQL database and hierarchical structure
  11.  */
  12.  
  13. class menu {
  14.     /* Constructor */
  15.  
  16.     function __construct($content, $label = NULL) {
  17.         $this->ul = array('<ul>', '</ul>');
  18.         $this->li = array('<li>', '</li>');
  19.         $this->content = $content;
  20.         $this->label = $label;
  21.     }
  22.  
  23.     /* Private */
  24.  
  25.     private $label;
  26.     private $ul;
  27.     private $li;
  28.     private $menu_str;
  29.     /* Public */
  30.     public $content;
  31.  
  32.     public function add_level($content, $id) {
  33.         $lenght = count($this->content);
  34.         for ($i = 0; $i < $lenght; $i++) {
  35.             if ($this->content[$i][1] == $id) {
  36.                 $pos = $i;
  37.                 break;
  38.             }
  39.         }
  40.         if (isset($pos)) {
  41.             $label = $this->content[$pos][0];
  42.             unset($this->content[$pos][0]);
  43.             $this->content[$pos][0] = $content;
  44.             $this->content[$pos][0]->label = $label;
  45.         }
  46.     }
  47.  
  48.     public function print_con() {
  49.         print_r($this->content);
  50.     }
  51.  
  52.     public function generateMenu() {
  53.         $this->menu_str = NULL;
  54.         $this->menu_str.= $this->ul[0];
  55.         $size = count($this->content);
  56.         for ($i = 0; $i < $size; $i++) {
  57.             $this->menu_str.= $this->li[0];
  58.             if (!is_object($this->content[$i][0]))
  59.                 $this->menu_str.= $this->content[$i][0];
  60.             else {
  61.                 $this->menu_str.= $this->content[$i][0]->label;
  62.                 $this->menu_str.= $this->content[$i][0]->generateMenu();
  63.             }
  64.             $this->menu_str .= $this->li[1];
  65.         }
  66.         $this->menu_str .= $this->ul[1];
  67.         return $this->menu_str;
  68.     }
  69.  
  70.     public function writeMenu($filepath) {
  71.         file_put_contents($filepath, $this->menu_str);
  72.     }
  73.  
  74. }
  75. /*Connection info*/
  76. $db_host = "localhost";
  77. $db_user = "root";
  78. $db_password = "EHA";
  79. $db_database = "page_system";
  80. $pdo = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_user, $db_password);
  81. /*Connection info*/
  82.  
  83. $qry = "SELECT MAX(parent) FROM menu";
  84. $max_parent = $pdo->query($qry)->fetch();
  85. $content = NULL; //Variable used to store the results of the qery
  86. $menus_objs = array(); //Array to hold the menus and its parent id.
  87. $obj_count = 0;
  88. $level = NULL; // this variable its going to hold
  89. //the last level in the tree
  90. $parent = NULL; //This variable its going to be modified
  91. //in each loop cycle to be later stord in the menu_objs
  92. //array at index 1 as the parent id for that group
  93. $level_element_count = array(); //This array uses the
  94. //$level variable as an index and store how many objects
  95. //exists in each level
  96. //
  97.         //Traverse array $max_parent times and group each elements
  98. //in menu objects according to its parent
  99. //
  100.        for ($i = 0; $i <= $max_parent[0]; $i++) {
  101.     $qry = "SELECT * FROM  menu WHERE parent = $i ORDER BY 'order' ASC";
  102.     $stmt = $pdo->query($qry);
  103.     $content_index = 0; //index for array of elements to be ineserted into a
  104.     //Create elements array to later create a menu obj
  105.     //Clear existing array before continuing
  106.     if (isset($content)) {
  107.         unset($content);
  108.     }
  109.     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  110.         if (!isset($content)) {
  111.             $level = $row['level'];
  112.             $parent = $row['parent'];
  113.         }
  114.         $content[$content_index] = array($row['label'], $row['id']);
  115.         $content_index++;
  116.     }
  117.     //if content exists Create a menu object using the content array
  118.     if (isset($content)) {
  119.         //if the level index array is not created yet initiazlize first
  120.         //value to one otherwise increse that value this array counts
  121.         //how many objects exist in each level.
  122.         if (!isset($level_element_count[$level])) {
  123.             $level_element_count[$level] = 1;
  124.         } else {
  125.             $level_element_count[$level]++;
  126.         }
  127.         //store the object in the correct index and pair it with its
  128.         //corresponding parent id and increment the objs count by one
  129.         $menus_objs[$obj_count][0] = new menu($content);
  130.         $menus_objs[$obj_count][1] = $parent;
  131.         $obj_count++;
  132.     }
  133. }
  134. //add a zero and the end of level element count
  135. $level_element_count[] = 0;
  136. $i = $obj_count;
  137. //Goinf backwards through the levels of the
  138. //array of objects
  139. for ($l = $level; $l > 0; $l--) {
  140.     $c = 0; //counter variable
  141.     for ($q = 0; $q < $level_element_count[$l]; $q++) {
  142.         $i--;
  143.         for ($upl = 0; $upl < $level_element_count[$l - 1]; $upl++) {
  144.             $k = $i + $c - $upl - $level_element_count[$l];
  145.             //
  146.             // $k is index of the corresponding upper level object
  147.             // index of object - qty of objets per level - qty of elements
  148.             // in upper level
  149.             //
  150.                    echo $k;
  151.             $menus_objs[$k][0]->add_level($menus_objs[$i][0], $menus_objs[$i][1]);
  152.         }
  153.         $c++;
  154.     }
  155. }
  156. //At the end we should have all the data from the DB organized
  157. //as a multi-dimentional menu we just have to call the generate menu
  158. //method on the last level object witch its always one, obj [0][0]
  159. $menus_objs[0][0]->generateMenu();
  160. $menus_objs[0][0]->writeMenu("../menu.html");
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement