Can the following code be abbreviated? Is there a simple way to implement the following code? I think it is too complicated to write this code. if I write data for a month, wouldn't I need to write 30 cases?
Please help me. I am a newbie.
Reply to discussion (solution)
What do you want to do?
Can you tell me what your requirements are?
What do you want to do?
Can you tell me what your requirements are?
I got all the data for this week from the database, and then determined the number of records each day. for example, today is Thursday, and I got data from the database from Monday to Thursday, then we get the data for Monday, Tuesday, Wednesday, and Thursday respectively. if today is Sunday, I get all the data for this week, and then I get the data for Monday, Wednesday, and Thursday respectively. In the future, it will be extended to the month to get the daily quota.
The program above indicates that
First obtain all the data queried from the database. Then, the initial daily quantity is 0. then, if is used to determine the time to be placed in the cyclic data, respectively, to determine the date, then the number of days plus 1, and finally output all the data.
Or is there another simple implementation method? Consider the pressure on the database and the speed of program execution.
Recursion...
If you get weekly data from the database
Then, set the time data retrieved by $ r ['Date']
$ C = array (0, 0, 0, 0, 0 );
// In The Loop
$ C [date ('W', $ r ['Date'])] ++;
$ C indicates the daily count.
If you get data from the database in January
Then, set the time data retrieved by $ r ['Date']
$ C = array_fill (1, 31, 0 );
// In The Loop
$ C [date ('D', $ r ['Date'])] ++;
$ C indicates the daily count.
What do you want to do?
Can you tell me what your requirements are?
I got all the data for this week from the database, and then determined the number of records each day. for example, today is Thursday, and I got data from the database from Monday to Thursday, then we get the data for Monday, Tuesday, Wednesday, and Thursday respectively. if today is Sunday, I get all the data for this week, and then I get the data for Monday, Wednesday, and Thursday respectively. In the future, it will be extended to the month to get the daily quota.
The program above indicates that
First obtain all the data queried from the database. Then, the initial daily quantity is 0. then, if is used to determine the time to be placed in the cyclic data, respectively, to determine the date, then the number of days plus 1, and finally output all the data.
Or is there another simple implementation method? Consider the pressure on the database and the speed of program execution.
It's easy to increase the number of days.
You can simply save it with an array.
If you increase by the day of the week, this is simple:
Use the array $ days = array (, 0) directly; // based on date ('W', $ r ['Date'])-number of weeks, for example, an array obtained from "0" (Sunday) to "6" (Saturday)
This way
You only need
$ Days [date ('W', your date)] ++; you can omit your judgment.
If you are on a monthly basis, you define an array with a subscript of 1-31. The initial values are all 0 arrays, and the other values are the same as those of the week.
If you get weekly data from the database
Then, set the time data retrieved by $ r ['Date']
$ C = array (0, 0, 0, 0, 0 );
// In The Loop
$ C [date ('W', $ r ['Date'])] ++;
$ C indicates the daily count.
If you get data from the database in January
Then, set the time data retrieved by $ r ['Date']
$ C = array_fill (1, 31, 0 );
// In The Loop
$ C [date ('D', $ r ['Date'])] ++;
$ C indicates the daily count.
Thank you. the chinmo moderator on the 6th floor has already solved this problem. what a hero sees is a little different. thank you!