bɪˈɡɪnə
bɪˈɡɪnə

Reputation: 1085

Track post views

I am working on a project where only title of posts are shown on main page and on clicking the title, full post is loaded on another page posts.php code for this is:

<a href="posts.php?postId=<?php echo $row['posts_id'] ?>"><?php echo $row['title']; ?></a>

Is there any method to track how many times post with specific id was loaded on posts.php. In simple words I want to track how many times full post has been viewed on posts.php page, someone suggested count HTTP requests with this URL. So please guide me on this just few lines on how the thing works can help me a lot.

Upvotes: 1

Views: 957

Answers (3)

Andrey Maia
Andrey Maia

Reputation: 1

You insert a code on top of your script post.php in order to count visitors.

Something like: http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php

If you prefer Database, you just have to change your code.

Upvotes: 0

N3R4ZZuRR0
N3R4ZZuRR0

Reputation: 2422

What you have to do is create a new column in your table, suppose it's name is 'hits'. Set the default value of it to 0 while creating a new row everytime.

$id = $_GET['postId'];
$sql = "UPDATE posts SET hits = hits + 1 WHERE post_id = $id"; //Suppose your table name is posts

Upvotes: 1

Karan
Karan

Reputation: 2112

You can add a new column in your database table like view_count. When you get data from database you can update view_count like

update table_name set view_count = view_count + 1 where post_id = $_GET['postId']

it will increment view_count every time when page will load.

Upvotes: 1

Related Questions