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));
Related
This is my Codeigniter project, I've made a bookshop function that counts the amount of brought and sold for the book in each entry and minus the sold price and gives me the amount that I currently have for each book.
Type "0" = The amount that I received
Else it's the amount that I spent
What I want to do now is I want to fetch the total amount that I received between date1 and date2 which should look something like "get_book_amount" for date1 -(minus) "get_book_amount" from date2.
The equation is like this:
On Date1 total amount that I received is 100, on Date2 it's 300. So the total amount that increased during that time is "200" which is the number I want to get.
How can I achieve this?
public function bookshop($date1,$date2)
{
$total_books = 0;
$books = '';
$this->db->select("*");
$this->db->from('books_s');
$this->db->where(['books_s.type' => 'Fantasy']);
$this->db->where(['books_s.stock' => 'Yes']);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$count_books = $query->result();
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
$getamount = $this->get_book_amount($single_book->id,$date1,$date2);
if($getamount > 0)
{
$amt = $getamount;
}
else
{
$amt = -($getamount);
}
$total_books = $total_books+$amt;
$books .= '<tr><td><h4>'.$single_book->name.'</h4></td>
<td style="text-align:right" ><h4>'.$amt.'</h4></td></tr>';
}
$books .= '<tr"><td ><h4><i>Total Current Assets</i></h4></td><td style="text-align:right;" ><h4><i>'.$total_books.'</i></h4></td></tr>';
}
}
}
//USED TO COUNT SINGLE BOOK AMOUNT
public function get_book_amount($warehouse_id,$date1,$date2)
{
$count_total_amount = 0;
$this->db->select("bookentry.id as transaction_id,bookentry.date,bookentry.naration,book_stock_entry.*");
$this->db->from('book_stock_entry');
$this->db->join('bookentry', 'bookentry.id = book_stock_entry.parent_id');
$this->db->where('book_stock_entry.warehouse', $warehouse_id);
$this->db->where('bookentry.date >=', $date1);
$this->db->where('bookentry.date <=', $date2);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$count_books = $query->result();
$count_total_amount = 0;
if($count_books != NULL)
{
foreach ($count_books as $single_book)
{
if($single_book->type == 0)
{
$count_total_amount = $count_total_amount + $single_book->getamount;
}
else
{
$count_total_amount = $count_total_amount - $single_book->getamount;
}
}
}
}
if($count_total_amount == 0)
{
$count_total_amount = NULL;
}
else
{
$count_total_amount = number_format($count_total_amount,'3','.','');
}
return $count_total_amount;
}
I think it's best if you do two separete querys and then calculate.
Create a Model Method call "get_book_amount_until_date"
First call the method with your date 1 and save it in a var.
Second call the method with your date 2 and save it in another var.
Now do:
Total = Results_date_2 - Results_date_1
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 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;
}
I want to select record with certain date and id. My datetime format in database is 2018-02-23 08:38:45. I've try this in my model but it always return the records with specific id only. My date format in $tgl is 23-01-2018 so I change the format using $date = date('Y-m-d', strtotime($tgl) );
public function spesific_odontogram($id_pasien, $date){
$query = "SELECT * FROM odontogram WHERE DATE('inserted_at') = $date AND id_pasien=$id_pasien";
$record = $this->db->query($query);
if ($record->row_array()>0) {
return $record->result_array();
}
return false;
//return $record;
}
Here's my controller
public function spesific_odontogram(){
$id_pasien=$_POST['id_pasien'];
$tgl=$_POST['tgl'];
$date = date('Y-m-d', strtotime($tgl) );
$data['record']=$this->m_pasien->spesific_odontogram($id_pasien, $date);
if ($this->input->is_ajax_request())
{
echo json_encode($data);
exit;
}
}
Use Date variable like '$date' instead of $date. Also make sure date format are same
public function spesific_odontogram($id_pasien, $date){
$query = $this->db->query("SELECT * FROM odontogram WHERE inserted_at = '$date' AND id_pasien=$id_pasien");
if ($query->row_array()>0) {
return $query->result_array();
}
return false;
}
Try using below query on your model :
public function spesific_odontogram($id_pasien, $date){
$query = "SELECT * FROM odontogram WHERE inserted_at >= $date AND inserted_at < $date + INTERVAL 1 DAY AND id_pasien=$id_pasien";
$record = $this->db->query($query);
if ($record->row_array()>0) {
return $record->result_array();
}
return false;
//return $record;
}
It basically check for only matches date record without calculate DATE() value of every row