Calling a model function in view - codeigniter

i am trying to access a function in model, in my view in codeigniter and its not working.
can somebody please tell me where is the problem.
MODEL:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
VIEW:
<select id="special" >
<?php echo $this->dashboard_ext_model->gettemplates(); ?>
</select>

Change this:
echo "<pre>"; print_r($result);exit;
To this:
echo "<pre>"; print_r($result); echo "</pre>";
Basically remove the exit. Exit aborts the script.
You should never have a good reason to call the model from the view. Model data should be passed to the controller, to then pass to the view.

Since this is MVC (Model View Controller) you shouldn't call a model from a view.
You should be calling the Model inside the controller then passing to the view as an argument.
Model:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
Controller:
function gettemplates(){
//$this->dashboard_ext_model = $this->load->model('dashboard_ext_model');
//uncomment this if you didn't load the model
$data['templates'] = $this->dashboard_ext_model->gettemplates();
$this->view->load('page_name', $data);
}
View:
<select id="special" >
<?php echo $templates ?>
</select>
MVC may look stupid at first but it really helps in large size projects.
MVC DESIGN: http://i.stack.imgur.com/Beh3a.png

Related

Order of the views in Query database and Views in codeigniter

The order of the views change wen i make a call to a database. In this case i make a Formulari.php that is a Controller.
Formulari.php
public function resum(){
**$this->load->view('header');**
$query = $this->db->query("SELECT * FROM tarifes");
# code...
foreach ($query->result_array() as $row) {
echo $row['operador'];
echo $row['minutatge'];
echo $row['permanencia'];
echo $row['dades'];
echo $row['preu'];
echo '</br>';
}
$this->load->view('resum_taula');
$this->load->view('footer');
}
When I see this controller the first i can see is the table that returns me. But que first view i want to see is the title.
Thanks a lot!
Controller
$data['formular_data'] = $this->your_model->getData();
$this->load->view('resum_taula', $data);
$this->load->view('footer');
Model
function getData() {
$query = $this->db->get('tarifes');
return $query->result_array();
}
View
<?php print_r($formular_data'); ?> // form $data['formular_data'];

Codeigniter model pass value controller to view

Table products: id, product, lining, ...
Table tbl_lining: id, article, description
Model
public function getliningsale($id)
{
$this->db->select('*');
$this->db->from('products');
$this->db->join('tbl_lining', 'products.lining=tbl_lining.id', 'left');
$this->db->where('products.id', $id); // Also mention table name here
$query = $this->db->get();
if($query->num_rows() > 0)
return $data->result();
}
Controller
function liningsale($id)
{
$data['liningsale'] = $this->sales_model->getliningsale($id);
$this->load->view('add', $data);
}
View
echo '<td><input class="span1 tran2" name="lining\'+ count +\'" type="text"';
echo 'value="';
foreach ($liningsale as $valor) {
echo $valor->article;
echo '-';
echo $valor->description;
echo '">';
}
echo '</td>';
This doesn't display any record.
I've tried several ways without success.
Can anyone help?
In your model, you need to return the $query result.
At the minute you are trying to return the result set of a variable named $data which I presume doesn't exist!!
Try changing return $data->result(); to return $query->result();
Hope that helps!!

accessing result array in codeigniter

I have the following happening in my model:
public function load_user_menu($username)
{
$this->db->select('*');
$this->db->from('menu');
$this->db->where('username', $username);
$query = $this->db->get();
return $query->result();
}
and the following in my controller:
public function index()
{
/*If user previously logged in and not logged out, username remains in session.
load username and load profile data.
*/
//check if user logged in or not
if(($this->session->userdata('username')!=""))
{
//load data from model
$profile = array();
$username = $this->session->userdata('username');
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
$this->load->view('profile', $profile);
}else{
//redirect to login function
$this->login();
}
}
but I am getting errors on profile view. I am sure I am accessing them wrong because I am doing this on view:
<? echo $userdetails['profilepic']; ?>
and nothing is showing but this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profile.php
Line Number: 60
which am certain because of the wrong accessing. How can I access the details based on the above?
assumming you have profilepic inside userdetails and seeing the error you are trying to get he property of nonobject that means
<? echo $userdetails['profilepic']; ?>
should be
<? echo $userdetails->profilepic; ?>
since you are taking you result as object and not array
return $query->result();
or you change this to
return $query->result_array();
$query->result(); produces an object; $query->result_array(); gives you an array
either is fine, depending on your needs
-There is no need of foreach loop in your controller as
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
-Try this instead.
$profile['userdetails'] = $this->profileModel->user_profile($username);
-To Access in View,use this as
echo $userdetails->profilepic;
just try this on your view page
<? echo $profilepic; ?>

Codeigniter : displaying query result in controller

I am trying to display my db query result in my controller, but I don't know how to do it. could you please show me?
Controller
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
// I want to display the the query result here
// like this: echo $row ['full_name'];
}
My Model
function profile($id)
{
$this->db->select('*');
$this->db->from('names');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{ return $query->row_array();
}
else {return NULL;}
}
echo '<pre>';
print_r($data['records']);
or
echo $data['records'][0]['fullname'];
Model:
function profile($id){
return $this->db->
select('*')->
from('names')->
where('id', $id)->
get()->row_array();
}
Controller:
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
print_r($data['records']); //All
echo $data['records']['full_name']; // Field name full_name
}
You do that inside a View, like this.
Controller:
function get_name($id){
$this->load->model('mod_names');
$data['records']=$this->mod_names->profile($id);
$this->load->view('mod_names_view', $data); // load the view with the $data variable
}
View (mod_names_view):
<?php foreach($records->result() as $record): ?>
<?php echo $record->full_name); ?>
<?php endforeach; ?>
I would modify your model then to something like this (it worked for me):
function profile($id)
{
$this->db->select('*');
$this->db->from('names');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query; // just return $query
}
}

How to pass multiple variable data from model to view

Could you please tell me how my model,controller and view should look like if I want to pass the following variable data($amount1, $amount2, $amount3) to my view file via controller from my model.
case 1: $amount1=100;
case 2: $amount2=500;
case 3: $amount3=1000;
I want to have the variables in a way that I don't have to echo them in any { } example:
foreach ($records as $row){ $i++; ?>
// I don't want to echo those inside in this.
// I want to echo it like this way- <? echo $amount1;?>
}
Thanks in Advance :)
If you pass an array of data from your controller to your view you can access each element as a variable in the view. Here is an example of what I mean:
model:
class Model extends CI_Model
{
public function get_data()
{
$data = array(
'amount1' => 100,
'amount2' => 500,
'amount3' => 1000,
);
return $data;
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
// get data from model
$data = $this->model->get_data();
// load view
$this->load->view('view', $data);
}
}
view:
<h1><?php echo $amount1; ?></h2>
<p><?php echo $amount2; ?></p>
<!-- etc... -->
I have found a solution myself. I just wanted to share so that it can help others. So here it is..
Your model should look like following :
function net_income(){
$data['amount1']=50;
$data['amount2']=100;
return json_encode($data);
}
Your controller:
function add(){
$this->load->model('mod_net_income');
$json = $this->mod_net_income->net_income();
$obj = json_decode($json);
$data['amount1']= $obj->{'amount1'};
$this->load->view('your_viewfile_name',$data);
}
And then in your view file: just
<? echo "$amount" ; ?>
Thanks :)

Resources