Simplified tutorials to learn the popular basic language C


Learn the basic desktop development application language


One of the popular PHP three tier (MVC) framework

Getting Multiple Database Table Rows - CodeIgniter

We already inserted many values into the database and we learnt the retrieval of data into our project. But in our data selection tutorial, we retrieved data one by one. Here let us learn how to get all or multiple data from our database table. This is the fetching of database rows.

First of all, let us create a controller to retrieve all the data.

Controller (getalldata):

public function getalldata()
    $data['query'] =  $this->ProjectModel->getalldata();


Controller calls a model named 'ProjectModel' is loaded.

$data['query'] =  $this->ProjectModel->getalldata();
$this->ProjectModel->getalldata(); calls a model function named 'getalldata'.

Model (getalldata):

public function getalldata()
  $query = $this->db->query("Select * from table_user");
  return $query->result();

Here the select query selects all the data from our database table. We can add conditions using 'where' clause.

$this->db->query("Select * from table_user WHERE user_age > 20");

return $query->result();
Here the query returns the given database retrieved result into the controller class. Note that, here retrieved data contains multiple values.

In Controller:

$data['query'] =  $this->ProjectModel->getalldata();
Due to the multiple retrieved data, the result should be stored on an array variable.  Here $data['query'] is an array variable, where $data is the variable name.

$this->load->view('user/showdata', $data);
Here the variable $data is send into the view page named 'showdata'.

View (showdata.php):

  <th>User name</th>
<?php foreach($query as $row): ?>
  <td><?php echo $row->user_name; ?></td>
  <td><?php echo $row->user_age; ?></td>
  <td><?php echo $row->user_email; ?></td>
<?php endforeach; ?>

In view page, we should use a loop to get individual data.  Here foreach loop is used.

<?php foreach($query as $row): ?>
In foreach loop, array variable $query is converted into individual result using a variable $row.
Now variable $row contains a row of data(tuple). So each row contains value of different columns in the database table such as user_name, user_age, etc.

echo $row->user_name; can print a row data with a column name of user_name.

Here we use <table> to align the data. So table tags such as <tbody>,<th>,<tr> and <td> are used.

<?php endforeach; ?>
This is the end of the foreach loop.

Sample Output:

Retrieve all database table rows data in CodeIgniter

You can customize the data by specifying different columns name on the view section, also you can modify the style of the document and data arrangement using HTML and CSS codes.

Retrieving Uploaded Images in CodeIgniter

In our previous lessons, we successfully uploaded files into the project directory and inserted the uploaded file name with extension to the database.

How to get Uploaded File Name and Insert into the Database

In our previous lesson, we successfully uploaded files into the project directory. There we just uploaded files into the project image folder, but we do not keep any details of files on the database.

File Upload in CodeIgniter

In our previous lesson, we created a user registration with Name, Address, Email, etc. But that does not contain option to upload user image or any file to the database. Uploading image, or any files such as Photos, PDF, Docs, etc are common in almost forms.

Removing index.php from URL Using htaccess

How do we run a CodeIgniter project? It’s very easy. But there is text ‘index.php’ on your CodeIgniter running url.

What is Base URL? How to Config and Use

Whenever you want to add images, external css, JavaScript files into your project, you need to specify the location of the file. While developing a CodeIgniter project on your computer, all your project files are located on your computer had disc, that is local host. So you specifies your local computer file location for specifying a file path for the project. But while hosting your project at internet server, all such locations will not work. So you need to specify the internet server location.

User Login Validation with Email and Password

Here let us create a login form validation with user email address and password. In previous article, we created a registration form with user's details such as name, address, email id, password, etc. So this article is based on previous registration, that is, a user can login to his account with his email and password.