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();
Related
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;
}
I have got 2 models within my Codeigniter, 1 getting all rooms and the second model is getting the booked rooms in a range of selected date.
On my view I can list all rooms with a foreach and when I insert another foreach for the booked ones to appear in red color, I get a result but, I see duplicated results
My Models:
public function rooms(){
$result = $this->db->select('*')
->from('rooms')
->group_by('room_id')
->get()
->result();
return $result;
}
public function check_room_availability($start, $end){
$this->db->select('*');
$this->db->from('room_actions as ra', 'rooms as r');
$this->db->join('rooms as r', 'r.room_id = re.room_id', 'LEFT');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$this->db->group_by('ra.room_id');
$query = $this->db->get()->result();
return $query;
}
And my View :
<?php foreach($all_rooms as $room) {
foreach($booked_ones as $booked) {?>
<div class="<?php
if($booked->room_id == $room->room_id){
echo 'room_box red';}
else{
echo 'room_box green';}?>">
<?php echo $room->room_id; ?>
</div>
<?php } ?>
<?php } ?>
I'd appreaciate if you could help me. Thanks.
As you can see in the image below if I have 2 rooms booked for the chosen dates my rooms duplicate. If I have 3 rooms booked, then it becomes X 3.
https://pasteboard.co/I8AkG8i.jpg
Try this, hope it will help you.
If rooms table has a unique room_id then do not need to use group_by()
public function rooms(){
$result = $this->db->select('*')
->from('rooms')
->get()
->result();
return $result;
}
Here do not need to join()
public function check_room_availability($start, $end){
$booked_rooms = array();
$this->db->select('room_id');
$this->db->from('room_actions as ra');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$query = $this->db->get()->result();
if(isset($query) && count($query) > 0){
foreach($query as $row){
if(!in_array($row->room_id, $booked_rooms)){
array_push($booked_rooms, $row->room_id);
}
}
}
return $booked_rooms;
}
Now you have a simple array of booked room ids. In the view do this with only one forach() loop
foreach($all_rooms as $room) {
if(in_array($room->id, $booked_rooms)){
//Room is booked
}
}
Try this,
public function check_room_availability($start, $end) {
$this->db->select('r.room_id as room_id,ra.room_id as booked_room_id');
$this->db->from('rooms as r');
$this->db->join('room_actions as ra', 'r.room_id = ra.room_id', 'LEFT');
$this->db->where('ra.days >=', $start);
$this->db->where('ra.days <=', $end);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
}
OR
public function check_room_availability($start, $end) {
$this->db->select('*,(select room_id from room_actions as ra where ra.room_id = r.room_id and ra.days >=' . $start . ' and ra.days <=' . $end . ') as booked_room_id');
$this->db->from('rooms as r');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
}
To better result please share your tables or your expected output, according to that I can update my query
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();
}
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;
}
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'}}]}]}]