I have these tables
Loans Table
| coop_id | loan_id | loan_availability |
Loan Seasons Table
| coop_id | loan_id | loan_season_start | loan_season_end |
And I'm trying to get the details of each loan in one array whether their availability is Always or Seasonal. If it is marked as seasonal, it has a data in the loan seasons table.
This is my query
$loanlist = DB::table('loans')
->join('loan_seasons', 'loan_seasons.loan_id', 'loans.loan_id')
->where('loans.coop_id', $coop_id)
->where(function ($q){
$q->where('loans.loan_availability', "Always")
->orWhere(function ($qq){
$qq->where('loans.loan_availability', "Seasonal")
->where('loan_season_start', '>=', Carbon::now()->month)
->where('loan_season_end', '<=', Carbon::now()->month)
->where('loan_season_status', 1);
});
})
->where('loans.loan_status', "1")
->get()->toArray();
I'm getting this output:
Array(
[0] => Array(
[id] => 2
[coop_id] => 1
[loan_id] => 3
[loan_name] => Loan 1
[loan_desc] => Loan 1 Description
[loan_maxamount] => 500000
[loan_availability] => Always
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_start] => 2
[loan_season_end] => 6
[loan_season_status] => 1
)
)
My expected output is like this:
Array(
[0] => Array(
[id] => 1
[coop_id] => 1
[loan_id] => 3
[loan_name] => Loan 1
[loan_desc] => Loan 1 Description
[loan_maxamount] => 500000
[loan_availability] => Always
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_status] => 1
)
[1] => Array(
[id] => 2
[coop_id] => 1
[loan_id] => 4
[loan_name] => Loan 2
[loan_desc] => Loan 2 Description
[loan_maxamount] => 200000
[loan_availability] => Seasonal
[loan_season_start] => 2
[loan_season_end] => 6
[loan_status] => 1
[created_at] => 2017-02-26 17:43:08
[updated_at] => 2017-02-26 17:43:08
[loan_season_status] => 1
)
)
I've been trying to solve this for hours but I still can't get the query right. Can somebody help? I'm very new to nested queries. Help?
I got it working now. Seems like I've been making simple queries complicated. I created different queries for each condition and joined them into one instead. Thanks.
Related
I have one multidimensional-array and i want to insert into my database, there is one issue if one entry in my database and same entry is on my multidimensional-array that value should not insert again in my database.
$activity[] = array('user_id'=> $_SESSION['user_id'], 'choosen_date' => $ch_date, 'distance' => $distance, 'pace' => $pace );
Array
(
[0] => Array
(
[user_id] => 1200
[choosen_date] => 2018-12-11
[distance] => 1.72
[pace] => 12.4812
)
[1] => Array
(
[user_id] => 1200
[choosen_date] => 2018-12-09
[distance] => 3.17
[pace] => 3.8736
)
[2] => Array
(
[user_id] => 1200
[choosen_date] => 2018-11-14
[distance] => 2.26
[pace] => 6.3504
)
[3] => Array
(
[user_id] => 1200
[choosen_date] => 2018-11-07
[distance] => 0.53
[pace] => 3.6576
)
)
And following is my database entry
----------------------------------------------------
| S.No | user Id | choose date | distance | Pace |
----------------------------------------------------
| 1 | 1200 | 2018-12-09 | 3.17 | 3.8736 |
----------------------------------------------------
| 2 | 1200 | 2018-12-11 | 2.17 | 5.67 |
----------------------------------------------------
so here, in database S.no 1 and array index 1 both are the same entry so I want to insert rest value into my database. So how can I insert?
I am assuming that you are using MySQL database, you can use INSERT IGNORE INTO
or INSERT ON DUPLICATE KEY UPDATE
If you want to do it through PHP then you can try this.
$i=0;
foreach($activity as $key=>$val)
{
$sn_check=mysqli_query($con,"SELECT id FROM table WHERE S.No='$i'");
if(mysqli_num_rows($sn_check)==0)
{
$user_id=$val[$i]['user_id']; //all other values can be catch like this.
$query_insert=mysqli_query($con,"INSERT QUERY");
}
$i++;
}
I have a database for clock in clock out system The table structure consists of time_id, user_id, weekNo, ClockYear, EntryDate, StartTime, EndTime and updated_at
basically the startTime is clock in and EndTime is clocked out time what im trying to do is run a query to get me the total worked hours by a user on a sepcific date total instead of total hours worked per individual entry. so currently i have the following query:
SELECT CONCAT(users.first_name," ",users.last_name) AS theUser,
time_logs.*, TIMEDIFF(endTime,startTime) AS totTime FROM time_logs
LEFT JOIN users ON time_logs.user_id = users.user_id WHERE
time_logs.entryDate >= "2016-08-17" AND time_logs.entryDate <= "2016-08-25" order by user_id ASC
and this returns the results as
[0] => Array
(
[theUser] => Sam Johnson
[timeId] => 14
[user_id] => 1
[weekNo] => 34
[clockYear] => 2016
[entryDate] => 2016-08-23
[startTime] => 2016-08-23 16:16:30
[endTime] => 2016-08-23 17:36:14
[updated_at] => 2016-08-23 17:36:14
[totTime] => 01:19:44
)
[1] => Array
(
[theUser] => Sam Johnson
[timeId] => 15
[user_id] => 1
[weekNo] => 34
[clockYear] => 2016
[entryDate] => 2016-08-24
[startTime] => 2016-08-24 01:00:00
[endTime] => 2016-08-24 05:15:00
[updated_at] =>
[totTime] => 04:15:00
)
[3] => Array
(
[theUser] => Bob Doe
[timeId] => 19
[user_id] => 2
[weekNo] => 34
[clockYear] => 2016
[entryDate] => 2016-08-24
[startTime] => 2016-08-24 10:00:00
[endTime] => 2016-08-24 13:15:00
[updated_at] =>
[totTime] => 03:15:00
)
As you can see it gives total time for that specific entry but what i want to get is just one entry for each user with the total time totalled up for all entries by that user on that day combined so for user 1
[0] => Array
(
[theUser] => Sam Johnson
[user_id] => 1
[weekNo] => 34
[clockYear] => 2016
[entryDate] => 2016-08-23
[totTime] => 05:34:44
)
can anyone help with this not sure if i can do it using a query
You are almost there!
Assuming that totTime is a Time Field (Not a DateTime field) and I HOPE user_id is unique
Sorry I put XXX as I didn't want to spend the time writing this fully out
Select CONCAT(XXX) theUser, user_id,min(weekNo) weekNo,min(clockYear) clockYear, min(entryDate) entryDate,sum(TIMEDIFF(endTime,startTime)) totTime FROM XXX ORDER BY user_id GROUP BY user_id
That should give you what you want. The key is GROUP BY which you didn't have. You can change the MIN commands to MAX if that's what you want to show.
theUser will be picked from the last entry I believe, user_id should remain the same and the other fields will be calculated by the GROUP BY and I had to repeat the field names on the sum and min functions because sum and min return a unique fieldname
I have this data table:
+-ID-+-sensorID-+----date---+-value-+
| 1 | 1 |'some date'| 20 |
| 2 | 1 |'some date'| 15 |
| 3 | 1 |'some date'| 18 |
| 4 | 2 |'some date'| 40 |
| 5 | 2 |'some date'| 68 |
| 6 | 2 |'some date'| 55 |
Now I want to create an array like this in php:
date - value sensorID(1) - value sensorID(2)
I also need something to show whenever a date has a single sensor value:
[date] => 01-01-2014 [value] => 50 [value] => 60
[date] => 02-01-2014 [value] => 20 [value] => 30
[date] => 03-01-2014 [value] => null [value] => 55
[date] => 04-01-2014 [value] => 20 [value] => 33
How can I do this efficiently in some simple steps? Otherwise I would just create a result for ID 1 and ID 2 or run my function twice on the complete array.
Just get all the data in one run and construct the array in PHP:
// do the mysql query stuff >> $result (array of all rows in db)
$data = array();
foreach ($result AS $row) {
if (!isset($data[$row['date']])) {
$data[$row['date']] = array('sensor1' => null, 'sensor2' => null);
}
$data[$row['date']]['sensor' . $row['sensorID']] = $row['value'];
}
Result would be something like
$data = array(
'01-01-2014' => array('sensor1' => 50, 'sensor2' => 60),
'02-01-2014' => array('sensor1' => 20, 'sensor2' => 30),
'03-01-2014' => array('sensor1' => null, 'sensor2' => 55),
'04-01-2014' => array('sensor1' => 20, 'sensor2' => 33)
);
I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..
I have an array as follows
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[33636] => 1
[67376] => 2
[73250] => 1
[125313] => 2
[142062] => 1
[342520] => 2
[357301] => 2
[357303] => 1
[404419] => 1
[408957] => 1
[415891] => 2
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
My aim is to order them by the single digit value so the output would put the 2's(or highest number) to the top
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[415891] => 2
[125313] => 2
[67376] => 2
[342520] => 2
[357301] => 2
[33636] => 1
[73250] => 1
[142062] => 1
[357303] => 1
[404419] => 1
[408957] => 1
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
Try with the arsort function:
arsort — Sort an array in reverse order and maintain index association
Example:
arsort($array);
// done, $array is now sorted
Built into PHP: http://php.net/manual/en/function.arsort.php
bascially you need toh known array function for php from here
and function as
arsort($arr);
Simple
arsort($array)
http://www.php.net/manual/en/function.arsort.php