lablanco
lablanco

Reputation: 103

Get categories in function in CodeIgniter not working

I made a function in the CodeIgniter framework so I can see all my categories of products from my database on a side menu bar but I'm getting an error when I'm trying to echo all the categories.. I used a db_helper.php file to do it.

Some database info:

db category table name: categories
I have 2 rows in the categories table:

Row 1 name of categories table: id
Row 2 name of categories table: name

This is my db_helper.php file:

<?php

function get_categories_h(){
    $CI = get_instance();
    $categories = $CI->Product_model->get_categories();
    return $categories;
}

This is my Product_model.php file:

<?php
defined('BASEPATH') OR exit ('No direct script access allowed');

class Product_model extends CI_model {

    public function saveProduct($data)
    {
        {
            $this->db->insert('products', $data);
            $product_id = $this->db->insert_id();
        }

        return $product_id;
    }



public function get_product_details($product_id) { 
$arrReturn = array(); 
$this->db->select('*'); 
$this->db->from('products'); 
$this->db->where('product_id', $product_id); 
$query = $this->db->get(); 
$result = $query->result_array(); 
if(!empty($result)){ 
$arrReturn = $result[0]; 
} 
return $arrReturn; 
}



/*
Get categories
*/

 function get_categories(){
        $ci =& get_instance();
        $ci->db->select('*');
        $ci->db->from('categories');
        $ci->db->get()->result_array();
        return $q;
    }


}

?>

    public function get_categories(){
        $this->db->select('*'); 
    $this->db->from('categories'); 
    $query = $this->db->get(); 
    $result = $query->result_array();
    }

And this is the sidebar in a view file where I'm trying to echo all the categories of my database (allecadeaus.php) :

<div class="container-fluid">
<div class="row">
    <div class="col-lg-3">
<div id="categorymenu">
  <center>  <h3>Categorieën</h3> </center>
        <ul class="list-group">
            <?php foreach(get_categories_h() as $category) : ?>
            <li class="list-group-item"><a href="#"><?php echo $category->name; ?> </a> </li>
            <?php endforeach; ?>
        </ul>
            </div>
            </div>

When I try to load the view file I see this error on the sidebar instead of the categories:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/allecadeaus.php

Line Number: 25

Backtrace:

File: /home/ubuntu/workspace/application/views/allecadeaus.php
Line: 25
Function: _error_handler

File: /home/ubuntu/workspace/application/controllers/AlleCadeausController.php
Line: 12
Function: view

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

When I print output of get categories:

A PHP Error was encountered



Severity: Notice


Message:  Undefined variable: q


Filename: models/Product_model.php


Line Number: 42





Backtrace:










            File: /home/ubuntu/workspace/application/models/Product_model.php

            Line: 42

            Function: _error_handler            








            File: /home/ubuntu/workspace/application/helpers/db_helper.php

            Line: 6

            Function: get_categories            








            File: /home/ubuntu/workspace/application/views/allecadeaus.php

            Line: 38

            Function: get_categories_h          












            File: /home/ubuntu/workspace/application/controllers/AlleCadeausController.php

            Line: 12

            Function: view          












            File: /home/ubuntu/workspace/index.php

            Line: 315

            Function: require_once  

Upvotes: 0

Views: 484

Answers (2)

NikuNj Rathod
NikuNj Rathod

Reputation: 1658

You can change your code as following solution.

Please add helper in autoload.php file:

File Path : application/config/autoload.php

<?php 
/*
  | -------------------------------------------------------------------
  |  Auto-load Helper Files
  | -------------------------------------------------------------------
  | Prototype:
  |
  |   $autoload['helper'] = array('url', 'file');
 */

$autoload['helper'] = array('db_helper');

?>

Please change your db_helper.php file:

File Path : application/helpers/db_helper.php

<?php if (!function_exists('get_categories_h')) {
    function get_categories_h(){
        $CI = get_instance();
        $categories = $CI->Product_model->get_categories();
        return $categories;
    } } ?>

You can check your modal file :

File Path : application/modal/Product_model.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product_model extends CI_model {

    public function saveProduct($data) { 
        $this->db->insert('products', $data);
        $product_id = $this->db->insert_id();
        return $product_id;
    }
    public function get_product_details($product_id) {
        $arrReturn = array();
        $this->db->select('*');
        $this->db->from('products');
        $this->db->where('product_id', $product_id);
        $query = $this->db->get();
        $result = $query->result_array();
        if (!empty($result)) {
            $arrReturn = $result[0];
        }
        return $arrReturn;
    }
    /*
      Get categories
     */
    public function get_categories(){
        $this->db->select('*'); 
        $this->db->from('categories'); 
        $query = $this->db->get(); 
        $result = $query->result_array();
        return $result;
    }
}
?>

You can change your view file.

<div class="container-fluid">
        <div class="row">
            <div class="col-lg-3">
                <div id="categorymenu">
                    <center>  <h3>Categorieën</h3> </center>
                    <ul class="list-group">
                        <?php foreach (get_categories_h() as $category) : ?>
                            <li class="list-group-item">
                                <a href="#"><?php echo $category['name']; ?>
                                </a> 
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>

I hope this will helps you. Thanks!

Upvotes: 2

Sushant Bassi
Sushant Bassi

Reputation: 379

Instead of calling a model from a helper you can code in the function in the helper itsef and return the result to the view file. Plus you need to add the helper in the autoupload.php file in the cofig directory.

 function get_categories(){
        $ci =& get_instance();
        $ci->db->select('*');
        $ci->db->from('categories');
        $q= $ci->db->get()->result();
        return $q;
    }

Upvotes: 0

Related Questions