I ask the question before I was not getting anymore replies and I decide to re-post it if that will help. I hope i'm not doing anything wrong by re-posting?
$total_cost_for_A = $this->admin_model->total_cost_for_A($branch, $department, $date);
$total_cost_for_B = $this->admin_model->total_cost_for_B($branch, $department, $date);
The variables $total_cost_for_A and $total_cost_for_B hold the returned value from a mysql summation queries in the model. The queries return false if no record was found. Now, I'm trying to perform this operation.
if(($total_cost_for_A === FALSE) && ($total_cost_for_B === FALSE))
{
$data['no_record'] = 'No Record Found!';
$this->load->view('admin/bookings', $data);
}
else
{
$this->load->view('admin/summary', $data);
}
This test always fail and execute the else statement instead, which is not what. Any assistance will be appreciated. Thanks
Here are the functions
function total_cost_for_A($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE bk.date = "'.$date.'"';
}
$result = $this->db->query($select_amount_paid);
if($result->num_rows() > 0)
{
return $result;
}
else
{
return FALSE;
}
}
function total_cost_for_B($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$total_LF_amount = 'SELECT SUM(total_amount) total_amount FROM large_format_print
WHERE date = "'.$date.'"';
}
$result = $this->db->query($total_LF_amount);
if($result->num_rows() > 0)
{
return $result;
}
else
{
return FALSE;
}
}
Try to change like this
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE date = '.$date;//i assume that date is an colum from graphics_booking
you are wrong with "bk.date" you can give directly if it is in your table with where condition and also try to avoid the same name for the function in the model and the varaible assigned.Try to change as different.otherwise your code looks good
No when u return u cannot return the entire , you need to return a row
so you need to do in your model as :
function total_cost_for_A($branch, $department, $date)
{
if($branch == 'Head Office' && $department == 'Summary')
{
$select_amount_paid = 'SELECT SUM(total_amount) total_amount FROM graphics_booking AS bk
WHERE bk.date = "'.$date.'"';
}
$result = $this->db->query($select_amount_paid);
if($result->num_rows() > 0)
{
return $result->row(); // you missed out here
}
else
{
return FALSE;
}
}
Same for the total_cost_for_B()
Related
Situation:
$post_1 = 'john';
$post_2 = 30;
$arr = array(['name'=>'john','number'=>70],['name'=>'clark','number'=>50]);
$collection = collect($arr);
if($post_1 == 'john')
{
$collection->where('name',$post_1);
}
if($post_2 == 70)
{
$collection->where('number',$post_2);
}
var_dump($collection->all());
But this doesn't work. I want to include filters but it depends on the post parameters to exist.
I think you can use when
$result= $collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
})->all();
dd($result);
or
$collection->when(($post_1=="john"), function ($collection)use($post_1) {
return $collection->where('name',$post_1);
})->when(($post_2 == 70), function ($collection)use($post_2) {
return $collection->where('number',$post_2);
});
dd($collection->all());
Ref:https://laravel.com/docs/8.x/collections#method-when
i have a filter string, what do you think how can i convert laravel query builder? I used some ways but I wasn't sure.
filter: [["id","=",2],"or",["id","=",3],"and",["name","LIKE", "%John%"]]
Thank you.
if ($request->get('filter')) {
$filter = json_decode($request->get('filter'));
if (gettype($filter[0]) === 'string') {
list($field, $id) = $filter;
$query->where($field, $id);
} elseif (gettype($filter[0]) === 'array') {
foreach ($filter as $value) {
if (gettype($value) === 'string') {
switch ($value) {
case 'or':
//
break;
case 'and':
//
break;
}
} elseif (gettype($value) === 'array') {
list($field, $clause, $id) = $value;
}
}
}
}
Maybe something like this:
$arr = [["id","=",2],"or",["id","=",3],"and",["name","LIKE", "%John%"]];
$query = Model::query(); // build the empty query
if(size($arr) > 0){ // if there is at least 1 element
$query->where(...$arr[0]); // apply that element
for($i = 1; $i < size($arr); $i+=2){ // for every other entry, loop through them 2 by 2 and based of the current one, apply orWhere or where
if($arr[$i] === "or")
$query->orWhere(...$arr[$i+1]);
else
$query->where(...$arr[$i+1]);
}
}
$result = $query->get();
This is my Callback function for form validation in CodeIgniter
public function user_check($str)
{
if ($str == 'sounitkar13')
{
return TRUE;
}
else
{
$this->form_validation->set_message('user_check', 'The %s is not found in our record');
return FALSE;
}
}
right now i am comparing $str with "sounitkar13" but i have a table with 100 username stored so i want to compare with those 100 user names and if it matches with any of those 100 then return TRUE otherwise FALSE. ( it is reverse of is_unique function.
you have to do condition on table rows using where condition
$query = $this->db->get_where('table', array(
'username' => $str
));
$count = $query->num_rows();
if ($Count != 0)
{
return TRUE;
}
else
{
$this->form_validation->set_message('user_check', 'The %s is not found in our record');
return FALSE;
}
public function shirts($type = '') {
{
if ($type == 'glass') {
$shirt = Product::where('category_id', '1') - > get();
}
elseif($type == 'ic') {
$shirt = Product::where('category_id', '2') - > get();
}
elseif($type == 'cover') {
$shirt = Product::where('category_id', '3') - > get();
} else {
$shirt = Product::all();
}
$products = Category::find($shirt);
return view('front.shirt', compact('products', 'shirt'));
}
}
can some one help me in minimising this Controller I don't want to use if else statement
I have a huge query that needs to be modified right now it doesn't eager load the withCount, whenever I call propertyimages_count it always return 0 instead of the correct count. But if I call $property->propertyimages()->count() it returns the correct number but it is not eager loading.
I'm not really sure why propertyimages_count, I am thinking maybe it is the alias.
$search = (isset($_GET['sSearch']) ? $_GET['sSearch'] : '');
$get = $this->method_vars($_GET);
$today = date('Y-m-d');
$where = Property::
withCount('propertyimages')
->with(['project', 'latestmaintenance', 'propertyimages'])
->join('projects', 'properties.project_id', '=', 'projects.id')
->where(function( $query ) use ($get) {
if($get['price_from'] >= 1 && $get['price_to'] <= 500000) {
$query->whereBetween('rental_price', [$get['price_from'], $get['price_to'] ]);
}
$query = $get['sqm_from'] >= 1 && $get['sqm_to'] <= 1000 ? $query->whereBetween('sqm', [$get['sqm_from'], $get['sqm_to'] ]) : $query;
if( $get['type'] != '' ) {
if( $get['type'] == 'house' ) {
$query->where('projects.type', 'Housing Project');
} else if( $get['type'] == 'condo' ) {
$query->where('projects.type', 'Condo Project');
} else if( $get['type'] == 'any' ) {
$query->whereIn('projects.type', ['Condo Project', 'Housing Project']);
}
}
$query = $get['view'] != '' ? $query->where('view', $get['view']) : $query;
$query = $get['project'] != '' ? $query->where('project_id', $get['project']) : $query;
$query = $get['bedrooms'] != '' ? $query->where('bedrooms', $get['bedrooms']) : $query;
$query = $get['bathrooms'] != '' ? $query->where('bathrooms', $get['bathrooms']) : $query;
$query = $get['property_status'] != '' ? $query->where('status', $get['property_status']) : $query;
if( $get['status'] != '' ) {
if( $get['status'] == 'rented' ) {
$query->whereRaw('CURDATE() >= check_in')->whereRaw('CURDATE() <= check_out');
} elseif( $get['status'] == 'vacant' ) {
$query->where(function($q) {
$q->whereRaw('CURDATE() NOT BETWEEN check_in AND check_out')->orWhereNull('check_in');
});
}
}
})
->orderBy('projects.name')
->orderBy('unit')
->whereRaw("(SELECT check_in
FROM property_rentals
WHERE property_rentals.property_id = properties.id
ORDER BY
CASE
WHEN check_out >= '{$today}' AND check_in <= '{$today}' THEN 0
WHEN check_in > '{$today}' THEN 1
ELSE 2
END
LIMIT 1) as check_in")
->whereRaw("(SELECT check_out
FROM property_rentals
WHERE property_rentals.property_id = properties.id
ORDER BY
CASE
WHEN check_out >= '{$today}' AND check_in <= '{$today}' THEN 0
WHEN check_in > '{$today}' THEN 1
ELSE 2
END
LIMIT 1) as check_out");
Maybe to add count images relation, something like this:
public function imagesCount()
{
return $this->hasMany('App\ProperyImage')
->selectRaw('property_id, count(*) as count)
->groupBy('property_id');
}
...and then add it to the with clause
Check this