Php : Avoid database queries for each visitors - php

Today multiple questions came to me about how web servers work with a basic code of request in database and display to the front.
For the example of a blog where we want to display our posts
I don't know any other way than
Query the database then stock the result in a php variable
Foreach the variable in the front to display the different contents
as simple as that but the question is :
Does the database query each time a visitor browse the web page supposed to display our posts ?
For me it has always worked as this. But now I'm asking myself why do we make requests on a table that we (the writers of the posts) update like one time each week everytime we visit the page ? In the case of a blog with thousands daily visitors while we update it every week, Do we query the database that much time ?
The question is the same for the php process that "foreach" every posts which require a little bit more process for the server before displaying the web page. Multiplied by the number of visitor.
If it really works as I think, how do we avoid all of these ?

Its sounds like you want to use Caching - there are simple ways to do this, and libraries/products that can help you see this article and this library

Related

Building webpages that are indexed for mutiple items and such

I'm moreless curious about webpages that are indexed. An example is gamestop. When you are looking through their product lists they have indexed for multiple tabs like 123456 on the near the bottom of the page. I also notice this with many search engines and pages selling products. I know it sounds dumb to ask a question like this, but I've always been curious how to code a mutiple indexed page like the example listed above. I hope you fellow coders out there could provide some sample code to demonstrate this idea would be perfect. Plus, it could help me in future to see just how it is done. Please provide any info you can. Thank you :)
Actually the INDEXING you are referring is called Pagination. Whenever you have multiple items/Records and you want to show only some of them in one page and rest on the other. e.g. you have 100 records in the database and you want to show all of them but loading all at once will slow down your page. So what programmers do is, they use pagination.
What they do is , They will show first 10 records on one page. if you have 100 records in database. Then first page will show records from 1 to 10, 2nd page will show 11 to 20 and so on till the end.
use reference
http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
or
Simple PHP Pagination script

PHP displaying page views for gallery style pages

This is really a point me in the right direction question. What path should I take if I want to display the number of page views each gallery page receives?
Retrieve Google Analytics Data via PHP, or
Capture the page views directly on my pages with my own PHP and mySQL setup?
Seems, like number 1 would be the better choice. I just don't know how difficult this option will be. Any insights on this?
Option 2 is definitely simpler.
If you do figure out how to get the page results out of Google Analytics, they will not be up-to-date. It takes Google at least several hours before the page views show up.
That also depends if You want to have info on more questions (statistics). I often use custom setup to track such things cause I can later use some data to do statistics about users actions.
For example, You want to give users (owners of photos) info about witch users (male or female, 30+ or below 30, and so on) are viewing. How many guests, how many registered users. There are tons of data You can retrive by building custom system.
It all depends on what You want to have at the end.
I suspect that if You want to have only raw data about number of users You coud somehow parse data from Google Analytics as well.
And as posted below. Google have to take time to update, custom setup not.

Refresh page and parallely analyze mysql

I am working on e-commerce project. Book Section has 60,000 books and 37,000 authors in my mysql tables.
here is the link of book table structure
Book page takes 13 seconds to load-Horrible. I want to know that is there any tool that Give me real time analysis. It mean, I refresh page and I can see total number of queries executed when page load completes. So I can analyze and write time effective queries.
Additionally, Please guide me little for how can I improve mysql performance for such e-commerce project. Project's Index page displays 3-4 items from all section like camera, Apparels, Mobile etc. So in turn fetch from almost all categories.
Please help.
Thank you In advance.
Are you limiting your result set of the data returned? Please remember to post the query that you are running.

PHP, MySQL: Display only required parts of my website in sister website

Now I have my website built on PHP & Mysql. Consider this like a forum. Now when a user posts a reply in my website 1 (ex. www.website1.com), I want to be able to show the starting thread and it's related replies in a sister website of mine. I want to do this in a way that it does not show the rest of the page & other page contents (like logo etc.). I don't think iframe would be a solution because an iframe would embed the whole page and the users visiting my sister website (totally different domain i.e. www.website2.com) would be able to see all the page contents, like logo etc. I want to avoid that. I want to make them see only limited information from website 1 and only the info. that I intend.
I hope that makes sense. In a way, you could say that I am trying to replicate my 1 website, and show only a limited part of it. Users browsing 2nd website can post a reply in the 2nd website and it should automatically be posted & visible to the visitors of the website 1. Users of website 1 should not know that a user of website 2 has posted it. They would feel that some user from website 1 has posted it. Do I have to use 2 separate mysql DB or just 1? I think it would be problematic if I am trying to use different DB. I also feel I might have to face DB connectivity issues as I can connect to only 1 DB at a time.
It's basically like users of website1.com should feel that they are replying to users of website1.com & users of website2.com should feel that they are replying to users of website2.com. (I need it this way to bridge the gap between them). At the same time I want to make the front end of the websites different so that they don't feel that they are replying to some other users outside the domain. These websites would be under my control and I will have access to the source code at any time. If I need to change the source code, these changes are welcome.
Is this really possible?
Thank you in advance.
I'd recommend generating RSS (might be runtime) and using it on the sister website. If RSS is not suitable for your needs, you can create your own XML-based format (or any other :) )
Make two forums which use one database. Both websites would put new messages in the same database.
Make an API for website1, so that website2 can retrieve and post messages on the forum. Website2 would do a HTTP request to website1, which returns XML or JSON, so that website2 can request a list of posts that it can display.
Have both sites connect to the same database and display the content they pull in whatever way is appropriate for the particular site. Each site can only pull the fields relevant to that site.
If the idea is to have two websites with the same data but different presentations, then you would want to simply share a single database between them - assuming they are hosted in the same place and can both get at the database.
You can then just create different PHP pages that both access the same database in the same way but display the data differently.
The best way to do this would be to have a shared library of functions or classes that both sites use to manipulate the data. You would then build a different "presentation layer" on top of that for each site.

counting a Widget?

Hello I've recently added a weather widget for people to add to their site.
Is basically a php page that people can link to.
My question is can I somehow put a counter or something to count how many people are using my widget?
my widget is located here http://www.site.com/widget/ it's in farsi language.
Well, it depends - do you want to know how many people have used your widget in their web site, or how many people have watched web sites that use your widgets? These numbers are completely different.
For the later option, jonstjohn's method would work great: For easy implementation and lots of features I recommend the Google Analytics way - they have really fancy graphs that show a lot of interesting information.
If on the other hand you want to count how many web sites are using your widget then you can do as follows:
Create a table in your database with a varchar column. Put a unique index on that column.
In your code, read the $_SERVER["HTTP_REFERER"] parameter to get the URL that called your widget.
Now you want to strip just the domain part from that URL as a web site would probably put your widget in their template so it will be available in all of their pages. So if you want to count web sites and not pages in web sites, then do something like
$domainParts = explode("/", $_SERVER["HTTP_REFERER"]);
$domain = $domainParts[2];
Now insert the domain you found out into your table. If this web site has already called your widget once then the insert would fail with a unique constraint error - just ignore the error (for example by using "#" on your insert command, like #mysql_query("INSERT INTO...") ).
To know how many web sites are using your widget, simply count the number of rows in the table.
You can place tracking code on the PHP page. Possibilities are:
Google analytics code
Record each time the .php page executes by inserting a row in a database
I'm sure there are others, but those should work.

Categories