Dynamic PHP / MYSQLi Pagination - php

I'm trying to make pagination work on my site dynamically and I'm not sure how to change regular pagination to accomplish what I need to do and I was wondering if a PHP guru could help out!
Basically, once a week a report runs and populates records for the last week in my database. Also, at the end of the month, the report runs and pulls in the last month of data.
I would like to build my PHP page so that all items in the same month (~4-5 weeklies and 1 monthly) show up on the same page, and that each pagination page is a different month's data.
Currently the weeks are stored as two fields, start and end date, in the format: 2018-mm-dd.
How could I have it so that page 1 is all 2018-03 records, page 2 is all 2018-02 records and page 3 is all 2018-01 records? I also would like this to be automated so each newest month becomes thE new page 1.
Does this make sense? Is this possible? Any assistance would be greatly appreciated!! Thank you very much!!!

Related

Retrieve next id in database which may not be the next auto incremented number php mysql

apologies as this seems to me to be a very simple answer, but am struggling to find a solution having searched around.
I have a pretty simplistic table that stores a series of info for booking a caravan - options are one week or two week bookings. I've previously used (and successfully for the last however many years) a simple if else statement to check availability. The weeks are sequential in the database on an auto incremented basis and i simply searched for the next week and a third week as a fail safe by
$next_week_id1 = $row['id'] + 1; and $next_week_id2 = $row['id'] + 2;.
Basic I know, but it has worked until now where there has been some kind of discrepancy with the auto increment - I have an id which misses the next incremental value so from id 91 for example it goes to 93, which messes up the whole script.
Its been a while since I touched this, just wondering how you'd hit the next value in the database (for the second week and then the third week) without using the id as an incremental stepping stone? They will always be the next one in the list, but clearly using id+1 or id+2 is a non-reliable way of accessing this...
Thanks for any advice.

ajax sum total number of VIEWS

I have a statistic on my page which shows the TOTAL NUMBER OF ARTICLE VIEWS...
here is the code I wrote to have the total number of views:
$db = JFactory::getDBO();
$total_views = "SELECT SUM(times_viewed) FROM #__hdflv_upload";
$db->setQuery($total_views);
$result = $db->loadResult();
What I'm trying to achieve is, UPDATE this total number of Views AJAX on page, so if I get for example +10 views in the last 5 seconds, the total number of views must SUM +10 AJAX... without page reload....
I searched in google etc.. but didn't find nothing close to this that can help me... can somebody PLEASE give me a hand. THANK YOU VERY MUCH.
On your statistic page, you could set a JS timer which uses .ajax to get the SQL results every 5 seconds. The script it calls can return the query result in JSON format, then update your DOM accordingly.
There's a pretty good answer at Joomla Stackexchange which should help you get a Joomla AJAX call working properly to run the SQL and return the result.

Show every 10th result Drupal 7 Views

I have Drupal 7 View. It contains 2249 results to be displayed on a graph. I am using the highcharts module to display that data. I have created a running total of the integer data with a global math expression field. I wanted to keep the expression's total on the graph but limit the amount of results as they are overlapping each other and it looks really bad.
I am using aggregation to combine days that are similar but it seems at this point date granularity is not available with the date module in views.
Is there a way to do this views php filter and use a bit of php to do the math so it would work ?
I have a Global:PHP filter that has this in it but it's not doing anything right maybe someone has a tip to get it working properly -
if( $row->id % 10 == 0 ) return TRUE;
thanks

Too many SQL calls on page load?

I'm constructing a website for a small collection of parents at a private daycare centre. One of the desired functions of the site is to have a calendar where you can pick what days you can be responsible for the cleaning of the locales. Now, I have made a working calendar. I found a simple script online that I modified abit to fit our purpose. Technically, it works well, but I'm starting to wonder if I really should alter the way it extracts information from the databse.
The calendar is presented monthly, and drawn as a table using a for-loop. That means that said for-loop is run 28-31 times each time the page is loaded depending on the month. To present who is responsible for cleaning each day, I have added a call to a MySQL database where each member's cleaning day is stored. The pseudo code looks like this, simplified:
Draw table month
for day=start_of_month to day=end_ofmonth
type day
select member from cleaning_schedule where picked_day=day
type member
This means that each reload of the page does at least 28 SELECT calls to the database and to me it seems both inefficient and that one might be susceptible to a DDOS-attack. Is there a more efficient way of getting the same result? There are much more complex booking calendars out there, how do they handle it?
SELECT picked_day, member FROM cleaning_schedule WHERE picked_day BETWEEN '2012-05-01' AND '2012-05-31' ORDER BY picked_day ASC
You can loop through the results of that query, each row will have a date and a person from the range you picked, in order of ascending dates.
The MySQL query cache will save your bacon.
Short version: If you repeat the same SQL query often, it will end up being served without table access as long as the underlying tables have not changed. So: The first call for a month will be ca. 35 SQL Queries, which is a lot but not too much. The second load of the same page will give back the results blazing fast from the cache.
My experience says, that this tends to be much faster than creating fancy join queries, even if that would be possible.
Not that 28 calls is a big deal but I would use a join and call in the entire month's data in one hit. You can then iterate through the MySQL Query result as if it was an array.
You can use greater and smaller in SQL. So instead of doing one select per day, you can write one select for the entire month:
SELECT day, member FROM cleaning_schedule
WHERE day >= :first_day_of_month AND day >= :last_day_of_month
ORDER BY day;
Then you need to pay attention in your program to handle multiple members per day. Although the program logic will be a bit more complex, the program will be faster: The interprocess or even network based communication is a lot slower than the additional logic.
Depending on the data structure, the following statement might be possible and more convenient:
SELECT day, group_concat(member) FROM cleaning_schedule
WHERE day >= :first_day_of_month AND day >= :last_day_of_month
GROUP BY day
ORDER BY day;
28 queries isnt a massive issue and pretty common for most commercial websites but is recommend just grabbing your monthly data by each month on one hit. Then just loop through the records day by day.

Storing and Displaying Live Stats

Say we are a site receiving massive amounts of traffic, Amazon.com size traffic. And say we wanted to display a counter on the home page displaying the total number of sales since December the first and the counter was to refresh via ajax every 10 seconds.
How would we go about doing this?
Would we have a summary database table displaying the total sales and each checkout would +1 to the counter and we would get that number every 10 seconds? Would we COUNT() the entire 'sales' table every 10 seconds?? Is there an external API I can push the stats off to and then do an ajax pull from them?
Hope you can help, Thanks
If your site is ecomm based, in that you are conducting sales, then you MUST have a sales tracking table somewhere. You could simply make the database count part of the page render when a user visits or refreshes your site.
IMO, there is no need to ajax this count as most visitors won't really care.
Also, I would recommend this query be run against a readonly (slave) database if your traffic is truly at amazon levels.
I would put triggers on the tables to manage the counter tables. When inserting a new sale the sum table would get the new value added to the row for the current day. That also gives sales per day historically without actually querying the big table.
Also, it allows for orders to be entered manually for other dates than today and that day get updated statistics.
As for the Ajax part that's just going to be a query into that sum table.
Whatever you do, do not re-COUNT everything every 10 seconds. Why not to have a cronjob, which does the counting of data every 10 seconds? It could take current time-10 seconds and in slave database add the difference to current count ?
Still 10 seconds sound bizarre. Every minute, mm?

Categories