Codeigniter model pass value controller to view - codeigniter

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

Related

I couldnt get the joined table value in my query

Workflow type table
[Workflow caterory table][2]
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
$this->db->from('workflow_category');
$this->db->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() AS $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
here im not able to print the $row["type_name"] and also showing a notice as undefined index for $row["type_name"].
Try like this...
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat,workflow_type.type_name')
->from('workflow_category')
->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
Error due to this line..
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
So it should be just
$this->db->select('workflow_category.wf_cat,workflow_type.type_name');
In above query i have been used query grouping.

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'];
}

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 how to show data in functions

My model:
function get_data($id)
{
$this->db->select('id, Company, JobTitle');
$this->db->from('client_list');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}
I want to get the the data from get_data(), is this the right way?
public function show_data( $id )
{
$data = $this->get_data($id);
echo '<tr>';
echo '<td>'.$data['Company'].'</td>';
echo '<td>'.$data['JobTitle'].'</td>';
echo '<td>'.$data['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
Use the row_array() function to get the data in the array format.
Reference url
http://ellislab.com/codeigniter/user-guide/database/results.html
you can use foreach loop to print
foreach ($data->result() as $row)
{
echo '<tr>';
echo '<td>'.$row['Company'].'</td>';
echo '<td>'.$row['JobTitle'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
thank you
just to improve answer, I use "general_model" for all my controllers, there are some exceptions where I need special queries, I just create desired model so "general_model" stays same and I can use it in any project.
e.g.
general_model.php
function _getWhere($table = 'table', $select = 'id, name', $where = array()) {
$this->db->select($select);
$q = $this->db->get_where('`'.$table.'`', $where);
return ($q->num_rows() > 0) ? $q->result() : FALSE;
}
.
.
.
//bunch of another functions
in controller I just call
$this->data['books'] = $this->general_model->_getWhere('book', '*', array('active' => '1'));
$this->render('book_list_view'); // $this->load->view('book_list_view', $this->data);
sidenote: I am extending CI_Controller therefore I use $this->data['books'] instead $data['books'] to pass data into view
in view
//check if there are any data
if ($books === FALSE) {
//some error that there are no books yet
} else {
//load data to table or something
foreach ($books as $book) {
$book->id; // book id
}
}

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

Resources