My dear friends today I am going to do the second part of the CURD.That is retrieve the data from a database..
I uses the database and the table in lesson 5.So refer it.
To do this I need Model View and a Controller.
Let us go.
I will create the controller first.
Open a new file and save it as mycontroller.php in the C:\xampp\htdocs\CodeIgniter\application\controllers directory.
Here is the code you need.
<?php
class mycontroller extends CI_Controller{
public function show(){
$this->load->model('mymodel'); //I am loading the method
$data['info']=$this->mymodel->get(); //I am calling the function called get() and assign it's return in to array
$this->load->view('myview',$data); //Load the view ana pass the values.
}
}
?>
Now I need the model to retrive data from the database table.
[Refer the previous two lesson.At there I create a database and a table].
Here is my model and save it in C:\xampp\htdocs\CodeIgniter\application\models diectory
<?php
class mymodel extends CI_Controller{
public function get(){
$all=$this->db->query("select * from info"); // my database quary.
return $all->result();
}
}
?>
Now I need a view to show the retrive data.
Here is my view and save it in C:\xampp\htdocs\CodeIgniter\application\views directory.
<?php
foreach($info as $fact){
echo $fact->ID;
echo $fact->Name;
echo "</br>";
}
?>
Now put below codes in your address bar of web browser and see the result.
That is it!!!!!!!!!!!!!!!!!!
-Ujitha Manujapriya Sudasingha-
I uses the database and the table in lesson 5.So refer it.
To do this I need Model View and a Controller.
Let us go.
I will create the controller first.
Open a new file and save it as mycontroller.php in the C:\xampp\htdocs\CodeIgniter\application\controllers directory.
Here is the code you need.
<?php
class mycontroller extends CI_Controller{
public function show(){
$this->load->model('mymodel'); //I am loading the method
$data['info']=$this->mymodel->get(); //I am calling the function called get() and assign it's return in to array
$this->load->view('myview',$data); //Load the view ana pass the values.
}
}
?>
Now I need the model to retrive data from the database table.
[Refer the previous two lesson.At there I create a database and a table].
Here is my model and save it in C:\xampp\htdocs\CodeIgniter\application\models diectory
<?php
class mymodel extends CI_Controller{
public function get(){
$all=$this->db->query("select * from info"); // my database quary.
return $all->result();
}
}
?>
Now I need a view to show the retrive data.
Here is my view and save it in C:\xampp\htdocs\CodeIgniter\application\views directory.
<?php
foreach($info as $fact){
echo $fact->ID;
echo $fact->Name;
echo "</br>";
}
?>
Now put below codes in your address bar of web browser and see the result.
That is it!!!!!!!!!!!!!!!!!!
-Ujitha Manujapriya Sudasingha-