How to correctly alias a table in codeigniter? - php

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();
}

Related

Codeigniter display TYPE_LABLE from TYPE table if result equal to specific CATEGORY

I'm working on Codeigniter rest API project and came across a problem. I would like my model to select additional data by condition.
if CATEGORY equals $CATEGORY show TYPE.TYPE_LABLE
otherwise just don't show "TYPE.TYPE_LABLE"
so how can i get this result?
thank you for your help.
function get_data() {
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$CATEGORY = array('CATEGORY1', 'CATEGORY2', 'CATEGORY3', 'CATEGORY4');
$types = array('type_1', 'type_2');
if ($id != null) {
$this->db->where('ID',$id);
//selct TYPE.TYPE_LABLE if CATEGORY equals $CATEGORY
$this->db->select('POST.ID, POST.DATE, TYPE.Post_Type as P_TYPE, TYPE.TYPE_LABLE$CATEGORY', FALSE);
$this->db->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left');
$this->db->join('CATEGORY', 'POST.CATEGORY_ID = CATEGORY.ID', 'left');
$this->db->order_by('POST.ID', 'DESC');
$this->db->where_in('POST.Post_Type', $types);
$this->db->where('POST.DATE >', date('Y-m-d', strtotime('01-01-2019')) );
$this->db->from('POST');
$result = $this->db->get();
}
return $result;
}

error joining tables in php mysql

I want to join tables in php mysql. Here's my code:
public public function getResponInfo($kode_laporan)
{
$this->db->select('*');
$this->db->from('laporan l, respon_user ru, respon r');
$this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
$this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
$this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
$this->db->where("r.kode_laporan = '". $kode_laporan."'");
//$this->db->order_by('r.tanggal', 'asc');
return $this->db->get();
}
And it got error like this
All I want to do is to display
'isi_respon' and 'tanggal' from respon table, 'nama_opd' from admin table, 'isi_respon' and 'tanggal' from respon_user table, 'nama' from user table.
Then, I want to order them by "tanggal" from respon and respon_user tables.
Both tables respon and respon_user have kode_laporan from laporan table.
Is it possible to do that? thank you for your help
updated
I think all the codes that u've given to me are working. but i got another problem to display them through the controller and view files.
here's my controller code:
public function detailLaporan($kode_laporan)
{
$data['page']='detaillaporan';
$data['laporan'] = $this->Home_model->getLapDetail($kode_laporan);
$data['id'] = $this->session->userdata('id');
$data['respon'] = $this->Home_model->getResponInfo($kode_laporan)->result();
$data['rsp'] = $this->Home_model->getResponInfo($kode_laporan)->num_rows();
$data['kode_laporan'] = $this->session->set_userdata('kode_laporan');
$this->load->view('home/master', $data);
}
view:
if($rsp > 0){
foreach ($respon as $r) {
echo $r['tanggal'];
echo $r['nama'];
/*i want to display 'nama' or 'nama_opd',
it depends on whether it belongs to 'respon' or 'respon_user' table*/
$r['isi_respon'];
}
}
Please try the below code
$this->db->select('r.isi_respon, r.tanggal as respon_tanggal, a.nama_opd, ru.isi_respon as respon_user_isi_respon, ru.tanggal as response_user_tanggal, u.nama');
$this->db->from('respon r');
$this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
$this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
$this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
$this->db->where("r.kode_laporan = '". $kode_laporan."'");
$this->db->order_by('r.tanggal, ru.tanggal', 'asc');
return $this->db->get();
$this->db->select('l.*,ru.*,r.*,a.*,u.*');
$this->db->from('laporan l');
$this->db->join('admin a', 'a.id_admin = r.id_admin', 'left');
$this->db->join('respon_user ru', 'ru.kode_laporan = r.kode_laporan', 'left');
$this->db->join('user u', 'u.id = ru.id_pengirim', 'left');
$this->db->where("r.kode_laporan = '". $kode_laporan."'");
$this->db->order_by('r.tanggal', 'asc');
return $this->db->get();

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

Codeigniter: query returns values of NULL

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

GROUP a result of JOINS using QUERY BUILDER in Codeigniter

I have the following function, the params are "asc,PV1"
public function get_products($order, $sku){
if(is_array($result) && $result['active']){
$id_emp = $result['id_emp'];
$mts = 'module_tienda_status';
$mtc = 'module_tienda_categories';
$mtp = 'module_tienda_productos';
$mtpt = 'module_tienda_product_type';
$mtpa = 'module_tienda_product_attribute';
$mta = 'module_tienda_attributes';
$mtao = 'module_tienda_attribute_option';
$mtpv = 'module_tienda_product_variation';
$mtpvo = 'module_tienda_product_variation_options';
$get = array(
$mtp.'.id',
$mtp.'.name',
$mtp.'.sku',
$mtp.'.desc_short',
$mtp.'.desc_long',
$mtp.'.id_category',
$mtp.'.price',
$mtp.'.id_status',
$mtp.'.inventory',
$mtp.'.imgs',
$mtp.'.qty',
$mtp.'.featured',
$mtc.'.name as cat_name',
$mtpt.'.id as type_id',
$mtpa.'.id_attribute',
$mtpa.'.id_option',
$mta.'.name as attr_name',
$mtao.'.option as opt_name',
$mtpv.'.inventory as var_inv',
$mtpv.'.qty as var_qty',
$mtpv.'.price as var_price',
$mtpvo.'.id_product_variation as var_id',
$mtpvo.'.id_attribute as var_attr',
$mtpvo.'.id_option as var_opt'
);
$this->db->select($get);
$this->db->from($mts);
$this->db->join($mtp, $mtp.'.id_status = '.$mts.'.id');
$this->db->join($mtc, $mtp.'.id_category = '.$mtc.'.id', 'left');
$this->db->join($mtpt, $mtp.'.id_product_type = '.$mtpt.'.id');
$this->db->join($mtpa, $mtpa.'.id_product = '.$mtp.'.id', 'left');
$this->db->join($mta, $mta.'.id = '.$mtpa.'.id_attribute', 'left');
$this->db->join($mtao, $mtao.'.id = '.$mtpa.'.id_option', 'left');
$this->db->join($mtpv, $mtp.'.id = '.$mtpv.'.id_product', 'left');
$this->db->join($mtpvo, $mtpv.'.id = '.$mtpvo.'.id_product_variation', 'left');
$this->db->where($mtp.'.id_emp', $id_emp);
if($sku != 'null'){
$this->db->where($mtp.'.sku', $sku);
}
if(!is_null($order)){
$this->db->order_by('module_tienda_productos.created', $order);
}
$query = $this->db->get()->result();
}else{
return $result;
}
}
the result is an array with 49 rows of the same product, the difference is that there are different attributes and variations, here is the example https://pastebin.com/3X4w6wEi
What i want is 1 single result with an array of attributes and 1 array of variations something like this (is a json, please decode it)
[{'id':'343','name':'Producto variable 1','sku':'PV1','desc_short':'<p><br></p>','desc_long':'<p><br></p>','id_category':null,'price':null,'id_status':'1','inventory':'0','imgs':null,'qty':'0','featured':'0','cat_name':null,'type_id':'2','attributes':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs','113':'s','114':'n','116':'xl'}}],'variations':[{'var_id':'42','var_inv':'0','var_qty':'0','var_price':'0.00','opts':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs'}}]}]}]

Categories