insert Time in to database using Codeiniter - php

I have a form which is for company registration,Which has fields for open hours and close hours for each day in a week.
I have Two Tables Store the time
Timing | Week
In my timing Table i have four columns
id | company_id | day | open |close
week Table contain each day id
id | name
Suppose on Monday the company which is open on 8 am and closed on 3 pm .
Like Monday user fill all the days like whenever is open and close,
Now how can i insert these data in by its day by day into timing Table.
id c_id day open close
1 1 1 06:28:13 14:47:32
2 1 2 06:28:13 14:47:32
3 1 3 06:28:13 16:47:32
4 1 4 06:28:13 14:00:32
5 1 5 06:28:13 12:07:32
6 1 6 06:28:13 18:47:32
7 1 7 06:28:13 19:47:32
I want to store some thing like this here day column refers to the week table which has id of the name of day.
I do not understand how to insert. i have also tried to insert_batch.
This is my form
Controller:
i have tried many times but i could not find way to do this.
I know this is not the right way, but How can i do this???
$time['open']['monday_open'] = $this->input->post('monday');
$time['close']['monday_close'] = $this->input->post('monday_close');

Related

Mysql GROUP BY and Union based on date condition

Here is my query - it mostly works, but I can see it failing on one condition - explained after the query:
$firstDay = '2020-03-01' ;
$lastDay = '2020-03-31' ;
SELECT * FROM clubEventsCal
WHERE ceFreq!=1
AND (ceDate>='$firstDay' AND ceDate<='$lastDay')
UNION SELECT * FROM clubEventsCal
WHERE ceFreq=1
AND (ceDate>='$firstDay' AND ceDate<='$lastDay')
GROUP BY ceStopDate ORDER BY ceID,ceDate ;
The first select gives me all Event records between the two dates. The second select gives me grouped/summarized Event records between the two dates. The problem though is if the value ceDate spans days across two months: IE: 2020-03-30 thru 2020-04-02. When I pull the records for March, all is good - the above query pulls the 2020-03-30 record (grouped) as the first instance of the 4 days/records - allowing us to charge for a single 4 day event. But when I pull the records for April its also going to pull 2020-04-01 as a new grouped Event record for the last two days of the 4 day event and try to charge the customer for a new Event - when in fact those two days were already a part of March's bill.
How can I write the query so that when ceDate starts in Month X but ends in Month Y that when records are pulled for Month Y its not trying to pull records that actually belong to an Event that started in Month X?
Examples of an Event record would look like this:
rid | ceID | ceActive | ceFreq | ceDate | ceStopDate
------------------------------------------------
1 1108 1 3 2020-03-09 | 2020-03-09
2 1111 1 2 2020-03-15 | 2020-03-15
3 1112 1 2 2020-03-17 | 2020-03-17
4 1117 1 1 2020-03-30 | 2020-04-02
5 1117 1 1 2020-03-31 | 2020-04-02
6 1106 1 3 2020-03-21 | 2020-03-21
7 1110 1 2 2020-03-05 | 2020-03-05
8 1113 1 2 2020-03-24 | 2020-03-24
9 1117 1 1 2020-04-01 | 2020-04-02
10 1117 1 1 2020-04-02 | 2020-04-02
The above query pulls all records where ceFreq != 1, and it pulls a single record for the ceFreq = 1 records (rids: 4 & 5). For March, we don't necessarily care that ceID 1117 spills into April. But when we pull records for April - we need to exclude rid 9 & 10, because the Event (ceID=1117), was already accounted for in March.
SELECT * FROM clubEventsCal
...
GROUP BY ceStopDate
This is gibberish.
MySQL (depending on configuration) allows it without choking - but it's semantically wrong and stands out as an anti-pattern.
There are some edge cases where the values returned might contain significant data, but they very unusual. Trying to explain a problem with code which does not work is perhaps not a good strategy.
Looking at your code, its possible that you don't need a union - but there's not enough information in your example records to say if this would actually give the result you expect (it will be significantly faster depending on your indexes):
SELECT IF(cefreq=1, rid, null) AS consolidator
, ceid
, cefreq
, MIN(cedate), MAX(cedate)
, ceStopDate
FROM clubEventsCal
WHERE cID=1001
AND ceActive!=2
AND (ceDate>='$firstDay' AND ceDate<='$lastDay')
GROUP BY IF(cefreq=1, rid, null)
, ceid
, cefreq
, ceStopDate
;
I would have added the ORDER BY - but I don't know where clId came from. Also This will give different resuts to what I think you were trying to achieve for any record where cefreq is null (if you really do want to exclude them, add a predicate in the WHERE clause).

Convert MySQL time stamp into DAY then sort by MAX

