Codeigniter MYSQL data minus date1 data from date2 - php

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

Related

->result() not showing all data/rows

I have a question
Let say we have this 2 tables in our database
first table : category_lists
second table: data_category
so if list_data_id is equal to 1 meaning that it has 2 data if you look at data_category table which is 6 and 7
Now what I want is to query from category_list table so that 6 and 7 will echo Car Wreckers and Cash For Cars.
This is what my code looks like:
below is my controller:
public function index($listing_name)
{
$this->load->view('layouts/head_layout_seo');
$this->load->view('layouts/head_layout');
$data['main_view'] = 'listings/main_view';
$data['listing_data'] = $this->Listing_model->get_detail_listing($listing_name);
$list_id = $data['listing_data']->list_data_id; // this is what I get list_data_id is equal to 1
$data['category_ads'] = $this->Ads_model->get_ads($list_id); // this is what you need to look
$this->load->view('layouts/main', $data);
$this->load->view('layouts/footer_layout');
}
below is my model:
public function get_ads($list_id)
{
$this->db->where('list_data_id', $list_id);
$query = $this->db->get('data_category');
$query = $query->result();
//return $query;
if (count($query) > 0) {
for ($i=0; $i < count($query); $i++) {
foreach ($query as $value) {
$this->db->where('category_lists_id', $value->category_lists_id);
$querys = $this->db->get('category_lists');
//print_r($query);
return $querys->result();
}
}
}
}
below is my view:
foreach ($category_ads as $value) {
<p><?php echo $value->categories; ?></p> }
with the code above I get only 1 data which is Car Wreckers as you can see from the table, it supposes to show 2 data Car Wreckers and Cash For Cars
Can anyone help me with this?
Thank You
Try this, in your model so no need to call DB two times and no need to loop
public function get_ads($list_id)
{
$this->db->select('cl.category_lists_id,cl.categories');
$this->db->from('category_lists cl');
$this->db->join('data_category dc','cl.category_lists_id = dc.category_lists_id');
$this->db->where('dc.list_data_id',$list_id);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return array();
}
}

How to fetch and return record with total number of records in codeigniter

I am working with rest api using codeigniter,I want to fetch records + total records + per page records,but not worked for me
Here is my function in controller
<?php
public function search_shop()
{
$users['rec'] = $this->Model_users->find_shop($_POST);
if($users['rec']!="")
{
$responseJSON = array("Status" => true,"Result" => $users['rec']);
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
else
{
$responseJSON = array("Status" => false,"Message" => "There is no matching record");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
}
And here is my "find_shop" function in model,How can i fetch all records with total number of records and per page records ?
public function find_shop()
{
$add_data['page_number'] = ($this->input->post('page_number') && !empty($this->input->post('page_number'))) ? $this->input->post('page_number') : NULL;
$add_data['search'] = ($this->input->post('search') && !empty($this->input->post('search'))) ? $this->input->post('search') : NULL;
$records="10";
$mins="10";
if($add_data['page_number']=="" || $add_data['page_number']=="0" || $add_data['page_number']=="1")
{
$starting_row="0";
$last_row="10";
}
else
{
$last_row="9";
$cd="1";
$lim="10";
$starting_row=$add_data['page_number']*$lim-$last_row-$cd;
$last_row=$add_data['page_number']*$records-$cd;
}
$this->db->select('*');
$this->db->from('shop s');
$this->db->like('shop_name',$add_data['search']);
$this->db->or_like('city',$add_data['search']);
$this->db->limit($last_row,$starting_row);
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$row = $query->result_array();
return $row;
return $query->num_rows;
}
else
{
return false;
}
}
?>
Try This, Hope it helps
in the Controller please test this after model related changes
$users = array();
$users = $this->Model_users->find_shop($_POST);
echo'<pre>';print_r($users);die;
in the Model, There is a mistake in the return, You cannot do two return at the same time
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$data = array();
$data['row'] = $query->result_array();
$data['total_records'] = $this->db->count_all_results('shop');
$data['per_page_records'] = $query->num_rows();
return $data;
}else{
return array('row'=>array(),'total_records'=>'0','per_page_records'=>'0');
}
Change your if condition like this:
$records="10"; // This is records per page showing
if ($add_data['page_number']=="" || $add_data['page_number']=="0" || $add_data['page_number']=="1")
{
$starting_row="0";
} else {
$starting_row = ($add_data['page_number'] - 1) * $records;
}
You need to set limit and offset of query. For example: limit 0, 10: this will show first 10 records. Here 0 is starting point and 10 is the length of records which you want to show. 10 will always be same as you are showing 10 records. Limit 10, 10: it will show records from 10 to 19. hope it clears your doubt.

