CodeIgniter Forums
Adding to data array for all functions in model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Adding to data array for all functions in model (/thread-11821.html)



Adding to data array for all functions in model - El Forum - 09-24-2008

[eluser]Kolin[/eluser]
Hi There,

I'm just in the process of learning OOphp and codeigniter
This is a bit of a nub question im sure but i cant figure it out.

I'm using the array $data[] to send data to my view.

I want to add the section name to $data['section'] for all functions in the model but it wont work.

The error i get it

Quote:Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/compirc/public_html/development/system/application/models/articlemodel.php on line 9

The code i'm using is

Code:
<?php

// model
// articles.php
//  

class Articlemodel extends Model {

    var $data['section'] = "articles";

    function __construct() {
        parent::__construct();
    }
    
    
    function getCategories(){
        $query = $this->db->query("SELECT * FROM article_categories");
        $categories = $query->result_array();
        $data['categories'] = $categories;
        return $data;
    }
    
    function getArticles(){
        $category = $this->uri->segment(3);
        $query = $this->db->query("SELECT users.username, articles.id, articles.name, articles.description, articles.author_id, articles.created FROM articles LEFT JOIN users ON articles.author_id = users.id WHERE category_id = $category");
        $articles = $query->result_array();
        $data['articles'] = $articles;
        return $data;
    }
    
    function getArticle(){
        $article = $this->uri->segment(3);
        $query = $this->db->query("SELECT * from articles where id = $article");
        $article = $query->row();
        $data['article'] = $article;
        return $data;
    }
    
}

?>

Any help on how to get this to work, or a better way to go about it would be appreciated.

Thanks,
Kolin


Adding to data array for all functions in model - El Forum - 09-24-2008

[eluser]Pascal Kriete[/eluser]
Two options:
Code:
// Should work
var $data = array('section' => 'articles');

// Or (better):
var $data;

function __construct()
{
    parent::__construct();
    $this->data['section'] = 'articles';
}



Adding to data array for all functions in model - El Forum - 09-24-2008

[eluser]Kolin[/eluser]
Thanks for the reply,

i added

Code:
print_r($data);

to the view used for the getArticles function then tried both ways you suggested and the array doesn't contain "section"

Is there anything else i could be doing wrong?

Just out of interest, why is the second way considered better?

--Edit--

Amended code

Code:
<?php

// model
// articles.php
//  

class Articlemodel extends Model {

var $data = array('section' => 'articles');

    //var $data;
    function __construct() {
        parent::__construct();
        //$this->data['section'] = 'articles';
    }
    
    
    function getCategories(){
        $query = $this->db->query("SELECT * FROM article_categories");
        $categories = $query->result_array();
        $data['categories'] = $categories;
        return $data;
    }
    
    function getArticles(){
        $category = $this->uri->segment(3);
        $query = $this->db->query("SELECT users.username, articles.id, articles.name, articles.description, articles.author_id, articles.created FROM articles LEFT JOIN users ON articles.author_id = users.id WHERE category_id = $category");
        $articles = $query->result_array();
        $data['articles'] = $articles;
        return $data;
    }
    
    function getArticle(){
        $article = $this->uri->segment(3);
        $query = $this->db->query("SELECT * from articles where id = $article");
        $article = $query->row();
        $data['article'] = $article;
        return $data;
    }
    
}

?>

print_r output

Code:
Array ( [view] => articles/category [vars] => Array ( [articles] => Array ( [0] => Array ( [username] => Kolin [id] => 1 [name] => Cat 1 - Test article 1 [description] => Cat 1 - Test article 1 [author_id] => 1 [created] => 2008-09-23 ) [1] => Array ( [username] => Kolin [id] => 2 [name] => Cat 1 - Test article 2 [description] => Cat 1 - Test article 2 [author_id] => 1 [created] => 2008-09-23 ) ) ) [return] => )



Adding to data array for all functions in model - El Forum - 09-24-2008

[eluser]Pascal Kriete[/eluser]
$data is a class variable, so you have to access it in the class scope, by using $this:
Code:
function getCategories(){
    $query = $this->db->query("SELECT * FROM article_categories");
    $categories = $query->result_array();
    $this->data['categories'] = $categories;
    return $this->data;
}



Adding to data array for all functions in model - El Forum - 09-24-2008

[eluser]Kolin[/eluser]
Works perfectly.

Thanks a lot.

Kolin