2

I have a problem in codeigniter. I call a funtion in controller Articles from the view.

<li><a href="index.php/articles/Category/1" ><?php echo $item['CATEGORY'] ?></a></li>

The function

   function getCategories() {

    $this->db->select('ID_CAT,CATEGORY');
    $this->db->from('CATEGORY');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return FALSE;
    }
    }

 function Category($id_cat) {
    if (!$id_cat) {
        $id_cat = 0;
    }

    $this->load->model('graf/home_model');
    $data = array();
    $data['categories'] = $this->home_model->getCategories();
    $filtro_cat = $id_cat;
    $data['article'] = $this->home_model->getArticles($filtro_cat);
    $this->load->view('graf/articles', $data, TRUE);

   }

home_model

function getArticles($filtro_cat) {

    $this->db->select('*');
    $this->db->from('ARTICLES');
    if ($filtro_cat != 0) {
        $this->db->where("ID_CAT=".$filtro_cat);
    }
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return FALSE;
    }
}

When i select the button the url changes to http://localhost/Graf/index.php/articles/Category/1 but does not load http://localhost/Graf/index.php/articles/

Why?

3
  • is method ->getCategories() declared inside home_model class? Commented Sep 21, 2016 at 0:05
  • yes, home_model have a getCategories and getArticles. Commented Sep 21, 2016 at 0:11
  • try to change the function name as category Commented Sep 21, 2016 at 3:41

2 Answers 2

2

When you set third argument of load->view() to true it returns string value and does not output anything

Try changing:

$this->load->view('graf/articles', $data, TRUE);

to

$this->load->view('graf/articles', $data);
Sign up to request clarification or add additional context in comments.

3 Comments

I change by $this->load->view('graf/articles', $data); but the url is the same localhost/Graf/index.php/articles/Category/1. No call the index function in the controller.
it won't call the index function...that's how codeigniter routing works. Read the docs on how controllers and routes work
i read but i dont know why not load the view, even if i use only $this->load->view('graf/articles') when i call the function Category.
1

set the routes.php in folder config, add this code;

$route['articles/Category/(.*)'] = 'articles/$1';

and change the link

<li><a href="<?php echo site_url('index.php/articles/'.$item['ID_CAT']); ?>" ><?php echo $item['CATEGORY'] ?></a></li>

and change your controller to this

$this->load->view('graf/articles', $data);

1 Comment

I apply the changes, but is the same, the url does not load the view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.