Welcome Guest, Not a member yet? Register   Sign In
get variable from the same class
#1

[eluser]Avril[/eluser]
Hello,

I have class called Blog_model that gets all my blog posts from the database.
This works like a charm, so here's my code:

Code:
<?php

class Blog_model extends Model{

    function getBlogPosts(){
        
        $this->db->select('title, author, content, date_posted');
        $this->db->from('lm_blog_posts');
        $this->db->where('type', 3);
        $q = $this->db->get();
        
        if( $q->num_rows() >= 1){
            foreach( $q->result() as $row ){
                $data[] = $row;
            }
            return $data;
        }
        
    }
    
    function splitTitle(){
    
        
    
    }
    
}

Now I have made a second function that will split the Title for design purposes.
So, how do I get the array from the getBlogPosts function into my splitTitle function?

Something like this?:

Code:
$this->getBlogPosts['title']
#2

[eluser]WanWizard[/eluser]
You will have to make a design decision:

You can add title splitting logic to getBlogPosts().

You can create splitTitle() so that it expects a data array, and then use this in your controller:
Code:
$data = $this->Blog_model->splitTitle( $this->Blog_model->getBlogPosts() );

or create splitTitle() so that it expects a string 'title', and then use this in your controller:
Code:
$data = $this->Blog_model->getBlogPosts();
foreach ( $data as $key => $value )
{
   $data[$key]['title'] = $this->Blog_model->splitTitle( $value['title'] );
}

You can also code splitTitle() to be an enhancement to getBlogPosts(). Have splitTitle() call getBlogPosts(), loop over the result like above, and return the array with converted titles.

Plenty of options to choose from... I would go for the last option. Keep it clean, and all data processing in the model.
#3

[eluser]Avril[/eluser]
Hey, thanks for the reply.

Well I'm planning to keep the code in de model, that's why I created the splitTitle function.
But I don't know how to call the getBlogPosts function from within the same model. Is it something like:
Code:
$this->blog_model->getBlogPosts()
??
#4

[eluser]WanWizard[/eluser]
It's a method local to the class, so
Code:
$data = $this->getBlogPosts();
will do. No CI magic involved.
#5

[eluser]Avril[/eluser]
damn your right, it's OOP PHP .. D'oh!




Theme © iAndrew 2016 - Forum software by © MyBB