Welcome Guest, Not a member yet? Register   Sign In
What are the difference between $this->data->total_pages and $data['total_pages']

Beginner's question.

Could anyone explain the differences between



My understanding is that the first one is an object way and second way is an array way.

Am I right?

Then what is $this in $this->data->total_pages?

When do I need to use $this->..?

Do I have to use it whenever I use it as an object?

I thank you in advance.

$this is the object in the context that it's being called used (as in "this" instance of the class), and you can only use $this in object methods.

class blog_object {
  function print_data(){
    echo $this->title . "<br/>";
    echo $this->body . "<br/>";
    echo $this->author . "<br/>";

$blog = new blog_object;
$blog->title = "A Blog";
$blog->body = "This is the text of a blog";
$blog->author = "Me";


Thanks for your reply.

Why $this->data->total_pages is not $this->total_pages?

What is the reason to add data?

You really need to read that OOP link.

Hopefully I can provide some basic insight if you understand OOP in PHP well enough.

CodeIgniter runs from one index.php file. For this to work, CI has to load a core object (objects are also called classes.) That core object's properties and methods can be conveniently referred to in your subsequent code using PHP's built in $this variable. That's because all the subsequent classes (objects) are an extension of the core object (see what I mean about reading up on OOP?) It just so happens that CI can load a data object to the core object called data with all the properties and methods you need to do database transactions (thus, $this->data.) total_pages from $this->data->total_pages is simply a property of the data object (although it would appear that it is an extended version of that class since I am not aware of a total_pages property.) I have no idea what it refers to without context. So I can't answer:

Quote:Why $this->data->total_pages is not $this->total_pages?

As for $data['total_pages']...

That is just how you'd refer to the value stored in an array called data with an associate key called total_pages. The data array in CI convention (can be called whatever you like) is most often used to pass an array of data to a view.

@bretticus, thank you for your reply and time. I appreciate it.

The code I am studying is pryrocms by Phil.

He uses this $this->data->total_pages in controller and in a view as well.

In view,
            &lt;?php if($this->data->total_pages == 1)
                echo '1 Page';
                echo $this->data->total_pages . ' Pages';

Is it common to do it?

I thought $data['total_page'] in controller and use it as $total_page is more CI way.

Is there any benefit using $this->data->total_pages rather than $data['total_page']?

Theme © iAndrew 2016 - Forum software by © MyBB