开发者

Two Part Question on Creating a Simple Page Counter in PHP

开发者 https://www.devze.com 2023-03-14 16:58 出处:网络
1) How do I add a simple page counter to a PHP page and insert the values in a MySQL table?Also I would need the MySQL table value to be updated with each new visit.

1) How do I add a simple page counter to a PHP page and insert the values in a MySQL table? Also I would need the MySQL table value to be updated with each new visit.

2) The trick is that the PHP page is a template for a variety of user generated landing pages. For example, I would like each of these pages to have their own separate counters:

examplesite.com/template.php?getvalue=bob

examplesite.com/template.php?getvalue=sam

examplesite.com/template.php?getvalue=samantha

My impression is that if I put the counter on the "template.php" file then it will add up all the visits from each user to a grand tot开发者_Go百科al. The output that I would like is to have each user only get counts for the individual landing page.

So, if there are a total of 12 visits, dispersed as follows:

examplesite.com/template.php?getvalue=bob  had 4 visits

examplesite.com/template.php?getvalue=sam  had 2 visits

examplesite.com/template.php?getvalue=samantha  had 6 visits

then I would want bob's page counter to read as '4', sam's as '2' and samantha's as '6.' Am I correct in assuming that if I just put the counter on template.php that each user's landing page would read as '12?' Do you have a solution for an easy way to fix this?


That's pretty simple:

pdo::prepare( 'UPDATE counter SET hits = hits+1 WHERE value = ?');
pdo::execute($_GET['getvalue']);

if ( pdo::rowCount() == 0 ) {
  pdo::prepare('INSERT INTO counter (?,0)');
  pdo::execute($_GET['getvalue']);
}


Well, just as you are able to separate the requests for bob and sam when generating the page, you can do the same for the counter?

You'll probably do something with $_GET['getvalue']. Just use that value (escaped, paramterized etc) in a query to update your counter....

 UPDATE yourtable SET count = count + 1 WHERE pagename = ?

and then bind the getvalue...


You would want to have a row in your MySQL table for each user. When you go to update the table, update the appropriate row for that user by reading the getvalue. Then do the same for displaying the user's page counter.

0

精彩评论

暂无评论...
验证码 换一张
取 消