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
Related
i want to make pagination on ci from other web reference, but average of them use only 1 table
does anyone know how to input more than 1 table on this condition
public function data($number,$offset){
return $query = $this->db->get('client',$number,$offset)->result();
}
i have try to input like this
public function data($number,$offset){
return $query = $this->db->get('client,advertisement,size',$number,$offset)->result();
}
but the result is not my expected
the NAMA is looping, the IKLAN too but the SIZE is not
i take the refences from here
I use this:
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$this->db->join('client', 'advertisement.id_client= client.id_client','inner');
$this->db->join('size', 'advertisement.id_size= size.id_size','inner');
$query = $this->db->get("advertisement");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
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 can I join the two arrays from controller? I tried to use AND operator as shown below but I failed. How can I make it possible?
function view_product($category_id)
{
$category_id=$this->uri->segment(3);
$this->load->model('select');
$this->data["products"]=$this->select->get_products($category_id);
$this->data["prices"]=$this->select->get_price($category_id);
$this->load->view("header");
$this->load->view("products",
$this->data);
}
here is my view:
<?php
$n=0;
foreach(($products as $product) && ($prices as $price) ) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
echo '</table>';
?>
My model
function get_products($category_id)
{
$this->db->select('*');
$this->db->from('categories c');
$this->db->join('products p', 'c.category_id=p.category_id', 'left');
$this->db->where('c.category_status = 0');
$this->db->where('c.category_id', $category_id);
$query = $this->db->get();
return $query->result_array();
}
function get_price($category_id)
{
$this->db->select('*');
$this->db->from('categories c');
$this->db->join('price r', 'c.category_id=r.category_id', 'c.product_id=r.product_id', 'left');
$this->db->where('c.category_status = 0');
$this->db->where('c.category_id', $category_id);
$query = $this->db->get();
return $query->result_array();
}
How can i join the two arrays from controller?
to iterate over 2 arrays, use array_map
function output_prices($product,$price) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
array_map('output_prices',$products,$prices);
source: http://php.net/manual/en/function.array-map.php
Please change your View as per below mentioned code.
<?php
$n=0;
$merg_detls=array_merge($products,$prices);
foreach($merg_detls as $detl) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$detl['product_id'].'</a></td>
<td>'.$detl['product_name'].'</td><td>'.$detl['product_description'].'</td><td>'.$detl['price_per_unit'].'</td><td>'.$detl['price_per_item'].'</td></tr>';
}
echo '</table>';
?>
How can I get the month and year of the results of post data from database?
This data i want to retrieve from database is 2016-03-11
My code inside the controller:
public function report(){
$nomor = $this->input->post('nomor_reseller');
$bulan = $this->input->post('month'); //with format YYYY-MM
$report_data = $this->hasil_m->get($nomor,$bulan);
}
My code in model:
function get($nomor,$bulan) {
$this->db->where('nomor_reseller',);
$this->db->where('tgl_pembelian',);
$this->db->select('*');
$this->db->from('tb_pelanggan');
$this->db->join('tb_anggota', 'tb_anggota.id_koperasi = tb_pelanggan.id_koperasi');
$this->db->order_by("tgl_pembelian","DESC");
$this->db->group_by('nomor_reseller');
$query = $this->db->get ();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
What i want to do is get 2016-03, using $this->db->where.
You might try:
echo date('Y-m', strtotime($date));
Friends I'm Unable to get open positions Sum. here is my code. I am getting 1(one) instead of total sum. Help me to solve this issue.
Controller:
function index(){
$userId = $this->phpsession->get("userId");
$userType = $this->phpsession->get('userType');
$date = date("Y-m-d");
if(!$userId && !$this->phpsession->get("userType")){
redirect(base_url().'user');
}
$config['base_url'] = base_url().'requirement/index';
$config['total_rows'] = $this->requirement_model->GetRequirement(array("count"=>true));
$config['per_page'] = 5;
$config['cur_tag_open'] = '<a>';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$options['offset'] = $this->uri->segment(3);
$options['limit'] = $config['per_page'];
$options['join']=true;
$data['clients'] = $this->client_model->ajaxclient();
$data['requirements'] = array(""=>"Choose requirement");
$data['requirement'] = $this->requirement_model->GetRequirement($options);
$data['links'] = $this->pagination->create_links();
$data['totalactive']=$this->requirement_model->GetRequirement(array("find"=>true));
$data['totalrequirement']=$this->requirement_model->GetRequirement(array("totalreq"=>true));
$data['openpositions']=$this->requirement_model->GetRequirement(array("openpos"=>true));
//print_R($data['openpositions']);exit;
//echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Requirement Details";
$this->layout->view("requirement/index",$data);
}
This is my model function
Model:
function GetRequirement($options = array()){
if(isset($options['requirementId']))
$this->db->where('requirementId',$options['requirementId']);
if(isset($options['clientName']))
$this->db->where('clientName',$options['clientName']);
if(isset($options['limit']) && isset($options['offset']))
$this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit']))
$this->db->limit($options['limit']);
$this->db->order_by("activateddate", "DESC");
if(isset($options['join'])){
$this->db->select('r.*,c.clientName as cName');
$this->db->from('requirement as r');
$this->db->join('clients as c','c.clientId=r.clientName');
$query=$this->db->get();
if(#$options['requirementId']) return $query->row(0);
return $query->result();
}
if(isset($options['find'])){
$this->db->select('distinct(clientName)');
$this->db->from('requirement');
$this->db->where('(clientName) and (noofpositions > 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
if(isset($options['totalreq'])){
$this->db->select('requirementName');
$this->db->from('requirement');
$this->db->where('(noofpositions > 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
if(isset($options['openpos'])){
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$this->db->count_all();
$query=$this->db->get();
return $query->num_rows();
}
$query = $this->db->get('requirement');
if(isset($options['count'])) return $query->num_rows();
if(#$options['requirementId']) return $query->row(0);
return $query->result();
}
This is my View page
View:
<div class="inner">
<h3><?php echo $openpositions; ?></h3>
<p>Total Positions Opened</p>
</div>
You are using sum which is an aggregate function and with out group by it will take whole table as one group in if(isset($options['openpos'])){ ... } part of code of your model your are returning num_rows() which returns the no. of rows so in your case there will be one row with the value of sum therefore you are getting result as 1 change your
if (isset($options['openpos'])) {
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$query = $this->db->get();
return $query->row()->openpos;
}
I think the mysql statement has an error.
Change the following line:
$this->db->where('(closedPos = 0) ');
To
$this->db->where('closedPos', 0);
remove the following line: (this will count all rows and return the value, which you do not want)
$this->db->countall();
If this does not solve your problem you could try outputting the mysql statement by adding exit($this->db->last_query()); to try and find the problem, like this:
if(isset($options['openpos'])){
$this->db->select_sum('openPos');
$this->db->from('requirement');
$this->db->where('(closedPos = 0) ');
$this->db->count_all();
$query=$this->db->get();
// output last query
exit($this->db->last_query());
return $query->num_rows();
}