CodeIgniter – Read

CodeIgniter – Read

Today, I will show you how to get results from database and print in in table.

This is very common operation and it is not to complicated, especially when using CodeIgniter’s Active Record for database communication.

In case you missed our previous tutorial with adding records to database, you can find it on the link bellow

CodeIgniter – Insert Tutorial

Lets start our lesson 2, CodeIgniter – Read

Controller

We need to modify our Site controller’s index function because we use it for displaying our results.

We will store results from our model into $data array that we’ll pass to the view.

Site.php

 

Model

We need a new function that will get data we submitted to database.

So, we added a new function called get that returns data from components table.

Component.php

 

View

We’ll now modify site.php view and add a table that will contain our results.

Every result will be shown in new table row.

In case that components table is empty, it will print that there are no added components.

site.php

Results

Now when we go to our CodeIgniter’s homepage, we’ll see results read from database.

codeigniter-read-1

Go to New Component and add a few more components, it will look like this

codeigniter-read-2


That concludes our Lesson 2 – CodeIgniter – Read

Stay tuned for the next lesson where we will edit some values from database.

You can download complete source code from this lesson on the link bellow

CodeIgniter Lesson 2 РRead

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *