Views vs. in page calculations - php

Ok I wanted to get the communities thoughts on the use of Views in MYSQL vs. in page calculations using php.
I am about to start trying to design a page which relies on retrieving data from my database and working out averages and totals based on the data it retrieves. My initial thoughts were that I should retrieve it in the normal way as if I were going to display each record and then calculate the averages and totals in php before displaying. However before I go to the trouble and no doubt hours of tinkering that it will take for an amateur like me to get it working, I wanted to know if it may well be worth setting up a view instead and just retrieve the information from that with a simple select command?
The data that it will be working the avergaes and totals out for is updated weekly and in an ideal world I would want my averages and total to be year to date until the end of the first year, at which point a total will continue running but a new year to date would commence.
Any ideas or suggestions would be great.
Alan.

I would think it is faster to let PHP do the calculations. I use PHP for this on my sites, though I've not done any speed tests myself...

Related

PHP - Updating data yearly. -Process advice-

I have made an employee management system as such, and it calcualtes leave days. Although I am trying to work out how to make the leave days update yearly according to the staffs starting date.
I think I know how I will do this easily. Except it will involve the software being open on that date.
How would I account for each day, without the software having to be open?
Heres the process i was thinking:
-Loop through array of staff data and determine if starting date matches current date.
-If it matches then add an ammount of days to their leave days
I know this is very basic, and it has some flaws. Here are the flaws i am thinking:
-If the page were to be refreshed, or page opened again then it would add x2 of the staff days.
-Also, if the software was not to be opened on this day then the staff would not get the added leave days.
How would I get around this issues? I feel like it should be an easy thing, but I cant think on how to do it properly.
Any help would be greatly appreciated,
Create your php script I'll call it leavetime.php
Then if you have c-panel on your server, there is a thing called cron, in there you can select the time to call it. typically it will be something like this for the command
usr/bin/php -q locationof/leavetime.php
https://www.youtube.com/watch?v=ZAbefcWLxrw
As for the actual php code to do this, it's to broad a topic to cover in a single question.

PHP, Calculating and Displaying Daily, Weekly and Monthly Stats

I am a newbie with PHP and therefore this is more of a conceptual question or maybe even a question about 'best practices'.
Often, I see websites with stats drawn from their database. For example, let's say it is a sales lead website. It may have stats at the top of the page like:
NEW SALES LEADS YESTERDAY: 123
NEW SALES LEADS THIS MONTH: 556
NEW SALES LEADS THIS YEAR: 3870
Obviously, this should not be calculated everytime the page is displayed, right? That would potentially be a large burden on the server? How do people cache this type of data. Any best practices? I thought I writing a CRON jobs that would calculate it on a daily basis and insert to a database. What are your ideas? Thank you!
You can calculate it once and then store it in a xcache. Here, however there doesn't seem to be a need for a cron. The query can run one time and store the result in xcache. Important thing here would be to set the expiration time of the stored value according to your use case. For eg. if you need to store daily stats like above, set the expiration time to be a few hours. In case of data which gets updated every minute, you can set the expiration time to be a few minutes.
Something like this.
$newSalesLeadYest;
if(xcache_isset("newSalesLeadYest")){
$newSalesLeadYest = xcache_get("newSalesLeadYest");
} else{
$newSalesLeadYest = runQueryToFetchStat();
//Cache set for X secs
xcache_set("newSalesLeadYest", $newSalesLeadYest, X);
}
What you need is to come up with a caching strategy.
Some factors to help you decide:
How frequent does the data change?
How important is the current values - is it ok if it's 1min, 1hr, 1day old?
How expensive, time wise, is loading fresh data?
How much traffic are you getting? 10s, 100s, millions?
There are a few ways you can achieve the result.
You can use something like memcached to persist the data to avoid it being generated each request.
You can use http caching and load the data client side using javascript from an api.
You can have a background worker (eg. run by cron), which generates the latest figures and persists to a lookup database table.
You could improve the queries and indexes so that getting live data is fast enough to do every request
You could alter you database schema so that you have more static data
From the 3 examples you gave, 3 simple counts should not be expensive enough to warrant complex caching systems. If you can paste the sql queries, we can help optimise them.
The data sounds like it will only get updated once per day, so a simple nightly cron "flatten" query would be a nice fit.

Checking database for changes to a field over time period

not sure if this is possible to do but...
i have a follow function on my cms and would like a form of 'trending' posts that would allow me to show the posts that have gained the most followers over the past 7 days or so.
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Or whats the best way around this? should i possibly be creating a script that will run a query on all the blogs, count there followers and store that in a DB field? I would make this happen every 7 days and run a check against this number and then the current follower amount?
Is this possibly over complicating things?
Thanks, Craig.
It looks like you are headed towards a Cron Job that will run periodically
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Yes, save your data in a table just for this purpose. Then setup a script that checks this special table in your database where the stats are kept, and chooses the top ones. This table get's updated everyday, say at 00:00. With the data saved, you may query it to find out as much as you're saving in it
TLDR: this is kind of a project in itself, but I'm no expert

PHP: Making a simple screen transition Dashboard

im planning to make a Simple Screen transition Dashboard. I am going to make several stored procedures that returns columns to be displayed between transitions.
My question is what would be the best way to do this in PHP, since this is the only accessible language to be used with MSSQL and that doesnt need license.
I was thinking to make a hidden Pane(Frame) that handles the delay and loop that loads the Main Page. But im worried that between transition it will take time to Load and might not be a good presentation.
Any tips?
Sorry.. Simple Screen Transition..
Ex. First page, shows columns (customer, number of transactions, total sales)
Second Page, Pie Chart of Last Month vs This Month
Third Page, shows columns (seller, number of transactions, total costs )
Fourth Page, shows the Bargraph of Costs for this years months vs last years months
and so on.. like 10 of like this.
I was adviced on making like an advertisement of sort. Like an Infinite Loop of Power Point presentation that does real time query as the Data is updated every minute.
Hope I did some clarifications, if it didnt please let me know.
FYI. The client PC that will handle this will only have the local webserver ( i will use xampp) and will connect to MSSQL thru network, they wont have access to the internet for security reasons that I dont know.
Let your php print them as normal html (nothing special there).
For your panels, have a page with div containers and use AJAX to load your php intermittently (JQuery and JQuery UI would be the easiest. Make a JavaScript function that uses JQuery delay() and then calls itself to set up a timer that loads the pages into the div containers using $.ajax()

PHP / MySQL timetable comparison

I have an idea for a project that I am currently working on, I am however struggling to find a way to implement this in PHP/MySQL.
I have a database with a list of activities that a tattoo parlour has, basically he works from 9 to 5 and when an event is added it is added to the database using date and time
| --- DATE----- | TIME |
2012-11-02 ---| 10:00:00
What I am trying to do is list all the times and dates within the tattooist timetable in the following week that does not already have events going on, this will also ensure the tattooist doesn't double book too!
any assistance will help guys, any assistance will help
The best way to create the application is to store each scheduled event into a database and "black out" dates that are already taken by the applications "events" table (per say)... You will probably want to do your event creation checks during the point that the event would be created or written to the mysql db.... You will probably want to have an events table with a start and end time for each event.... when you go to schedule an event (assuming user input dictates event time and date) just write a mysql query that selects the event where the DATE(a.lastevent) = DATE(NOW())... let me know if you have further questions
Edit:
I would build the front end of the application calendar style using a JavaScript interface. In the long run it will be much more user friendly and much easier to create, I've implemented them in a day.
The framework I suggest using is ExtJS, there are many tutorials and extensions for calendars, I know that ExtJS 3 has a calendar built into it and you can view a tutorial on that here: http://www.sencha.com/learn/using-calendar
Additionally, if you decide to use ExtJS4, you can use this plugin: https://github.com/bmoeskau/Extensible/downloads or this one http://www.sencha.com/forum/showthread.php?142488-Extjs-4-based-open-source-Calendar-application
The second link is open source, the demo looks to be Spanish (it does what you need and more), but if you use Chrome you'll get a bar at the top asking if you want the page translated, click yes, it does a great job. From there you can do find and replaces to get it to English.
The ExtJS 3 tutorial will show you how to interface the calendar with a PHP page so that you can update your MySQL database via AJAX. It is much simpler than you might think, and you'll save a ton of time trying to code it from scratch.
Sometimes it depends on how long each scheduled event might be. Are you adding these events with just a timestamp or are you also tracking how long of a block is being assigned?
you could have an appointment set using a start and stop time and some math..
I have a annoying tendency to use a epoch timestamp instead of a mysql date time. then you can make calculations to see if an appointment overlaps or find the gaps. just have to learn to count in seconds or provide math to adjust :).

Categories