1

I'm a newbie to codeigniter and I'm attempting to write a function that would basically save a name and url to session data whenever you visited a certain page, then report it back in a small widget on the screen.

It's supposed to work as a kind of history function for what pages have been accessed and in what order. So far when working with test data it works great! however I'm trying to figure out how I can call the "add" function on each page that is visited, so we can supply the name and url of that page that was visited. Is there any way to do this? Or is there any way to report back a set of variables such as a name and url for a page after you visit it?

For example: say I visit page1, page2, page3, and page6 and I want each of those to show up in my history function. On each of those pages I would load the history view, and I would want to call the history's controller function "add(name,url)" and fill it in something like this:

add('page1','page1.php')

But I know that you're not supposed to access the controller from the history because that's not the way it's supposed to be done, however I cannot think of any better way to do this. Any help would be appreciated.

4

2 回答 2

9

I don't know why dont you call this on every controller. but if you want to call a function of the current controller, you have to get the instance of the current controller this way:

<?php
$CI =& get_instance();
$CI->method($param);
?>
于 2013-05-03T19:47:09.270 回答
0

the easiest way to do this would be to put a method in the constructor of your class. that way it will always run first thing, no matter what you are doing. remember that anything you can do in the controller -- sessions, validation, etc -- you can do in a model.

function __construct() {
parent::__construct();

// load model that has page tracker methods
$this->load->model( 'pagetracker_m' );

// call the method to track the pages, and have it return the results
if ( ! $this->history = $this->pagetracker_m->_trackpage(); ) {   
    echo 'Error getting history ' ; }

 } // end construct


 function something() {

// pass history to $data, then use echo $history->firstpage, etc in view
$data['history'] = $this->history ;

   // call your view etc 

}
于 2013-05-04T01:32:12.660 回答