Trying to write a mySQL query that selects the most recent(**) number of times a light was turned on by a user.
**Most recent being all the times the light was turned on for the most recent day in the DB.
Sample Table:
DB Name: LLL
Table Name: Lights
UserID | LightOn | LightOff
-----------------------------------------------------
3 | 2018-01-08 09:00:00 | 2018-01-08 09:03:00
3 | 2018-01-08 10:15:00 | 2018-01-08 10:17:00
3 | 2018-01-07 15:00:00 | 2018-01-07 15:05:00
So, From this table, we can tell that
UserID 3 (Bob) turns the light on:
2 times on January 8th (at 9AM for 3 minutes and 10:15AM for 2 minutes) &
1 times on January 7th (at 3PM for 5 mins)
I want my query to return 2, because there are 2 records for the most recent day of January 8th.
I'm at the point where I can only get the number of records:
SELECT COUNT(C.LightOff) AS count FROM LLL.Lights AS C
WHERE C.UserID = 3
ORDER BY C.LightsOff DESC
I get the following back:
count
-------
3
I need to figure out a way to convert the time stamp into a DAY and get all the records that match that MAX Day.
The desired result is:
count
-------
2
Any ideas?
Assuming you have a proper datetime value in you lightoff column
you could get the most recent day and join with your count
select count(*) from LLL.Lights
inner join (
select max(date(LightOff)) max_date
FROM LLL.Lights
WHERE UserID = 3
) t on t.max_date = date(LightOff)

Creating a table for each individual user's timetable data

I'm coding an online timetable website which holds all your current subjects you are attending at school and contains slots to write work and it's due date. Mainly for my own personal use, but I guess I could share the URL with a few people when it's completed. Anyway, I have a login / register system set up, linked to a MySQL database. I have the following columns in a table called 'users'.
userid, username, firstname, lastname, password
Before I attempt anything too stupid, I wanted somebody to give an opinion on what I am about to attempt...
I thought I could write some PHP that creates a table for each new user when they sign up. It would contain all their subjects for each day of the week, and once they input their data it would write it to the database and they wouldn't have to edit their information unless they had to (subject change, etc...)
Would a whole new table for each user's subject data be efficient? The data would have two dimensions: The day of the week (x axis) and the periods of the school day where the subjects are situated (periods 1-6 for my school)
Anyway, thanks for reading, opinions on the best way to go around doing this would be helpful. Thank you.
EDIT: Strawberry's suggestion
userid,day,period,subjectid
1 1 1 4
1 1 2 2
1 1 3 5
1 1 4 3
1 1 5 1
1 1 6 7
2 1 1 4
2 1 2 2
2 1 3 5
2 1 4 3
2 1 5 1
2 1 6 7

Get all months with number of months and days from 2 date?

Here is my 2 mysql tables. In clients table is showing user connection date and in clients_pay_bill table showing in which month user is paid his/her bill from connection date (conn_date).
a) clients
clients_id conn_date
=======================
1 2016-06-01
2 2016-07-17
3 2016-06-22
4 2016-09-03
b) clients_pay_bill
cpp_id clients_id paid_month
===================================
1 1 2016-07-03
2 2 2016-07-22
3 4 2016-09-09
4 2 2016-07-22
Now I want to show all months with number of days and months of which clients is not paid until current date.
For example :
clients_id = 1 connection date (conn_date) is 2016-06-01 and he only paid 2016-07-03 month bill till now. So the sql query will be output following months:
2016-06
2016-08
2016-09
**2016-07 will not print because he already paid this months bill**
and I also want to show number of days and months e.g: 3 months some days..
I can't imagine how the sql query should look like ?
I will suggest you a bit different approach cause this one will kill you both logically and performance speaking. The way it should be done is to have records genreated in clients_pay_bill not when someone pays but when he/she should pay and as an addition to it there should be 2 columns: due_date and paid_date. So your table would look like this:
cpp_id clients_id due_month paid_month
====================================================
1 1 2016-06-03 NULL
1 1 2016-07-03 2016-07-03
1 1 2016-08-03 NULL
2 2 2016-09-22 NULL
3 4 2016-09-09 NULL
4 2 2016-07-22 NULL
You could generate monthly due note via cron or if you prefer to stay in SQL than via events. After that you have simple query selecting which due note has NULL in paid_month cause if payment was done than you would update paid_month column.
Regarding days I am not sure what effect you would like to achieve (example would help) but having list of due notes it's already half way to make calculations regarding amount of days on these records (eg. with DATEDIFF)

Efficient SELECT from table of boolean changes

I'm trying to figure out an efficient query for a project I'm working on.
We're recording a switch state into a table, each time it changes, a row is added with the new value (0 or 1).
Here's a simplified structure of the table:
day | hour | state
-----+------+-------
10 | 1 | 1 # day 10
10 | 6 | 0
10 | 21 | 1
11 | 3 | 0 # day 11
11 | 6 | 1
13 | 13 | 0 # day 13
....
Now we need to make a daily overview, something like this:
Day 11 : Switch was on during 0-3, 6-24
SELECT * FROM log WHERE day = 11 will give us only [3,0] and [6,1]. From those we can guess that it started ON and ended ON, but how about day 12?
SELECT * FROM log WHERE day = 12 gives nothing, obviously - there's no clue to guess from.
What is an efficient and reliable way to get the starting and ending state for a given day? Something like "Select one entry before day 12 and one after day 12"?
SELECT
day,
hour,
state
FROM
log
WHERE
day*100+hour
BETWEEN
(SELECT max(day*100+hour) FROM log WHERE day < 12)
AND
(SELECT min(day*100+hour) FROM log WHERE day > 12)
Will give you everything between (including) the last entry before day 12 and the first entry after day 12.
The second part might be unnecessary if you don't need to know when the state changed, and it's enough to know the state didn't change until at least midnight of the selected day.

Categories