Laravel search query with different tables

There are three tables; venues, venues_parameters and calendars tables. On a venue table all venues added and venue details added on venues_parameters table. On a calendar table, a booked (1st half, 2nd half, full day with flag 1, 2 & 3) venue detailed store with date and other information.
What i want that if i am searching with today date, date it is check on the calendar table that the searchable date are already booked or not, if date booked with 1st half or 2nd half venue will available on search else not available.
My controller code :
public function index(Request $request, $event_type = '', $capacity = '', $venue_type = '', $catering = '')
{
//dd($request->datepicker); return date (format : 25/12/2017)
$venueQuery = Venue::whereStatus('Active');
dd($venueQuery);
if($request->occasion) {
$event_type = $request->occasion;
}
if($request->datepicker) {
$selectedDate = $request->datepicker;
} else {
$selectedDate = date('m/d/Y');
}
if($request->capacity) {
$capacity = $request->capacity;
}
if($request->venue_type_id) {
$venue_type = $request->venue_type_id;
}
if($request->catering) {
$catering = $request->catering;
}
if($event_type != '') {
$venueQuery->whereHas('occasions', function ($query) use ($event_type) {
$query->where('occasions.id', $event_type);
});
}
if($capacity != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($capacity) {
$query->where('venue_parameters.max_capacity', $capacity);
});
}
if($venue_type != '') {
$venueQuery->whereHas('venueType', function ($query) use ($venue_type) {
$query->where('venue_types.id', $venue_type);
});
}
if($catering != '') {
$venueQuery->whereHas('venueParameter', function ($query) use ($catering) {
if($catering == "indoor") {
$catering = "1";
$query->where('venue_parameters.is_indoor', $catering);
} if($catering == "outdoor") {
$catering = "1";
$query->where('venue_parameters.is_outdoor', $catering);
}
});
}
$venues = $venueQuery->get();
$venues_count = count($venues);
return view('front.venue.index', compact('venues','event_type','venues_count','selectedDate'));
}
Calendar table for the reference :

Get month in date from database

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

codeigniter mysql query output data display with specific condition

here are the following codes:
controller.php
function getchart() {
$prac = $this->input->post('prac_name');
$datee = $this->input->post('datee');
$this->load->model('appoint');
$results['appoint'] = $this->appoint->getappoint($prac , $datee);
$this->load->view('ajax/getappchart' , $results);
}
model.php
function getappoint($prac , $datee) {
$this->db->select('rdv.id as rdvid, startTime, endTime, day, firstname, lastname');
$this->db->from('rdv');
$this->db->join('contact', 'contact.id = rdv.contact_id');
$this->db->where('people_id',$practicien);
$this->db->where('DATE(day)', $datee);
$this->db->order_by('TIME(startTime)', 'ASC');
$query = $this->db->get();
//print_r($this->db->last_query());
return $query;
}
view.php
if ($appoint->num_rows() > 0) {
foreach($appoint->result() as $sub_row)
{
// display output.
}
} else {
echo 'No Appointments on Above Date.';
}
?>
what i need is, there are appointments with sametime and day(mostly 2 same).
if there is morethan 2, i need to set two different class style for both the appointment.
how can i achieve this ?
Thanks.
Final answer: done it with the help of #minhaz-ahmed
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//REST VIEW CODE
}
}
else {
echo 'No Appointments on Above Date.';
}
You can do like this
if ($appoint->num_rows() > 0) {
$appoint_counter = array();
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
if (!isset($appoint_counter[$key])) {
$appoint_counter[$key] = 0;
}
$appoint_counter[$key] ++;
}
foreach ($appoint->result() as $sub_row) {
//i am assuming your startTime is H:M:S, and day Y-M-D format
$key = strtotime($sub_row['day'] . ' ' . $sub_row['startTime']);
$style_class = 'YOUR_1ST_CLASS';
if ($appoint_counter[$key] > 2) {
$style_class = 'YOUR_2ND_CLASS';
}
//YOUR REST VIEW
}
} else {
echo 'No Appointments on Above Date.';
}
What do you mean by class style? Anyway, the code below assumes you will put the results in a datagrid.
$prev_date = "";
$prev_time = "";
$results = $query->result();
foreach($results as $appointment) {
if ($prev_date != $appointment->day) // Assuming "day" is tables date
# Display output here
if ($prev_time != $appointment->startTime) // Logically, display should sort appointments by their starting time
# Display output here
# Display appointment rows here
$prev_date = $appointment->day;
$prev_time = $appointment->startTime;
}
if (empty($results)) {
# Display "no appointments found" output here
}

Categories