Wikipedia http://en.wikipedia.org/wiki/Main_Page , has sections like:
Todays featured article,
From the news,
Did you know etc.
Say in my page, I want to get the main highlights from the database table(s) (multiple databases possible), what is the best possible way to query? I mean create separate connections and then query or use multiple queries? Is it better to use PDO for this purpose?
And how can I make a particular section update without refreshing the page say every 10 min? Is the code going to be complicated?
Can anyone please let me know.
Thanks.
yes the code is going to be complicated but not difficult.
If you want to use PDO then you should use it, it depends if you want to use it or not.
First you need to decide the highlights that you want to show in the main page and then decide how to fetch this related info.
You can use multiple queries. First fetch and then display.
And how can I make a particular section update without refreshing the page say every 10 min
for this you will have to use ajax.
If you want to refresh the data, you have two options.
First of all, you could write JavaScript to run away after a set interval and get fresh data. This is a VERY BAD idea - it exposes you to browser specific bugs, it won't work on machines with JavaScript disabled, and far more importantly it means you'll be furnishing your pages with the connection details for your database (and also allowing connections to it from anywhere).
A better solution would be to use AJAX to handle this - which basically means that you serve "just the data" rather than the whole page. Again, it's dependent on Javascript, but then implementing client side behaviour without the benefit of client side scripting will never work anyway!
Martin.
#1 The todays feature is either calculated from the current date; or specified, either manually or via a randomizing script which saves todays' date together with the id, so it will stick for the rest of the day.
#2 From the news is a simple query with order by date desc from the news.
#3 Did you know says it's from their recently added articles, which would also be somewhat of an order by date desc-query.
PDO is just a wrapper for the same SQL-queries as usual, you can achieve the same result without it.
About updating a section, I'd use a cache with a 10 minute limit. This will not reload for people staying on the page for 10 minutes (for that you would need AJAX) but it will load fresh content on a timebased period. You should base your choice of whether you expect users to spend 10 minutes on the same page, or just give some news at least 10 minutes in the spotlight before they get exchanged.
Related
nowadays, I know two methods how to present limited data from database to user.
the first method is using pagination like
first previous 1 2 3 .. next last
the second method is using
Load more
like on Facebook or Twitter, but of course this is need AJAX function to load more data.
which one the best to use if we look from system performance or user convenience?
which one the best to use if we look from system performance or user convenience?
Assuming that there is no need for a user to jump to page X of results, a "load more" option will (usually) work better for both fronts.
You'll have slightly lower load on the db server, since you're not making it re-calculate how many pages it has all the time.
Your users will like the "load more" option, since they won't need to have a full page refresh and can just keep on scrolling.
Of course, neither of those necessarily applies to your exact situation. If you have a database that has baked-in pagination, or a need of your users to select a particular page, or an existing application that supported paginatino and your users prefer it, then paginagion may be the better choice.
And you forgot the third "infinite scroll" option, btw.
Well this is kind of a question of how to design a website which uses less resources than normal websites. Mobile optimized as well.
Here it goes: I was about to display a specific overview of e.g. 5 posts (from e.g. a blog). Then if I'd click for example on the first post, I'd load this post in a new window. But instead of connecting to the Database again and getting this specific post with the specific id, I'd just look up that post (in PHP) in my array of 5 posts, that I've created earlier, when I fetched the website for the first time.
Would it save data to download? Because PHP works server-side as well, so that's why I'm not sure.
Ok, I'll explain again:
Method 1:
User connects to my website
5 Posts become displayed & saved to an array (with all its data)
User clicks on the first Post and expects more Information about this post.
My program looks up the post in my array and displays it.
Method 2:
User connects to my website
5 Posts become displayed
User clicks on the first Post and expects more Information about this post.
My program connects to MySQL again and fetches the post from the server.
First off, this sounds like a case of premature optimization. I would not start caching anything outside of the database until measurements prove that it's a wise thing to do. Caching takes your focus away from the core task at hand, and introduces complexity.
If you do want to keep DB results in memory, just using an array allocated in a PHP-processed HTTP request will not be sufficient. Once the page is processed, memory allocated at that scope is no longer available.
You could certainly put the results in SESSION scope. The advantage of saving some DB results in the SESSION is that you avoid DB round trips. Disadvantages include the increased complexity to program the solution, use of memory in the web server for data that may never be accessed, and increased initial load in the DB to retrieve the extra pages that may or may not every be requested by the user.
If DB performance, after measurement, really is causing you to miss your performance objectives you can use a well-proven caching system such as memcached to keep frequently accessed data in the web server's (or dedicated cache server's) memory.
Final note: You say
PHP works server-side as well
That's not accurate. PHP works server-side only.
Have you think in saving the posts in divs, and only make it visible when the user click somewhere? Here how to do that.
Put some sort of cache between your code and the database.
So your code will look like
if(isPostInCache()) {
loadPostFromCache();
} else {
loadPostFromDatabase();
}
Go for some caching system, the web is full of them. You can use memcached or a static caching you can made by yourself (i.e. save post in txt files on the server)
To me, this is a little more inefficient than making a 2nd call to the database and here is why.
The first query should only be pulling the fields you want like: title, author, date. The content of the post maybe a heavy query, so I'd exclude that (you can pull a teaser if you'd like).
Then if the user wants the details of the post, i would then query for the content with an indexed key column.
That way you're not pulling content for 5 posts that may never been seen.
If your PHP code is constantly re-connecting to the database you've configured it wrong and aren't using connection pooling properly. The execution time of a query should be a few milliseconds at most if you've got your stack properly tuned. Do not cache unless you absolutely have to.
What you're advocating here is side-stepping a serious problem. Database queries should be effortless provided your database is properly configured. Fix that issue and you won't need to go down the caching road.
Saving data from one request to the other is a broken design and if not done perfectly could lead to embarrassing data bleed situations where one user is seeing content intended for another. This is why caching is an option usually pursued after all other avenues have been exhausted.
Ok. I didn't know how to put this question in Title so here is a quick description.
Let's say I have site with some promotional stuff to give for free (or not:).
When I have something to give I announce this on facebook and twitter etc. and people can come to website and fill quick form, couple quick questions and of course name and address.
But the problem is I have for example 20 pieces of this thing to give for free.
When you submit the form this goes automatically to database table.
I know how to display current status for this offer with some PHP (like: there's only 12 items left.hurry up!), there is also no problem with refreshing this every couple seconds with AJAX. But problem I see in here is when let's say this will become more popular and I will have many offers during short time.
I don't want database to be overloaded with queries from hundreds of people every two seconds.
Is there any way to send just one query every two seconds (somewhere on the sever?) and just somehow update value from this query in any browser currently visiting the website?
I'm not sure if this is clear question but what I'm asking is what would be the best practice for this kind of situation.
Is my concern about overloading the database even reasonable?
And extra problem...
In this particular situation - with the limit for amount of people that can participate - is there any threat that I can have strange behavior when two people will send form in exactly the same time when there is only one item left?
I would love to see any directions in this subject. Even general one will do :)
PS: No, english is not my first language :)
Thx
Can't you have one script loading from the database and save it somewhere that isn't the database (a file, preferably) and then it can be extracted from there? This will include a cronjob for that script to be run every 5 second.
So the situation I have is I'm using Wordpress and I'm trying to write a custom plugin.
What I want to do is grab an array of all the posts and show the first 25.
Then underneath that I want to have a button that says , "Show Next 25" and when I hit the button it will add the next 25 to the list of articles, and if I hit again, wash , rinse repeat till it's out of articles.
I was going to write this in straight PHP / MySQL but not sure how to incorporate JQuery into it.
My thought was to write a series of offsetting MYSQL statements but thats probably too clunkly.
My 2nd thought was to pull all the post info into one Associative array and then find a way to parse the other output into the hidden divs.
Thoughts?
I tried looking for a wordpress plugin that does this but did not find one and I tried searching for a situation like this on here, found some stuff similar but not the same.
I guess you could compare this to commenting feature where it shows the last 10 commends and you have hit a button to see the rest of the comments.
Thanks much in advance.
I would grab from the database only 25 at a time. Then I might use JQuery to do the ajax call to pull another 25 based on what has already been shown.
Use a limit 25, offset clause on the mysql. Send the next offset value to the web page as a hidden input or a javascript var. Then use JQuery ajax to call your php function that returns a JSON data of the next 25 rows to display.
So this isn't JQuery, but I like this:
http://www.kryogenix.org/code/browser/sorttable/
But I also agree w/ the thoughts, you're wasting the resources of Ajax if you do it this way. Ajax would do well here.
Here's how "I" would do it, though this is just a suggestion.
First, if you don't already have it, I would suggest something like Komodo Edit to code in. It's free, but I have the pay for for very cheap (i think my last upgrade lic was 30 bucks?). If you, buy just one copy, they'll offer you a ton of discounts if you ever decide not to upgrade. Good program for HTML5/php editing.
Second, go look at Codeigniter's Active Record. After looking this page over, you'll see very easily why I would suggest CI. CI's Active Record makes safe and secure SQL calls an absolute breeze, though for better security, I would suggest you make use of there XML filter and never "chain" Active Record calls as it shows you can do on that page.
Finally, as I said before, I would read up on jQuery's .ajax(), .get(), and .post() commands. After a stirring read, I'd probably end up using .get() to make calls to a controller containing a simple php active record function that would return the wanted update data at +25 "ahead" of what the user is actually looking at.
I am looking to display 60,000 records on a webpage with php pulling the records from a mysql database on localhost. These 60,000 records may change depending on the data input.
The records have 5 text fields and due to the sheer number of records, a significant time is taken to send the data from the mysql server to the web browser. Even on a localhost, the time taking is around 15 seconds. During this time, the page is empty.
I would like to seek professional opinion on how to either to
1. display the data in an alternative method, (which I'm not sure what method) or
2. hasten the sending of data from mysql server to the web browser using caching technology like memcache.
In the end i will be deploying the application on the internet where the lag would be immensely unacceptable (i.e. > 15 seconds).
Thank you and Best Regards!
I would suggest trying AJAX pagination. No user will be able to see and analyze 60k records at one time. You can have the php display the first x (however many fit on the average screen or two) records to fill 2-3 pages, and have JavaScript listen for a scroll change. If a user starts scrolling down, have it automatically query the next y records, and add them to the display list. Possibly also removing the records from the top of the list.
Also, adding some quick-jump links or a search feature could help, as you wouldn't want to scroll down 60k records to make changes.
This will significantly lighten the server and client load, as it would only have to serve up a couple hundred records at a time.
DataTable
You should have a look at YUI's DataTable. You should hook the datatable up to autocomplete. There is also an example how they did it in YUI2(help) but YUI3 is a lot faster.
Caching
Caching is also important. You say you could use memcached so that is very good. I am a big fan of redis(But both will work, but the nice thing is that redis is I think better suited for autocomplete). There is even a free plan of Redis To go.
Another important tip is to make sure you are getting your data as you want it displayed from the database. In other words if there is any calculation or processing that you have to do, avoid doing it in PHP code during loops. Use SQL functions to process data, name fields, etc. Databases are good at that sort of thing. Of course this may or may not apply to exactly what you're doing.