This is probably really simple, but I just started using Code Igniter.
I have the following code in a model and want to get the database query array to pass to the controller and then pass it to a page view.
public function view_user($id){
$this->db->where('userid', $id);
$query = $this->db->get('users');
return $query->result_array();
}
What is the code necessary to get the returned array in the controller?
Thanks in advance for your help!
here you Go with perfact answer
Model code
public function view_user($table,$id)
{
$this->db->where('userid', $id);
return $this->db->get($table);
}
Now in controller
Call your Method Like
$this->load->model('model_name');
$data['array_name']= $this->model_name->view_user('Tabelname',$id)->result();
If u want to Get Only single row Insted of result write ->row(); in above statement
now load your view and pass the $data as 2nd pararameter
$this->load->view('viewname',$data)
now in your VIEW
Acesss Array Like
<?php foreach($array_name as $row)?>
Related
I'm having a problem with laravel., I'm trying to send the variable $codes on my view :
$codes = Code::where('user', $id)->get();
return view('user.edit', ['user' => $user,'codes' => $codes]);
But I get that error Too few arguments to function e(), 0 passed in
The variable $user goes well but not the variable code, anyone have an idea for a solution?
Thank you all
use this:-
return view('user.edit')->with('codes',$codes);
you can get the user with :-
Auth::user()->name;
I'm pretty sure, if you are following Laravel naming conventions, in your eloquent query the column name is user_id and not user. Also if you are using such querys, and you have foreign, you can define relations in your models, like the following:
Code Model
public function user
{
$this->belongsTo(User::class);
}
User Model
public function codes
{
$this->hasMany(Code::class);
}
Instead of line
$codes = Code::where('user', $id)->get();
You can go with:
$codes = $user->codes();
If you don't have the foreigns in your database
$codes = Code::where('user_id', $id)->get();
If non of these helps, please dd() your variables before returning the view, and share result with me in comment. In laravel 7 how you return view is good.
I'm trying to make it so that you can view the number of rows of something on my website, I converted the result into an array and then foreached it in the view, yet it calls the argument invalid.
As I said, I've tried the usual, passing on the data from the model to the controller and then to the view. I tried returning $query->num_rows(); as well as a count_all_results back to the controller array for it to be used in the view, both are invalid.
controller.php
$this->load->model('Main');
$data = array(
'getapps' => $this->Main->countapps()
);
$this->load->view('header', $data);
view.php
<?php foreach($getapps as $apps) {
?>
apps <strong><?php echo $apps; ?></strong>
<?php }
?>
model.php
public function countapps() {
$this->db->where('usedby', $_SESSION['uid']);
$this->db->get_where('apps', 'appstatus', 'Used');
$stmt = $this->db->count_all_results();
return $stmt;
}
What I expect to happen is for the view to display the number of rows, instead it tells me that the foreach argument is invalidly supplied.
In CI $this->db->count_all_results() substitutes $this->db->get() in a database query. Therefore you cannot use $this->db->get_where() and $this->db->count_all_results() at the same time, use num_rows() instead.
write your model function like this:
public function countapps() {
$query=$this->db->where('usedby', $_SESSION['uid'])
->get_where('apps', 'appstatus', 'Used');
$stmt = $query->num_rows();
return $stmt;
}
you could change in your controller to a shorter:
$data['getapps'] => $this->Main->countapps();
Then in your view, you don't need to use foreach() as $data['getapps'] is not an array but a number. You can output it in the view simply like
echo $getapps;
I am stuck in getting a value from model and process it in controller. I am new to codeigniter. My model code is this:
public function display_cat_courses($value)
{
$this->db->select('*');
$this->db->from('courses_tbl');
$this->db->join('course_category_tbl', 'courses_tbl.course_category_id = course_category_tbl.id');
$this->db->where('course_category_id', $value);
$query = $this->db->get();
return $query->result();
}
And my controller is this:
public function view()
{
$this->load->model('Category_model');
$cat_id = $this->uri->segment(3);
$data['category']=$this->Category_model->display_cat_courses($cat_id);
}
I just want to get a column value from course_category_tbl and store that inside an array as array element to process that later.Model works fine but in my controller I want to store "course_type" which is a column in course_category_tbl. How to get it.
As you are succesfully getting all datas in array format from model in controller and you only want one field data from array that you need to use for some purpose.For that,you can make use of
$field_values_array = array_column($whole_array, 'field_name');
print_r($field_values_array );
From this code,you will get all values of that column field in array Format,and later you can pass it to view for further proccessing.
You can select required column only:
$this->db->select('course_category_tbl.course_type');
If there is only one row that you'll get by passing the category ID then in the model in place of $query = $this->db->get(); you can add put
$this->db->row_array() and this will give you a one dimensional array and then from the result $query you can access the desired elemnt by passign the index.
E.g
$query['course_type'] will give you course type
I'm trying to fetch certain values from and then pass it to another model in the same control.
However I'm only able to display the last row in the view.
I have shared my code below and I'm not sure where I'm going wrong.
Controller:
public function test($id){
$mapping_details = $this->queue_model->get_mapping_details($id);
foreach ($mapping_details as $value) {
$data['agent_details'] = array($this->agent_model->get_agent_details($value['user_id']));
}
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Model:
public function get_agent_details($id) {
$query = "select * from user_table where id = ".$id." and company_id = ".$this->session->userdata('user_comp_id');
$res = $this->db->query($query);
return $res->result_array();
}
Welcome to StackOverflow. The problem is the iteration in your controller. You are iterating through the $mapping_details results and per every iteration you are re-assigning the value to $data['agent_details'] , thus losing the last stored information. What you need to do is push to an array, like this:
foreach ($mapping_details as $value) {
$data['agent_details'][] = $this->agent_model->get_agent_details($value['user_id']);
}
However, wouldn't it be best if you created a query that uses JOIN to get the related information from the database? This will be a more efficient way of creating your query, and will stop you from iterating and calling that get_agent_details() over and over again. Think of speed. To do this, you would create a model method that looks something like this (this is just an example):
public function get_mapping_details_with_users($id){
$this->db->select('*');
$this->db->from('mapping_details_table as m');
$this->db->join('user_table as u', 'u.id=m.user_id');
$this->db->where('m.id', $id);
$this->db->where('u.company_id', $this->session->userdata('user_comp_id'));
return $this->db->get()->result();
}
Then your controller will only need to get that model result and send it to the view:
public function test($id){
$data['details_w_users'] = $this->queue_model->get_mapping_details_with_users($id);
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Hope this helps. :)
i have a page that shows all student profiles with their results. all the user info i putted in the table "user"
and then i have another table "results" that shows all the students their scores from diffrent courses. the thing is i don't know how to write the query or controller function to link the student with their corresponding results. I need some help here thanks
Controller
function students()
{
$data = array();
$this->load->model('kdg_model');
$query = $this->kdg_model->get_students();
$query2 = $this->kdg_model->get_resultStudent();
if ($query)
{
$data['user'] = $query;
$data['results'] = $query2;
}
$this->load->view('students_view',$data);
}
Model
get_students get all rows from the database table user.
get_resultstudent gets all the rows from results. tried to combine them but it just gives me all the same rows back on every profile.
function get_students(){
$this->db->where('admin', 0);
$query = $this->db->get('user');
return $query->result();
}
function get_resultStudent(){
$this->db->select('*');
$this->db->from('results');
$this->db->join('user', 'user.id_user = results.FK_student');
$query = $this->db->get();
return $query->result();
}
I'm not sure what your database schema looks like but from what I can work out I've got this. Okay so in your controller you want to build your array of students. Let do this by calling two model functions. The first model function we are going to call we get us all of the students in the 'user' table. The model will pass back an array to the controller which we can loop through to get the individual students. On each iteration of the loop we can pass the 'id_user' to another function in our model to get the results for that student.
//This is where we will store the students and their results
$aData['aStudentResults'] = array();
//Load the model we need
$this->load->model('kdg_model');
//Get an array of students
$aStudents = $this->kdg_model->get_students();
//Now we have an array of student id's let loop through these and for each student
//let get their results
foreach($aStudents as $student)
{
//Add the student to the array
//The student id (id_user) is the key
$aData['aStudentResults'][$student->id_user] = $this->kdg_model->get_resultStudent($student->id_user);
}
//Pass out array of students to the view
$this->load->view('students_view', $aData);
These are the model functions we are calling in the controller
//Get all the students that are not admins
function get_students(){
$this->select('id_user')
-from('user')
->where('admin', 0);
$query = $this->db->get();
return $query->result();
}
//Use the id_user we were passed by the controller to get the results
function get_resultStudent($id_user){
$this->db->select('*');
$this->db->from('results');
$this->db->where('FK_student', $id_user);
$query = $this->db->get();
return $query->result_array();
}
At this point we now have all the data we need. We just need to pass it from the controlle to the view which we did by passing the view $aData. In our view we can access what we passed to it like so
<? foreach($aStudentResults as $studentId => $aResults): ?>
<p>Student id: <?= $studentId ?></p>
<?foreach($aResults as $aResult): ?>
//Do something with results
<? endforeach; ?>
<? endforeach; ?>
This hasn't been tested so there may be some syntax errors but hopefully you should get an idea of what I am trying to do and what you need to do.
It's important for you to understand how MVC works. Practice selected data from your database in your models, and then passing that data to your views via the controller. You'll quickly get the hang of it.
If there is anything you don't understand in this answer please leave a comment and let me know.