Welcome Guest, Not a member yet? Register   Sign In
general function call
#1

[eluser]dizyn[/eluser]
Hi,

I need a function that will give me list of all the pages in database. I want to access that function anytime from anywhere. for example i wanted to access it from footer. I am new to CI, can any help me how to write that kind of function and how to access its output in footer? Here is my controller code, i have already made a function in model.

Thanks in advance .

Code:
<?php
class Pages extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('pages_model');
}

public function index()
{
  $data['contents_item']['title'] = 'My Home page with con';

  $data['title'] = $data['contents_item']['title'];
  $this->load->view('pages/header', $data);
  $this->load->view('pages/index', $data);
  $this->load->view('pages/footer');
}



public function registar()
{
  $data['contents_item']['title'] = 'registar';

  $data['title'] = $data['contents_item']['title'];
  $this->load->view('pages/header', $data);
  $this->load->view('pages/register', $data);
  $this->load->view('pages/footer');
}


public function view($slug)
{
  $data['contents_item'] = $this->pages_model->get_pages($slug);

  if (empty($data['contents_item']))
  {
   show_404();
  }

  $data['title'] = $data['contents_item']['title'];

  $this->load->view('pages/header', $data);
  $this->load->view('pages/view', $data);
  $this->load->view('pages/footer');
}

}
#2

[eluser]InsiteFX[/eluser]
Would be something like this, if the model is already loaded. You may need to create a library and call it from there.

You can create a helper in /.application/helpers/util_helper.php
Code:
public function list_pages()
{
    // get the CI super object
    $CI =& get_instance();

    return $this->CI->model_name->method();
}

Then in your views you can do something like this.
Code:
<?php echo list_pages(); ?>

This will depend on if you need to use a foreach loop to display them.
#3

[eluser]dizyn[/eluser]
I made file named util_helper.php in helpers folder with following code. Now can i load my helper using $this->load->helper('util_helper'); ? this loading will be model or controller?
When i created following herper got this error:
Parse error: syntax error, unexpected T_PUBLIC in /home/onlineq/public_html/application/helpers/util_helper.php on line 2
thanks

Code:
public function list_pages()
{
    // get the CI super object
    $CI =& get_instance();

    return $this->CI->pages_model->get_sitepages();
}
#4

[eluser]dizyn[/eluser]
its fixed, i used:
Code:
function list_pages()
{
    // get the CI super object
    $CI =& get_instance();

    return $this->CI->pages_model->get_sitepages();
}

Now, i have loaded this helper in controller like:
$this->load->helper('util_helper');

i have view file footer.php, how to call this in footer?
thanks


#5

[eluser]InsiteFX[/eluser]
If you are going to use it alot then you could auto load it in ,/application/config/autoload.php




Theme © iAndrew 2016 - Forum software by © MyBB