CodeIgniter Forums
Field variable problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Field variable problem (/showthread.php?tid=13518)



Field variable problem - El Forum - 11-25-2008

[eluser]iainco[/eluser]
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed.');

class Article extends Controller
{
    var $article;
    
    function __construct()
    {
        parent::__construct();
        $this->article = $this->_fetch();
    }

    function index()
    {
        $data = array(
            'article' => $this->article,
            'rating'  => $this->_fetchRating()
        );
    
        $this->load->view('article', $data);
    }
    
    function rate()
    {
        $vote = $this->input->post('vote');
        
        if($vote):
        $this->load->model('assist/rate_model');
        $this->rate_model->save(($vote == 'Up' ? true: false), $this->article['ID']);
        endif;
    }
        
    function _fetch()
    {
        $this->load->model('assist/article_model');
        return $this->article_model->fetch(str_replace('-', ' ', $this->uri->segment(2)));
    }
    
    function _fetchRating()
    {
        $this->load->model('assist/rate_model');
        $data  = $this->rate_model->fetch($this->article['ID']);
        
        $score = $data['up']*2 +($data['user'] - ($data['up'] + $data['dn'])*1);
        $max   = $data['user']*2;
        return ($score / $max)*100;
    }
}
?>

I'm having trouble accessing my field array variable article after the constructor and index methods are called.

When a user accesses http://localhost/article/article-title-here everything works fine, the _fetch helper method is used to get information about the article from the database. This information is what I want to still be available and accessible by all other methods.

But after the view is loaded, and I try the rating function... which calls the rate method, I get an error that $this->article['ID'] is null, in fact, $this->article is also null.

So basically I'm not sure why it's not working, is it just tha I can't expect the article field array variable to still be available for when another method of the controller is accessed?

Please get back to me if you need more information.

Thanks


Field variable problem - El Forum - 11-25-2008

[eluser]uptime[/eluser]
[edited] I got you wrong... Nevermind... :-)


Field variable problem - El Forum - 11-25-2008

[eluser]iainco[/eluser]
Yeh, the index method is working fine, and is loading the view correctly and all information is shown how I want it.

But it's when I come back to call a separate method in controller (rate), by submitting a form on the page that is loaded, it is unable to access the article array.


Field variable problem - El Forum - 11-25-2008

[eluser]uptime[/eluser]
[quote author="Chels87" date="1227655432"]Yeh, the index method is working fine, and is loading the view correctly and all information is shown how I want it.

But it's when I come back to call a separate method in controller (rate), by submitting a form on the page that is loaded, it is unable to access the article array.[/quote]


Add this at the beginning of rate():

Code:
var_dump( $this->_fetch() );
exit;


What does it return?


Field variable problem - El Forum - 11-25-2008

[eluser]Colin Williams[/eluser]
I must assume your rating form submits to the wrong URI.


Field variable problem - El Forum - 11-25-2008

[eluser]iainco[/eluser]
It's okay, the problem is that the constructor for the class is called again when the rate method is called.

I can get it working as I need it, just requires some extra code.

Thanks guys


Field variable problem - El Forum - 11-25-2008

[eluser]uptime[/eluser]
Yeah, the constructor is always called, make sure you have the title in your uri->segment(2) and it's a done deal.

Good luck!