Reputation: 23
can anyone tell me how to display (individual) page views from users. I have 5 web pages in my website and when I place the following php code in all pages it shows the same no of page views instead of no of views per page.
Note; I'm still a newbie in web designin.
Here is my code, counter.php
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("myinfo_db") or die(mysql_error());
mysql_query("UPDATE counter SET counter = counter + 1");
$count = mysql_fetch_row(mysql_query("SELECT counter FROM counter"));
echo "$count[0]";
?>
Upvotes: 0
Views: 125
Reputation: 4025
You have to specify for which page you're increasing the page count. Your table should look something like: id - primary key & counter - the current page count
Each page will be assigned a unique id and all operations will done against that id. For example:
$currentPageId = 1; // let's say this is index.php, for other pages you have 2, 3 and so on
// update page count for the current page
mysql_query("UPDATE `counter` SET `counter` = `counter` + 1 WHERE `id` = $currentPageId");
// fetch the page count for the current page
$count = mysql_fetch_row(mysql_query("SELECT `counter` FROM `counter` WHERE `id` = $currentPageId"));
Good luck!
Upvotes: 0
Reputation: 1439
Your counter
table should have at least 3 columns: id, page, and views.
So when a user visits, lets say homepage, then update views for that page using
mysql_query("UPDATE counter SET views = views + 1 WHERE page = 'homepage'");
then when displaying views for homepage only, you use:
mysql_query("SELECT views FROM counter WHERE page = 'homepage'");
I hope that helps.
Upvotes: 2