Codeigniter : displaying query result in controller - codeigniter

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

Related

Codeigniter alphabetical menu

I want to create simple alphabetical menu, and things look like I'm close to what I want but I have to remove duplicate letters:
in model I use:
public function svi_izvodjaci_za_izbornik()
{
$this->db->order_by('naziv', 'ASC');
$query = $this->db->get('izvodjac');
return $query->result();
}
in controller I use:
public function index()
{
$data['izvodjaci_za_izbornik'] = $this->pocetak_model->svi_izvodjaci_za_izbornik();
$this->load->view('zaglavlje');
$this->load->view('pocetak', $data);
$this->load->view('podnozje');
}
in view I use:
<?php foreach ($izvodjaci_za_izbornik as $izvodjac_za_izbornik): ?>
<?php echo mb_substr($izvodjac_za_izbornik->naziv, 0, 1, 'utf-8'); ?>
<?php endforeach; ?>
and the result is:
Also I plan to pass that letter to controller method who will then call model method that looks something like this:
public function izvodjaci_abecedno($letter)
{
$this->db->like('naziv', $letter, 'after');
$query = $this->db->get('izvodjac');
return $query->result();
}
Thank you for help.
Remove duplicate alphabets like this.use $this->db->distinct();
In your Model
<?php
public function svi_izvodjaci_za_izbornik()
{
$this->db->distinct();
$this->db->select('naziv');//if you nedd to select more columns name seperate by comma like col1,col2,col3,..
$this->db->from('izvodjac');
$this->db->order_by('naziv', 'ASC');
$query = $this->db->get();
return $query->result();
}

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

Codeigniter dropdown only retrieve last data from db

I use CI form with form_dropdown helper and tried to pull Mysql data into its options, from the below code, its only retrieve the last record from db into the option list?
please advise what is wrong with my code?
Model
public function getStates() {
$query = $this->db->get('states');
$return = array();
if($query->num_rows() > 0){
$return[''] = 'please select';
foreach($query->result_array() as $row){
$return[$row['state_id']] = $row['state_name'];
}
}
return $return;
}
Controller
$this->load->model('db_model');
$data['options'] = $this->db_model->getStates();
$this->load->view('create_new', $data);
View
$state = array(
'name' => 'state',
'id' => 'state',
//'value' => set_value('state', $state)
);
<?php echo form_label('State', $state['id']); ?>
<?php echo form_dropdown($state['name'], $options); ?>
<?php echo form_error($state['name']); ?>
<?php echo isset($errors[$state['name']])?$errors[$state['name']]:''; ?>
send full query result from the model to the view like this
Model
public function getStates() {
$query = $this->db->get('states');
return $query->result();
}
let the controller as it is and now you can populate states in dropdown like this:
View
<?php
foreach($options as $opt){
$options[$opt->state_id]=$opt->state_name;
}
echo form_dropdown($state['name'], $options);
?>
Try this:
function getStates() {
$return = array();
$query = $this->db->get('states')->result_array();
if( is_array( $query ) && count( $query ) > 0 ){
$return[''] = 'please select';
foreach($query as $row){
$return[$row['state_id']] = $row['state_name'];
}
}
return $return;
}

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

Resources