Created
February 6, 2019 06:22
-
-
Save RizkiHerdaID/b5b0ed59228af00d630f6c62f55d141a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function index() { | |
$all_categories = $this->categories->get_many_by(array('CHARACTER_LENGTH(kode_kategori) <= 2')); | |
$categories = $this->get_level_0($all_categories); | |
} | |
public function get_level_0($parent) { | |
$results = $this->kategori_m->get_many_by(array('status' => 1)); //capture the query data inside $results variable | |
$menu = array(); | |
foreach ($parent as $list) { | |
array_push($menu, $this->get_parent($results, $list->kode_kategori)); //get_menu() function is bellow | |
} | |
return $menu; | |
} | |
public function get_parent($results, $parent_id = NULL) { | |
$menu = ''; | |
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) { | |
if ($results[$i]->kode_induk_kategori == $parent_id) { | |
if ($this->parent_child($results, $results[$i]->kode_kategori)) { | |
$sub_menu = $this->get_parent($results, $results[$i]->kode_kategori); | |
$menu = $this->addParent($results, $i, $sub_menu, $menu); | |
} else { | |
$menu = $this->addChild($results, $i, $menu); | |
} | |
} | |
} | |
return $menu; | |
} | |
public function parent_child($results, $kode_induk) { | |
for ($i = 0, $iMax = count($results); $i < $iMax; $i++) { | |
if ($this->categoryCodeMatchedAndLessThanSixLength($results, $kode_induk, $i)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private function categoryCodeMatchedAndLessThanSixLength($results, $kode_induk, $i) { | |
return $results[$i]->kode_induk_kategori == $kode_induk && strlen($results[$i]->kode_induk_kategori) <= 6; | |
} | |
private function addParent($results, $i, $sub_menu, $menu) { | |
$menu .= '<button class="list-group-item" data-toggle="collapse" data-target="#sm' . $i . '"> | |
' . $results[$i]->kode_kategori . '. ' . ucwords(strtolower($results[$i]->nama)) . '<span class="badge">' . $this->kategori_m->count_barang($results[$i]->kode_kategori) . '</span>' . '<span class="caret"></span> | |
</button>' . | |
'<div id="sm' . $i . '" class="sublinks collapse">' . | |
$sub_menu . | |
'</div>'; | |
return $menu; | |
} | |
private function addChild($results, $i, $menu) { | |
$menu .= ' <a class="list-group-item" href="' . site_url('homepage/katalog?kategori=') . $results[$i]->kode_kategori . '" style="background-color: #eaeaea"> | |
' . $results[$i]->kode_kategori . '. ' . ucwords(strtolower($results[$i]->nama)) . ' | |
<span class="badge">' . $this->kategori_m->count_barang($results[$i]->kode_kategori) . '</span></a>'; | |
return $menu; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment