retrieve data from database in for loop - codeigniter

my controller is
function shiftstudentdetailsmultiple(){
$data['Show'] = $this->input->post('show[]');
$roll = $this->input->post('roll[]');
//echo sizeof($data1);
for($i=0;$i<sizeof($roll);$i++)
{
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$this->load->view('registrar/shift_student_view', $data);
}
and model is
function getstudentsdetailscouseandmultiple($Uniq_Id){
$this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
$this->db->where("sprd.Uniq_Id",$Uniq_Id);
$this->db->from("Students_Prim_Detals sprd");
$this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
$this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r
$data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
the data showing correct .. when load only showing one data..the last entry..
$this->load->view('registrar/shift_student_view', $data);

Each time you call...
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.
You will need to revise your controller along these lines.
$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{
$student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$data['results'] = $student_detail;
Then in your view you will have to loop through $results to display each item in the array.
foreach($results as $detail){
echo "Name: ". $detail->name;
echo "Course: ". $detail->Course_Name;
//etc...
}

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

Explain the functionality or meaning of this : $data['row']

I have this doubt.
// This is my controller
public function profile()
{
$session_data = $this->session->userdata('logged_in');
$uid= $session_data['id'];
$pic = $this->user_model->getImage($uid);
$data['profile_pic']= $pic ;
$data['row'] = $this->user_model->get_user_data($uid);
$test = $this->load->view('profile_view',$data,TRUE);
echo $test;exit;
}
Based on name it seems to be:
public function profile()
{
$session_data = $this->session->userdata('logged_in');//checking user login or not
$uid= $session_data['id'];//getting id from session
$pic = $this->user_model->getImage($uid);//geting user image
$data['profile_pic']= $pic ;//storing image to data array
$data['row'] = $this->user_model->get_user_data($uid);//passing $uid to model and store in data array what row model returns
$test = $this->load->view('profile_view',$data,TRUE);//passing data array to view
echo $test;exit;
}
//model
i am passing user id only
public function get_user_data($uid){
//echo $uid;exit;
$data = array();
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$uid);
$query = $this->db->get();//checking that $uid data in database
if($query->num_rows() > 0){
$get_user_data=$query->result();
//print_r($get_user_data);exit;
//print_r($items);exit;
return $get_user_data; // if find, return data to controller
}
else{
return FALSE;
}
//return $query->row();
}

Call to a member function row() on a non-object

i am getting error Call to a member function row() on a non-object in codeigniter my controller is
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
my model is
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
here loadContent() is just load the content with view path
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
in my model I am getting the result as an array of object in $query and then it is returned as $result like this -
$query = $this->db->get()->result(); but at the controller $survey stores array of object and i want to show the content of that array of object ,previously I use
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
to get that data but the problem is $survey->row() cannot return that data bcoz it is not an object it is array of object so it can't be returned through row() method
so instead of this I just call the first element of that data like this-
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
Somehow its works for me bcoz I want to show the first row of the data
if sembody wants to show all data then I think he shuld try logic to increment the key value of that array of object for me it is $survey[] you can use foreach loop for increment the of value of the key element
The problems i see are your model, I will dissect it and add comments to your original code to point out the issues:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
after applying all the changes:
MODEL:
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
CONTROLLER
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
I think when you use $this->db->get(), you need to pass the table name as param like this:
$this->db->get('table_name')->result();

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

CI pagination, POST method not working

Okay, I am pretty new in CI and I am stuck on pagination. I am performing this pagination on a record set that is result of a query. Now everything seems to be working fine. But there's some problem probably with the link. I am displaying 10 results per page. Now if the results are less than 10 then it's fine. Or If I pull up the entire records in the table it works fine. But in case the result is more than 10 rows, then the first 10 is perfectly displayed, and when I click on the pagination link to get to the next page the next page displays the rest of the results from the query as well as, other records in the table. ??? I am confused.. Any help??
Here's the model code I am using ....
function getTeesLike($field,$param)
{
$this->db->like($field,$param);
$this->db->limit(10, $this->uri->segment(3));
$query=$this->db->get('shirt');
if($query->num_rows()>0){
return $query->result_array();
}
}
function getNumTeesfromQ($field,$param)
{
$this->db->like($field,$param);
$query=$this->db->get('shirt');
return $query->num_rows();
}
And here's the controller code ....
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$config['base_url']='http://localhost/cit/index.php/tees/show/';
$config['total_rows']=$this->T->getNumTeesfromQ('Title',$KW);
$config['per_page']='10';
$this->pagination->initialize($config);
$data['tees']=$this->T->getTeesLike('Title',$KW);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$data['links']=$this->pagination->create_links();
$this->load->view('tee_res', $data);
What am I doing wrong here ???? Pls help ...
I guess the problem is with the $KW=$this->input->post('searchstr'); ..
Because if I hard code a value for $KW it works fine. May be I should use POST differently ..but how do I pass the value from the form without POSTING it , its CI so not GET ... ??????
In Controller
<?php
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$count = $this->model_name->getNumTeesfromQ($KW);//Count
$config['base_url']= base_url().'index.php/tees/show/';
$config['total_rows']= $count;
$config['per_page']='10';
$config['uri_segment'] = 4;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['links'] = $this->pagination->create_links();
$data['tees']=$this->model_name->getTeesLike($KW,$limit,$page);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$this->load->view('tee_res', $data);
In Model
public function getNumTeesfromQ($KW)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW'");
$result = $query->result_array();
$count = count($result);
return $count;
}
public function getTeesLike($KW,$limit,$page)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
echo all data using foreach
then at bottom of page use <?php echo $links; ?>this will show your Pagination

Resources