how to display my query in view codeignitier - codeigniter

this my controler
public function ajax_pasien($Kode)
{
$data = $this->infokamar->get_by_id1($Kode);
echo json_encode($data);
}
this my model
public function get_by_id1($Kode)
{
$sql = "select a.norm, b.nama as pasien, b.alamat, c.tanggal, c.tanggal1, f.jumlah, f.biaya from TTKunjungan a inner join TMPasien b on A.norm = b.norm inner join TTKunjunganDetail c on A.Kode = c.kodekunjungan inner join TMTrfTindakan d on c.kodetindakan = d.Kode inner join TMTrfLayanan e on d.kodelayanan = e.Kode left join (select kodett, sum(jumlah) as jumlah, sum(biaya) as biaya from TTKunjunganDetail where kodett is not null group by kodett) f on c.Kode = F.kodett Where e.kodeklasifikasi = 2 and c.tanggal1 is null and c.kodett is null and c.kodetindakan = '{$Kode}'";
$query = $this->db->query($sql);
return($query->result_array());
}
and this my view but nothing show

The best way to check your queries is by two ways:
1. Printing the last query Use below code to print last executed query
<?php echo $this>db->last_query() ?>
2. Enable profiler Make sure your output class is loaded/autoloaded. Just enable profiler in your controller's constructor by adding below code and see the magic after rendering the page
$this->output->enable_profiler(true)
-Cheers

Send data from controller to your respective view as follows in controller..
public function ajax_pasien($Kode)
{
$data = $this->infokamar->get_by_id1($Kode);
//echo json_encode($data);
$this->load->view('view_name',$data);
}

Related

How to get data from 2 related tables in the same controller in codeigniter?

I'm trying to get data from 2 DB tables that are related. Departments and Functions: Each Department has multiple Functions.
I want to show all data in an accordion on the same page.
Until now, the code works like this:
Declare the variable departments in the controller, and in view, in the foreach loop, I have called the Functions model in a variable.
Controller:
public function index(){
//Data variable declarations
$data['page_title'] = "Departments and Functions";
$data['page_subtitle'] = "choose an occupation for the list of users assigned";
//Data variable declaration - from models
$data['departments'] = $this->Department->get_all('departments');
$this->load->view('layouts/header');
$this->load->view('layouts/title', $data);
$this->load->view('layouts/aside');
$this->load->view('departments/index', $data);
$this->load->view('layouts/footer');
}
Here I want to add the Function->get_by_department_id() method. How to get the ID dynamically?
The occupation model methond:
public function get_by_fk($table = 'occupations', $fk = "department_id" , $fk_value=$department->department_id){
$this->db->select('*');
$this->db->from($table);
$this->db->where($fk, $fk_value);
$query = $this->db->get();
return $result = $query->result();
}
Try using a left join
function get_departments_and_functions() {
$sql = 'SELECT `deptartments`.*, `functions`.*
FROM `departments` LEFT JOIN `functions`
ON `functions`.`deptartments_id` = `departments`.`id`';
return $this->db->query($sql)->result();
}
You could also query the departments and then foreach over the departments and query to get the functions. Then you would manually build the result set.

ConfigServiceProvider in Laravel 5 doesn't pass the correct values to Controllers/Views

I run a query in the ConfigServiceProvider to set count values that I want to use throughout the application.
public function boot() {
$query = "SELECT count(*) FROM property_in_property_category INNER JOIN property ON property_in_property_category.property_id = property.id INNER JOIN business ON property.business_id = business.id WHERE property_in_property_category.property_category_id = 1 AND property.active = 1 AND business.isActive = 1";
$sale = DB::select($query);
foreach($sale as $object) {
$sale_array[] = (array) $object;
}
$query = "SELECT count(*) FROM property_in_property_category INNER JOIN property ON property_in_property_category.property_id = property.id INNER JOIN business ON property.business_id = business.id WHERE property_in_property_category.property_category_id = 2 AND property.active = 1 AND business.isActive = 1";
$rent = DB::select($query);
foreach($rent as $object) {
$rent_array[] = (array) $object;
}
config([
'for-sale' => $sale_array[0]['count(*)'],
'for-rent' => $rent_array[0]['count(*)']
]);
}
When I run the queries in phpMyAdmin I get the correct values like 413 and 227 back.
But when I call these values in a view using config('for-sale') and config('for-rent'), the values are incorrect, like 446 and 239. This is on a live Linux server. When running on local (Windows) it gives me the correct values.
First I thought this may have to do with casing of names, but as the query runs well in phpMyAdmin and there is no casing in the rest of the code this is not a possibility

Laravel5 Eloquent custom method return array

I have a static method in my User model which counts something, i am using a raw statement:
static function countOrders($userId)
{
$sql = "SOME CUSTOM-COMPLICATED QUERY";
$result = \DB::select( \DB::raw( $sql ) );
return $result; // <-- toArray() ? returns Exception!
}
Somewhere in my controller:
$orders = User::countOrders(*USER-ID*);
How can I get an array with records as arrays and not objects without modifying the global configuration for the FETCH method?
You don't need to run toArray().
When executing raw queries select returns array, it's stated in the docs:
The select method will always return an array of results.
static function countOrders($userId)
{
$sql = "SOME CUSTOM-COMPLICATED QUERY";
$result = \DB::select( \DB::raw( $sql ) );
return $result;
}

codeigniter LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}

Codeigniter JOIN dont back db result

Codeigniter dont back database result.
Database table "Category" and "Sub Category"
DB Shema:
Categor
-----------------------------
ID Name
----------------------------
1 Fishing
2 Hunting
3 Test Category
Sub_category
-----------------------------
ID cat_id name
----------------------------
1 1 Fishing rod
2 2 Hunting ammunition
3 3 Test sub category
I want list all sub category for some category. When some1 click on Fishing category i want show all sub category for fishing. My code is this:
Controller:
public function get_sub_category($id = 0)
{
$this->load->model('front_m');
$data['sub_cat'] = $this->front_m->show_sub_cat($id);
$this->template->set_theme('zend')->set_layout('front.html')
->build('sub_category',$data);
}
MODEL:
public function show_sub_cat($id=0)
{
$this->query = $this->db->select('*');
$this->query = $this->db->from('category');
$this->query = $this->db->where('id='.$id');
$this->query = $this->db->join('sub_category', 'sub_category.cat_id=category.id');
$this->query = $this->db->query('SELECT * FROM category');
$this->query = $this->db->get();
if ($this->query->num_rows() > 0) {
$this->query->result();
}
return $this->query ;
}
What is wron i all time have DB error or blanko page.
Based on your question it sounds like your over thinking it, but I'm a bit confused as well. What I'm hearing is: How can I get my sub categories based on someone clicking on a main category id? If that's the case then your making it way more complicated than it needs to be.
MODEL
public function show_sub_cat($catid=NULL)
{
$result = $this->db->get_where('sub_category',array('cat_id'=>$catid));
if ($result->num_rows()>0)
{
return $result->result();
}
}
$sql_query ="SELECT
c.name,
sc.*
FROM Category as c
LEFT JOIN Sub_category as sc ON sc.cat_id = c.ID
WHERE c.ID = $id";
return $this->db->query($sql_query)->result();

Resources