Here I have my database table and my table name is category where I store my data category and subcategory in category column
i want to fetch this data like this format in ul li for this code i have trying to arrange but i am not fetch data perfectly like this please help me to arrange my code this format.
You can try like this
//to select categories
$this ->db-> select();
$this->db->from('category');
$this->db->where('pid',0);
$query = $this->db->get();
$categories = array();
$i=0;
foreach ($query->result_array() as $row) {
//put all category names to $categories array
$categories[$i] =$row['category'];
$i++;
}
//to select relevant sub categories
$sub_categories=array();
foreach ($categories as $category) {
$this ->db-> select();
$this->db->from('category');
$this->db->like('category', $category, 'after');
$query = $this->db->get();
$j=0;
foreach ($query->result_array() as $row) {
//put all sub categories names to $sub_categories array
$sub_categories[$category][$j] =$row['category'];
$j++;
}
}
$return_data['categories']=$categories;
$return_data['sub_categories']=$sub_categories;
return $return_data;
This code is useful for get nth level of category,subcategory array
controller:
$this->load->model('getmenu_model');
function get_menu() {
$ci = & get_instance();
$ci->load->model("getmenu_model");
$menu['menu'] = $ci->getmenu_model->get_menu();
$echo = echoMenu($menu['menu']);
//$echo.= "<li>Loose Diamonds</li>";
$menu['print_menu']=$echo;
print_r($menu);
//return $menu;
}
function echoMenu($arr) {
$ci = & get_instance();
$echo = "";
//$echo.="<ul>";
foreach ($arr as $subArr) {
if (!empty($subArr['sub_menu'])) {
$echo.= "<li>";
$echo.= "" . $subArr['name'] . "";
if ($subArr['sub_menu']) {
$echo.= "<ul>";
$echo .=echoMenu($subArr['sub_menu']);
$echo.= "</ul>";
}
$echo.= "</li>";
} else {
$echo.= "<li>" . $subArr['name'] . "</li>";
}
}
//$echo.= "</ul>";
return $echo;
}
model:
public function get_menu() {
$this->db->select('id,name,parent_id');
$menu = $this->db->get('category')->result_array();
$data=$this->menu_child($menu);
//print_r($data);
return $data;
}
function menu_child($menu, $parent = NULL) {
// echo $parent."---";
$main_menu = array_filter($menu, function($a)use($parent) {
return $a['parent_id'] == $parent;
});
// print_r($main_menu);
if ($main_menu) {
foreach ($main_menu as $key => $value) {
$main_menu[$key]['sub_menu'] = $this->menu_child($menu, $value['id']);
}
}
// print_r($main_menu);
return $main_menu;
}
Related
I am trying to build an un-oredered list menu tree from my database in PHP and MySQL from here
I have an array of page objects I am returning from the db. Each page object has parent_id attribute, which is set to null if it doesn't have a parent. Here's what the page objects look like:
mydatabase
PHP Handling
$menu = mysqli_query($conn, "SELECT * FROM kategori ORDER BY id_kategori DESC");
while($row=mysqli_fetch_assoc($menu)) {
}
function has_children($rows,$id) {
foreach ($rows as $row) {
if ($row['parent'] == $id)
return true;
}
return false;
}
function build_menu($rows,$parent=0)
{
$result = "<ul>";
foreach ($rows as $row)
{
if ($row['parent'] == $parent){
$result.= "<li>{$row['nama_kategori']}";
if (has_children($rows,$row['id_kategori']))
$result.= build_menu($rows,$row['id_kategori']);
$result.= "</li>";
}
}
$result.= "</ul>";
return $result;
}
echo build_menu($menu);
This isn't outputting the correct results for me, can anyone help me
I want to try to add foreach on controllers , because before I show you foreach on views
This my Controllers
$data['report_data'] = $this->hasil_m->get_data($nomor);
//create foreach here, from data $data['report_data']
$data=array('title' =>'KOPKAR - Pelanggan',
'isi' =>'admin/hasil'
);
$this->load->view('dashboard/wrapper',$data);
This my Models
$this->db->select('*');
$this->db->from('tb_produk as pro');
$this->db->join('tb_reseller as res', 'pro.reseller_produk = res.nomor_reseller');
$this->db->join('tb_pelanggan as pel', 'pro.id_barcode = pel.id_barcode');
$this->db->select_sum('jumlah');
$this->db->group_by('res.nomor_reseller');
$ambil = $this->db->get('');
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
This my Views
Instead of doing :
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
return $hasil;
just
return $ambil->result_array();
I would to create a recursive function in order to retrieve data from an array and to organize then.
However I have some difficulties to create the right logic. The principle must be apply to any sub level until it ends or nothing is found.
I want to prevent this kind of code by repeating a foreach inside a foreach...:
$cats = get_categories($args);
$categories = array();
foreach($cats as $cat){
$parent = $cat->category_parent;
if ($parent) {
$categories['child'][$parent][$cat->cat_ID] = $cat->name;
} else {
$categories['parent'][$cat->cat_ID] = $cat->name;
}
}
}
if (isset($categories['parent']) && !empty($categories['parent'])) {
foreach($categories['parent'] as $id => $cat){
$new_cats[$id]['title'] = $cat;
if (isset($categories['child'][$id])) {
foreach($categories['child'][$id] as $child_id => $child_cat){
$new_cats[$child_id]['title'] = $child_cat;
$new_cats[$child_id]['parent_id'] = $id;
if (isset($categories['child'][$child_id])) {
foreach($categories['child'][$child_id] as $sub_child_id => $sub_child_cat){
$new_cats[$sub_child_id]['title'] = $sub_child_cat;
$new_cats[$sub_child_id]['parent_id'] = $child_id;
}
}
}
}
}
}
}
May be this code help you to get idea for formatting your desired array format.
<?php
// Main function
function BuildArr($arr)
{
$formattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$returnArr = SubBuildArr($val['children']); // call recursive function
if(!empty($rs))
{
$formattedArr[] = $returnArr;
}
}
else
{
$formattedArr[] = $val;
}
}
}
return $formattedArr;
}
// Recursive Function( Build child )
function SubBuildArr($arr)
{
$sub_fortmattedArr = array();
if(!empty($arr))
{
foreach($arr as $val)
{
if($val['has_children'])
{
$response = SubBuildArr($val['children']); // call recursive
if(!empty($response))
{
$sub_fortmattedArr[] = $response;
}
}
else
{
$sub_fortmattedArr[] = $arr;
}
}
return $sub_fortmattedArr;
}
}
?>
I use this code for my previous project for generating categories that upto n-th level
print_r($listb) is working correctly in controller. but when we are passing this array to view page, its not working in the view page. Controller and view page and Model function is given below:
Controller
public function index() {
$this->load->helper('url');
$this->load->view('user/templates/header');
$data['banner'] = $this->banner_model->viewbanner();
$this->load->view('user/templates/sidebar', $data);
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$listb = array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$this->load->view('user/templates/featured', $listb);
exit;
$this->load->view('user/templates/footer');
}
}`enter code here`
View
<?php
print_r($hhh);
?>
Model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
return $query->result();
}
In your controller Change this Line :
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
to
$listb['hhh'][] = $this->featured_model->viewb2bproduct($pid);
In view :
<?php
foreach ($fff as $product){
echo $products[0]['prd_id']."<br>";
echo $products[0]['prd_name']."<br>"; //and so on
}
Your $listb['hhh'] is in foreach loop and you are overriding it each time for every element of $list. and in controller you are printing it inside the foreach loop thats why it is giving output but not in your view.
In model
public function viewb2bproduct($id) {
$this->db->select('*');
$this->db->from('jil_products');
$this->db->where('prd_id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In controller
public function index()
{
$this->load->helper('url');
$data['banner'] = $this->banner_model->viewbanner();
$data1['list1'] = $this->featured_model->viewfeaturedlist(1);
$this->load->view('user/templates/sidebar', $data);
$this->load->view('user/templates/header');
$this->load->view('user/templates/featured', $listb);
$this->load->view('user/templates/footer');
}
}
and in view
<?php
foreach ($list1 as $new_list1)
{
echo "<p>".$new_list1['table_field']."</p>"
}
I have no idea on this part
$listb = array();
foreach ($data1['list1'] as $list1)
{
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$hhh=array();
foreach ($data1['list1'] as $list1) {
$list = explode(',', $list1->fet_list);
$type = $list1->fet_type;
foreach ($list as $pid) {
if ($type == 1) {
$hhh[] = $this->featured_model->viewb2bproduct($pid);
//print_r($listb);
}
}
}
$listb['hhh']=$hhh;
$this->load->view('user/templates/featured', $listb);
I have made function to get multidemnsional menu from database.
public function get_menu($parent=0,$vis=1) {
$categories = array();
$this->db->from('ci_categories');
$this->db->where('cat_child',$parent);
$this->db->where('cat_vis',1);
$this->db->order_by('cat_order');
$q = $this->db->get();
$result = $q->result();
$i=0;
foreach($result as $mainCategory) {
$mainCategory->cat_subcategories = $this->get_menu($mainCategory->cat_id);
$categories[$i] = $mainCategory;
$i++;
}
return $categories;
}
It is working but I would like to print out the menus. For now I can print main menu and one child with this code:
if (count($menu) > 0) {
foreach($menu as $menu_item) {
$link = anchor("category/".$menu_item->cat_url, $menu_item->cat_name);
echo "<li>$link";
if (!empty($menu_item->cat_subcategories)) {
echo "<ul>";
foreach($menu_item->cat_subcategories as $subcat) {
$sub_link = anchor("category/".$subcat->cat_url, $subcat->cat_name);
echo "<li>$sub_link</li>";
}
echo "</ul>";
}
echo "</li>";
}
}
It is working too but how could I print out even 3rd or 4th submenu without manualy writing those foreach and ifs ? Thanks
You can try a recursive function...
Something like:
function showMenu($menu) {
echo "<ul>";
foreach ($menu as $menu_item) {
$link = anchor("category/".$menu_item->cat_url, $menu_item->cat_name);
echo "<li>$link";
if (!empty($menu_item->cat_subcategories)) showMenu($menu_item->cat_subcategories);
echo "</li>";
}
echo "</ul>";
}
That's an idea ;)