Order of the views in Query database and Views in codeigniter - 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'];

Related

Foreach row array CodeIgniter

I have created a model which joins 2 tables
function list_get($id){
$this->load->database();
$query = $this->db-> select('*')
->from('lists')
->join('list_items', 'list_items.items_list_id = lists.list_id')
->where('items_list_id', $id);
return $query->get()->row_array();
}
In my controller I created the view_list function
public function view_list($id = !FALSE){
$this->load->model('model_lists');
$data["query"] = $this->model_lists->list_get($id);
$this->load->model('model_lists');
if($data["query"]){
$this->load->view('lists/view_list',$data);
} else {
redirect('lists');
}
}
Now my view returns only the first result from the database
<?php echo $query['item_url'];?> How can I create a foreach to show all the results?
change this line return $query->get()->row_array();
to return $query->get()->result_array();
in view
foreach($query as $querys){
echo $querys['item_url'];
echo '</br>';
}
Change this return $query->get()->row_array(); to this
return $query->result_array();
and in view
foreach($query as $row)
{
echo $row['column_name'];
}

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!!

Calling a model function in view

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

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 :)

Codeigniter num_row returns "array" instead of number

Alright, Im trying to count all the rows where "Membership_Status" = Active. The result I get right now is "Array" instead of a number.
Here is my model
class Report_model extends Model
{
function count_members()
{
$query = $this->db->get_where('Membership', array('Membership_Status' => 'Active'));
return $query->num_rows();
}
}
Here is my controller
class Report extends Controller {
function YTD_report()
{
$data['main_content'] = 'report_membership_view';
$this->load->view('includes/template', $data);
}
}
Here is my view
report_model->count_members();
echo $total;
?>
My result is Array, where according to the db info, it should be 4.
What can I do/change to get it to display the proper number?
thanks
the $data array your passing to the view will create one variable for each key to be used by view...
So your controler once the model is loaded, you should do:
$data['total'] = $this->Report_model->count_members();
Then in the view you can use the $total variable like this:
<?php echo $total; ?>

Resources