Welcome Guest, Not a member yet? Register   Sign In
Field variable problem
#1

[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
#2

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

[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.
#4

[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?
#5

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

[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
#7

[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!




Theme © iAndrew 2016 - Forum software by © MyBB