I am handling three scenarios :
1) If query returns no result
2) if query returns one result
3) if query returns two or more result
This is my query :
$events = DB::table('bookings')
->join('events','bookings.event_id','=','events.id')
->join('spots','bookings.spot_id','=','spots.id')
->join(DB::raw('(select S.event_id,sum(S.spaces) as sum_spaces from spots S group by S.event_id) d'), function($join)
{
$join->on('bookings.event_id', '=', 'd.event_id');
})
->select('bookings.event_id','events.name', 'spots.event_date','d.sum_spaces',
DB::raw('COUNT(bookings.event_id) as tb'))
->groupBy('bookings.event_id')
->get();
I get $events in json format like this :
[] = 0
[{"event_id":1,"name":"Yoga","event_date":"2016-05-02","sum_spaces":"450","tb":6}] = 1
[{"event_id":1,"name":"Yoga","event_date":"2016-05-02","sum_spaces":"100","tb":16},{"event_id":2,"name":"Yoga","event_date":"2016-05-02","sum_spaces":"450","tb":6},{"event_id":3,"name":"blah","event_date":"2016-05-02","sum_spaces":"250","tb":6}] = 3
If you see that 0,1 and 3. thats what I want to know for a given resultant query.
I need to check if the above result has how many results. How can I find this
Because this is how I am handling the scenarios :
if(empty($events))
{
//sets default value
}
else if(check if $results have one and only one result)
{
//Do something
}
else
{
//here I can handle if $events query have more than one result.
}
If I understood you correctly, you need to count() them:
$numberOfResults = count($events);
Related
I display an element randomly from my test table, so how can I get the id of this element:
$tests= DB::table('test')->inRandomOrder()->limit(1)->get();
because I want to compare it with an other id
->get() returns a Collection or records from your test table. You need to use a loop to compare a single record's value:
$id = 1; // Or whatever you're comparing to
$tests = DB::table('test')->inRandomOrder()->limit(1)->get();
foreach($tests AS $test){
dd($test->id == $id);
}
Or simply use ->first() to return a single record from test:
$id = 1;
$test = DB::table('test')->inRandomOrder()->first();
dd($test->id == $id);
->get(); method return instance of \Illuminate\Database\Eloquent\Collection. For getting a single instance use ->first();method. See also official docs https://laravel.com/docs/5.5/queries#ordering-grouping-limit-and-offset For example.
$test = DB::table('test')->inRandomOrder()->first();
if ($test->id == $id) {
// your logic
}
I am trying to get a list of coupons through ajax when the checkboxes are selected. So everything else is working fine but the query is returning only the first match.
So my query is:
$this->db->from('tbl_coupons');
if($storeids !=''){
$ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
}
$this->db->where('coupon_cat', $catid);
$this->db->where('coupon_status', 'active');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$ds = $query->result_array();}
According to this my SQLquery becomes
SELECT * FROM `tbl_coupons`
WHERE `coupon_store` IN('1,97')
AND `coupon_cat` = '16'
AND `coupon_status` = 'active'
But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97
I checked values for coupon store 97 which exists in that category.
use below way if data exist it will be part of query.
storeids = explode(',',storeids);
$ids = array();
foreach($storeids as $val){
$ids[] = $val;
}
if(!empty($ids)){
$this->db->where_in('coupon_store', $ids );
}
hope it will create proper sql query
The query is mostly correct, except at line 2, where you need to make the change as:
WHERE coupon_store IN('1','97')
everything else remains the same.
I want to perform following search query, when user sends values I want to get matching results
If any results contain "none" i want to remove it from where condition and check only matching values for others
Example 1
$fructose = high
$lactose = mid
$polyols = none
$fructan = none
if so i want to check only matching results for $fructose=high and $lactose=mid
Example 2
$fructose = high
$lactose = mid
$polyols = mid
$fructan = mid
If so I want to check all matching results
Please advise, below is my current query
$fod = FodMap::select('*')
->where('fructose_level', '>=', $fructose)
->where('lactose_level','>=', $lactose)
->where('polyols_level', '>=', $polyols)
->where('fructan_level', '>=', $fructan)
->get();
Here I tried to solve your problem by writing query using eloquent structure. You should manage value and operator in where condition as per your need.
You can write your query with if condition like this:
$query = FodMap::select('*');
if($fructose != none){
$query = $query->where('fructose_level',$fructose);
}
if($lactose != none){
$query = $query->where('lactose_level',$fructose);
}
if($polyols != none){
$query = $query->where('polyols_level',$fructose);
}
if($fructan != none){
$query = $query->where('fructan_level',$fructose);
}
$fod = $query->get();
I am trying to condition a table so that it only shows when SQL results are returned. If no values are returned, then do not display the table. Here is what I have:
## PPAP Information
$q23 = "SELECT * FROM $modlible.P0353 JOIN $amflible.CUSMAS ON PCUSNO = CUSNO WHERE PITEM='$id'";
$stmt23 = db2_prepare($con, $q23);
$result23 = db2_execute($stmt23);
$fin23 = db2_fetch_assoc($stmt23);
How can I detect whether or not a result has been returned to me?
This might help
$iRows = db2_num_rows($stmt23);
if ($iRows > 0) {
//Do if zero results
} else {
//Do if more results
}
or you can check
if ($fin23) {
//Row was fetched
}
You can check the SQL code returned. If the code is SQL0100 (+100) it means no rows were returned.
If there are no results your while loop will not loop
while($fin23 = db2_fetch_assoc($stmt23)){
// print your results
}
I have a Joomla 1.5 site. I have articles in my site with different "status". What I'm trying to do is "count" and show how many articles have for example "status = 1"(expired) or "status = 3"(blocked) or "status = 2"(active) etc..
Here is the statuses in PhpMyAdmin - http://awesomescreenshot.com/07a8ijz75
Here is what I wrote, but it ALWAYS gives me same result - 1
<?php echo count($this->row->status==1) ?>
Did I miss something?
Thanks
Use the SQL count function.
select count(*) from articles where status = 1;
Use your DB! If you are sorting, counting, etc, data from a database in PHP code you're doing it wrong.
If you want all statuses, do something like:
select status, count(*) from articles group by status;
The count function in PHP counts all the elements in an array, and in your example you passed it a boolean value. As a result count doesn't know what to do with it, and so it returns -1, which isn't a valid count.
My PHP is really rusty (I haven't used it in a looong time), but here are two possible ways to accomplish what you want:
1. Use a function map/reduce style
<?php
$row[0]->status = 1;
$row[1]->status = 2;
$row[2]->status = 1;
$row[3]->status = 3;
$row[4]->status = 1;
// Count the number of statuses that are equal to 1
echo array_reduce(array_map(function($x) {
return $x->status == 1 ? 1: 0;
}, $row), function($x, $y) {return $x + $y;});
You'll have to replace the $row variable with $this->row, obviously. The code is essentially working in two steps. The inner part:
array_map(function($x) {
return $x->status == 1 ? 1: 0;
}, $row)
Creates a list where every status that's equal to 1 becomes a 1 and everything else becomes a 0. So you have an array of "array(1, 0, 1, 0, 1)". The out part of the code:
array_reduce( ... , function($x, $y) {return $x + $y;});
Takes the new array as the first argument and sums it all up by passing in the first two values of the array into the function, and then each following value and the result of the last function call. As a result all the values get summed, and you have a proper count of the matching values.
2. Use a simple procedural style
<?php
$row[0]->status = 1;
$row[1]->status = 2;
$row[2]->status = 1;
$row[3]->status = 3;
$row[4]->status = 1;
// Do it again, but in a procedural style
$num_1_statuses = 0;
foreach ($row as $r) {
if ($r->status == 1) {
$num_1_statuses++;
}
}
echo $num_1_statuses;
This should be really straightforward, it just has a variable that gets incremented whenever a row's status matches.