How to show retrieve data from database into text field in codeigniter - php

it's my model code:
<?php
class Books_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_restaurants()
{
$sql = "SELECT id, names FROM restaurants ";
$query = $this->db->query( $sql );
return $query->result();
}
}
controller code:
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$this->user_data['result']=$this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$this->user_data);
}
}
What code I written in view file that the data show in text field?

Try the following :
In the Controller
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data['results'] = $this->Books_model->get_restaurants();
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);
}
}
In the View:
<?php
foreach ($results as $result)
{?>
<label>Restaurant Name : </label>
<input type="text" value="<?php echo $result->names;?>" />
<?php } ?>

in the controller
<?php
class Booking_Controller extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('Books_model');
}
public function view()
{
$data["result"]=$this->Books_model->get_restaurants();
//$this->load->helper(array('form','url')); not needed
$this->load->view('restaurants/booking',$data["result"]);
}
}
now in your view
<?php
// notice that CI strip the key "result" from the array $data to become a variable $result in the view
foreach ($result as $row)
{
echo $row->id."<br>";
echo $row->name."<br>";
echo "----";
}
?>
Note:
there is no member like this "$this->user_data['result']" in
codeigniter but there is "$this->session->user_data("data_name")" if
you want to store some data in the session, but then, no need to pass
it to the view as an argument you can call the session data from the
view directly

Related

How to pass result from Model to Controller in CodeIgniter?

I want to get data from the database and display it on a webpage using CodeIgniter. I coded my controller, model and view as follows.
Controller;
//HomeController
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model;
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
return $query->unbuffered_row('object');
}
}
?>
View;
//HomeView
<?php
echo "Recoeds from database<br>";
while($records)
{
echo $records->name." ".$records->age."</br>";
}
?>
But this code doesn't print anything on the screen.(echo "Records from database<br>"; )
So I tried the following code given in the CodeIgniter documentation and echoed the result in the model itself rather than return it to the controller and then to the views.It worked fine.
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
while ($row = $query->unbuffered_row())
{
echo $row->name;
echo $row->age;
}
}
}
?>
My question is how do we return the result of the unbuffered_row() method into the controller and then to the view as per MVC architecture? We can get the output by echoing result at the model itself but it is against the purpose of the MVC architecture.
You should use return $query->result(); and then get the right object in the view or controller (that's up to you).
Try This One
controller
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$data = $this->db->query('SELECT * FROM data');
return array('count'=>$data->num_rows(), 'data'=>$data->result(),'first'=>$data->row());
}
}
?>
View
<?php
echo "Recoeds from database<br>";
foreach($record['data'] as $row)
{
echo $row->name." ".$row->age;
}
?>
I simulated your code in local.Your mistake is here.$records is object .And your while loop has not condition for leaving the loop
<pre>
<?php
var_dump($records);
echo "Recoeds from database<br>";
foreach($records as $value) {
echo $value->name."<br>";
echo $value->age."<br>";
}
?>
</pre>

Echoing in Model View Controller

I am new to MVC and I need to echo data from my database in View. Can anyone help me with that? Here is the code:
Controller:
class Index extends Controller {
function __construct() {
parent::__construct();
//echo 'We are in index';
}
function index(){
$this->view->render('index/index');
}
function get(){
$this->model->get();
}
}
Model:
class Index_Model extends Model {
public function __construct()
{
parent::__construct();
}
function get()
{
$sth = $this->db->prepare('SELECT * FROM data');
$sth->setFetchMode(PDO::);
$sth->execute();
$sth->fetchAll();
}
}
How to echo in View?

Getting a drop down list from a database in CodeIgniter

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}

How to call Base Controllers Method in Codeigniter?

I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}

load menu on the header file using codeigniter

I'm new to Codeigniter and i have been trying to develop some part using it.
On my header file, i need to load my menu items and i have create a menu controller, menu model and a view.
Controller page
<?php
class Menu extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('menu_model');
}
public function index(){
$data['menuArray'] = $this->mainMenuDataLoad();
if($data['menuArray']){
$this->load->view('menu' , $data);
}
}
public function mainMenuDataLoad(){ /* create menus Array */
$rootMenuData = $this->menu_model->loadManuData();
if($rootMenuData){
for($e=0; $e<count($rootMenuData); $e++){
if($rootMenuData[$e]){
$data[$e] = array(
'title' => $rootMenuData[$e]['title'],
'menu_id' => $rootMenuData[$e]['menu_id'],
'url' => $rootMenuData[$e]['url'],
'menu_icon' => $rootMenuData[$e]['menu_icon'],
);
$get_sub = $this->mainMenuDataLoad($rootMenuData[$e]['menu_id']);
if($get_sub){
$data[$e]['sub'] = $get_sub;
}
}
}
return $data;
}
return false;
} }
this is my model page
class Menu_model extends CI_Model{
public function loadManuData(){
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$r=0;
foreach ($query->result() as $row) {
$data[$r]['root_id'] = $row->root_id;
$data[$r]['menu_id'] = $row->menu_id;
$data[$r]['title'] = $row->title;
$data[$r]['url'] = $row->url;
$data[$r]['menu_icon'] = $row->menu_icon;
$r++;
}
return $data;
}
return false;
}
public function __construct(){
parent::__construct();
}}
andon my menu view page i am looping the menu data.
But on my header.php if i try to call the menu controller like this
$this->load->controller('menu');
it gives me an error like this.
Fatal error: Call to undefined method CI_Loader::controller() on header.php
What am i doing wrong?.
Someone please guide me.
thanks in advance
menu.php view Page
<ul class="nav navbar-nav">
<?php
print_r($menuArray);
for($q=1; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
?>
</ul>
you cant call a controller from view
i.e. in view page writing this code $this->load->controller('menu'); is not permissible.
The controller loads the view and model, its the controller that is the prime here
[More Edit:]
change your model to this
class Menu_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function get_menu()
{
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
return $query;
}
}?>
then in the controller do this
public function index(){
$data['menuArray'] = $this->menu_model->mainMenuDataLoad()->result_array();
$this->load->view('header', $data);
$this->load->view('menu'); // u are passing data from here//
$this->load->view('landing_page');
$this->load->view('footer');
}
and finally the view
<?php
if(count($menuArray)>0)
{
for($q=0; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
}?>
You also dont need the public function mainMenuDataLoad() function in controller
Better solution if you make a BaseController with a function and call it from extended controllers.
BaseController:
class BaseController extends CI_Controller
{
protected $data = array();
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
protected function LoadContView($aContentView) {
$this->data['menu'] = $this->my_model->getMenu();
$this->load->view('common/ViHeader', $this->data);
$this->load->view($aContentView, $this->data);
$this->load->view('common/ViSidebar', $this->data);
$this->load->view('common/ViFooter', $this->data);
}
Mypage:
class Mypage extends BaseController{
function __construct() {
parent::__construct();
}
public function index() {
$this->LoadContView('my_view');
}
}
also use foreach( $menu_array as $menu_item) :)

Categories