Codeigniter: query returns values of NULL - php

Hi there I am trying to check if my query is successful but it is returning indexes of NULL values making it seem that the query is successful. How can I check if the query is success in my sample query because I think it shouldn't return NULL indexes.
Model
public function classmanage_classinfo($section_id) {
$query = $this->db->select('students.user_id, students.studentnumber,
students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
->join('student_section', 'student_section.section_id = sections.section_id', 'left')
->join('students', 'students.user_id = student_section.student_id', 'left')
->where('sections.section_id', $section_id)
->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Controller
$data['class_info'] = $this->staff_model->classmanage_classinfo($section_id);
View
<p><?php var_dump($class_info) ?></p>
According to my var_dump the array is counted as a row even though the indexes are null? Why is this happening. How could I make sure that if the query isn't successful it triggers false in my if statement. If there are ways to improve or solve this logic it would be most welcome. Thanks.
Edit:
Full controller
$data['class_info'] = $this->staff_model->classmanage_classinfo($section_id);//determinant if query is null is in the model
$data['section_info'] = $this->staff_model->classmanage_sectioninfo($section_id);
Full Model
public function classmanage_classinfo($section_id) {
$query = $this->db->select('students.user_id, students.studentnumber,
students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
->join('student_section', 'student_section.section_id = sections.section_id', 'left')
->join('students', 'students.user_id = student_section.student_id', 'left')
->where('sections.section_id', $section_id)
->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
public function classmanage_sectioninfo($section_id) {
//section_name
$query = $this->db->select('section_name')->where('section_id', $section_id)->get('sections');
return $query->row();
}

I assume that your query returns many rows not a single row.
$query = $this->db->select('students.user_id, students.studentnumber,
students.lastname, students.firstname, students.middlename, students.level, students.year')->from('sections')
->join('student_section', 'student_section.section_id = sections.section_id', 'left')
->join('students', 'students.user_id = student_section.student_id', 'left')
->where('sections.section_id', $section_id)
->get()->result();
if (!empty($query)) {
return $query->result();
} else {
return false;
}

Related

MySQL get_where with If condition in Codeigniter

I have the following function in my model to filter officer records user wise. The function is working fine.
public function getOfficer()
{
$q = $this->db->order_by('last_name','ASC')->get_where('tbl_officer', array('status' => '1', 'usr' =>$this->session->userdata('id_user')));
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}
Further, I want to filter officer records only for the user = 4 in the session, by p_code->8,10 and 24
The If functions as follows :
if($usr == 4)
{
$this->datatables->where('tbl_officer.p_code', 8)->or_where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
} else {
$this->datatables->where('tbl_officer.usr', $usr);
}
How can I include this If condition into my model function mentioned above ? What can be changed ? Can anyone help ?
PLease use where_in
$this->datatables->where('tbl_officer.p_code', 8)->or_where('tbl_officer.p_code', 10)->or_where('tbl_officer.p_code', 24);
to
$this->datatables->where_in('tbl_officer.p_code', [8,10,24]);
Just need to change the method of query writing
public function getOfficer()
{
$usr = $this->session->userdata('id_user');
$userArray = array(8,10,24);
$this->db->select('*');
$this->db->from('tbl_officer');
if($usr == 4){
$this->db->where_in('usr',$userArray );
}else{
$this->db->where('usr',$usr);
}
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
return false;
}

Codeigniter Select and Count MySQL Records

Using Codeigniter 3, I would like to display all the records from a table in a MySQL database. I'd also like to include the number of records selected.
For example;
Showing x number of records;
record 1
record 2
record 3
etc
Currently I have the following (which works);
// select all records
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
// count all records
public function countRecords() {
$this->db->select('count(*) as count');
$this->db->from('records');
$query = $this->db->get();
return $query->row();
}
My question is do I need two separate queries in order to achieve this (select and count)?
Is there a more efficient way of achieving what I want?
You can do something like this :
public function selectRecords()
{
$query = $this->db->get('records');
if ($query->num_rows() > 0 )
{
$records = $query->result_array();
$data['count'] = count($records);
$data['all_records'] = $records;
return $data;
}
}
Pass it to the view from your controller :
$data = $this->model_name->selectRecords();
/*print_r($data) to see the output*/
$this->load->view('your_view',$data);
In view :
<?php echo $count .' number of records';?>
you can do only:
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
and
$records = $this->selectRecords();
$count = count($records);
In The first function itself you can get the count using $query->num_rows() function
public function selectRecords() {
$return = array();
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
$return['count'] = $query->num_rows();
$return['records'] = $query->result_array();
return $return;
}
try this
it will help you to provide pagination for records
public function selectRecords($params = array(), $count = false) {
$offset = isset($params['offset']) ? $params['offset'] : '';
$limit = isset($params['limit']) ? $params['limit'] : '';
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
if ($count) {
return $this->db->get()->num_rows();
}
if (empty($offset) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($offset) && !empty($limit)) {
$this->db->limit($limit, $offset);
}
$result = $this->db->get()->result();
return $result;
}

How to correctly alias a table in codeigniter?

I would like to do a join between 2 tables from my db.
this is my code:
public function getEpisodes($limit = 12, $order = 'id_e', $das = 'desc')
{
$this->db->select('e.id_e, e.id_s, e.num_e, s.id_s, s.nom_s, s.thumb_s')
->from('s_episodes as e, s_series as s')
->join('s', 'e.id_s = s.id_s', 'left')
->order_by('e.'.$order, $das)
->limit($limit);
$query = $this->db->get();
return $query->result_array();
}
This is the error code I get, when running my code:
What is wrong with my table alias?
Maybe try moving the s_series as s...
public function getEpisodes($limit = 12, $order = 'id_e', $das = 'desc')
{
$this->db->select('e.id_e, e.id_s, e.num_e, s.id_s, s.nom_s, s.thumb_s')
// Move s_series as s
->from('s_episodes as e')
->join('s_series as s', 'e.id_s = s.id_s', 'left')
->order_by('e.'.$order, $das)
->limit($limit);
$query = $this->db->get();
return $query->result_array();
}

Doctrine - conditional where

I have this method in my Laravel controller :
public function getUserOrders($userId) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'")
->andWhere("Orders.usersUserId = '{$userId}'")
->getQuery()->getArrayResult();
return $results;
}
I want to change this method that $userId will be optional , so last condition is not require in this situation:
public function getUserOrders($userId = NULL) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'")
if (!is_null($userId))
->andWhere("Orders.usersUserId = '{$userId}'")
->getQuery()->getArrayResult();
return $results;
}
I'm trying to figure out what is the best way to achieve this. Any suggestion?
Try this
public function getUserOrders($userId = NULL) :array{
$results = $this->_em->createQueryBuilder()
->select('Orders.orderItemCount')
->from($this->entityClass, 'Orders')
->Where("Orders.orderItemCount > '0'")
->andWhere("Orders.orderTotalPrice > '0'");
if (!is_null($userId)){
$results->andWhere("Orders.usersUserId" = $userId);
}
$results->getQuery()->getArrayResult();
return $results;
}

Counting row totals with Codeigniter Active Record

I'm storing values (int) for quantities in my database. I need to run a count on the total quantity by adding all the row totals together. The problem with $this->db->count_all_results() is that it returns the total rows but doesn't count all the values stored. Any help would be much appreciated.
function currentDealTotalQuantity($id)
{
$this->db->select('quantity');
$this->db->from('table');
$this->db->where('id', $id);
$total_sold = $this->db->count_all_results();
if ($total_sold > 0)
{
return $total_sold;
}
return NULL;
}
function currentDealTotalQuantity($id)
{
$this->db->select_sum('quantity');
$this->db->from('table');
$this->db->where('id', $id);
$query = $this->db->get();
$total_sold = $query->row()->quantity;
if ($total_sold > 0)
{
return $total_sold;
}
return NULL;
}
I believe you want this guy: $this->db->select_sum();
You replace your select statement with it, so that you have $this->db->select_sum('quantity'); That will produce the query string SELECT SUM(quantity) as quantity
The documentation is found here.
function currentDealTotalQuantity($id)
{
$qry = $this->db->select_sum('quantity')
->from('table')
->where('id', $id)
->get();
if ($qry->num_rows() === 0)
{
return FALSE;
}
return $qry->row('quantity');
}

Categories