CodeIgniter: Choosing a Data From GET

CodeIgniter Step By Step Tutorial: At previous tutorial, we learn show a data. But we have define ID that we want to show. In this post, we learn how to show a data that ID defined from GET.

For model: Open “books_model.php” within CodeIgniter\system\application\models. Add following function:

<?php
	class Books_model extends Model{
	
	function Books_model(){

	parent::Model();

	}


	function books_get($id){

	$this->load->database();

	$query =$this->db->getwhere('books',array('id'=>$id));
	return $query->row_array();
	}

}
?>

Continue reading →

CodeIgniter: Showing Simple One Data

CodeIgniter Step By Step Tutorial: After show all data, We learn how to show one data. We want a data with certain ID, example ID = 2. We still use same files with previous practice just add a new function books_get in the model
and remove previous function..

For model: Create “books_model.php” within CodeIgniter\system\application\models. Add following code:

<?php
	class Books_model extends Model{
	
	function Books_model(){

	parent::Model();

	}


	function books_get(){

	$this->load->database();

	$query =$this->db->getwhere('books',array('id'=>2));
	return $query->row_array();
	}

}
?>

Continue reading →

CodeIgniter: Showing Simple All Data

CodeIgniter Step By Step Tutorial: We have set database and create table for CodeIgniter practice. Now, we learn how to show data in CodeIgniter. As we know, CodeIgniter use MVC pattern. We will use Model for retrieve data from database.
First, build a model. Create a file named “books_model.php” within CodeIgniter/CodeIgniter\system\application\models. Enter following code:

<?php
	class Books_model extends Model{
	
	function Books_model(){

	parent::Model();

	}

	function books_getall(){

	$this->load->database();

	$query = $this->db->get('books');

	return $query->result();
	}

}
?>

Continue reading →

CodeIgniter: Preparing Database

CodeIgniter Step By Step Tutorial: After set database configuration,we will learn about showing data from database in CodeIgniter. But, before that, we prepare a database for practice. This post create a database named “ci” and a table named “books”. We use phpMyAdmin for easy.
1. Open your phpmyadmin.
2. Enter database name “ci” in create new database field.
3. Click Create button. Your database will be created.
create database
Database create successfullycreate database successfully

4. Create new table by entering name of new table at create new table field.
5. Enter number of fields.
6. Click Go button.
codeigniter - table creating use phpmyadmin
7. Enter name of field, type, and length
enter field name
8. Choose auto increment and check primary key for primary key field. and Click Save button.
set pripary key and click save
Table create successfully
table create successfully
9. Now, we insert data. Click Insert tab.
click insert tab
10. Enter data.
11. Click Go for saving.
insert dummy data
Continue reading →

CodeIgniter: Setting Database Configuration

CodeIgniter Step By Step Tutorial: Previous post, we have build simple first application. That application do not use database. In this post, we try to set database for CodeIgniter.

please, open database.php within CodeIgniter\system\application\config. Set config like below
database configuration

Make sure, it match with your database.