Cannot get data after joining from multiple tables in Codeigniter - php

I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}

Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.

Related

Codeigniter: How to get data from id using two table by passing an id

I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.
How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?
Model
function get_by_id($id)
{
$this->db->from('student');
$this->db->where('p_id',$id);
$query = $this->db->get();
return $query->row();
}
Controller
public function ajax_edit($id)
{
$data = $this->Model_Action->get_by_id($id);
echo json_encode($data);
}
Hi I think you are looking for this. I use a sample from your code:
function get_by_id($id)
{
$this->db->from('student');
$this->db->join('table_b', 'student.p_id=table_b.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
Actually you can find more here
function get_by_id($id)
{
$this->db->select('*')
$this->db->from('student');
$this->db->join('parent','student.p_id=parent.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}

How to make a join and search in data using codeigniter and Php

I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();

CodeIgniter COUNT with active record?

I am working in codeigniter.. I want to count the no. of rows having the same order_no.. below is my code.
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
$this->db->select('*');
$this->db->from('order_details');
$this->db->where('email', $email);
$this->db->group_by('order_no');
$this->db->order_by('order_no', 'DESC');
$query = $this->db->get();
return $query->result();
}
Pls help..
You can use $this->db->count_all_results()
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
//$this->db->select('*');// no need select as you only want counts
$this->db->from('order_details');
$this->db->where('email', $email);
//$this->db->group_by('order_no');//no need group_by as you only want counts
//$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts
return $this->db->count_all_results();;
}
Try changing your select line to look like this:
$this->db->select('COUNT(*) as count');
Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:
$this->db->select('*, COUNT(*) as count');
Feel free to change the lowercase name for count, I'm just using that as an example.
Check Codeigniter Manual
$query = $this->db->get();
return $query->num_rows(); //Return total no. of rows here
$query->num_rows(); will count the total active record return by your query.

Can't link user to their corresponding results CODEIGNITER

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.

CodeIgniter getting data from database

In my CodeIgniter project I'm getting the list of projects and successfully output them on a page. However, the data in one of the columns on that page should be retrieved from a different table in DB using the project ID. Could anybody help me to figure out how that can be done? Basically I need to make another query to that other table specifying the project id but don't actually know how to do that with CodeIgniter.
UPDATE
In the model I'm getting the list of projects with the following function:
function get_projects_list($page, $limit){
$sql = sprintf("SELECT * FROM Project WHERE deleted != 1 LIMIT %d, %d", ($page-1)*$limit, $limit);
$query = $this->db->query($sql);
return $query->result();
}
And in the controller I call the following function:
$projects_list = $this->Project_management_model->get_projects_list($curPage, self::$LIMIT_PER_PAGE);
$data['projects_list'] = $projects_list;
$data['cur_page'] = $curPage;
$data['page_count'] = $pageCount;
$this->load->view('project_management_view', $data);
And in the view I simply run on the $data with foreach and list the results in a table. In that table there's a column where I need to show a result from another table based on the ID of the project of that very row.
Thanks for helping.
You didn't mention whether you are using ActiveRecord or not. I am assuming that you are. I'll also guess that maybe what you need to do is use a JOIN.
If you were using straight SQL, you would do this using some SQL that might look something like this:
SELECT a.appointment_time, u.user_real_name FROM appointment a, site_user u WHERE u.site_user_id = a.user_id;
That would pull the user's name from the user table based on the user id in the appointment table and put it with the appointment time in the query results.
Using ActiveRecord, you would do something like this:
$this->db->select('appointment_time,user_real_name')->from('appointment')->join('site_user', 'site_user_id=appointment_user_id');
But why don't you tell us a little bit more about your question. Specifically, do you want this column in the other table to be related to the rows from the first table? If so, my JOIN suggestion is what you need.
I've actually found a way to do that with a custom helper. Creating a new helper and loading it in the controller gives an option to use the function from that helper in the view.
Thanks.
public function get data()
{
$this->db->flush_cache();
$query = $this->db->get('project_table');
$result = $query->result();
$data = array();
for ($i = 0;$i < count($result);$i++)
{
$data[$i] = $result[$i];
$data[$i]['project_data'] = temp($result[$i]->id);
}
return data;
}
private function temp($id = 0)
{
$this->db->flush_cache();
$this->where('id',$id);
$query = $this->db->get('project_table2');
$result = $query->result();
if (count($result) != 0)
return $result[0]->data;
}
you can do it by some thing like that,or you can use sub-query by query function of database.

Categories