Count hits for post using php and mysql - php

How i can develop a hit counter for my posts.
I have a mysql table with following cols
id title body hits
how i can increment hits by 1 whenever a user read post entry.
Please help.

In the page where you show your post you should update database.
I.e. you show post post.php?id=53 so in post.php you have to do
'UPDATE table SET hits = hits + 1 WHERE id = ' . (int) $_GET['id']

Related

Getting value of a button and use it in another page in php

I have designed a table in index.php page that gets data from database, with these columns:
Full name, email, address (of customers) and the last column of each row has a edit button with <a ></a> tag, that its href links to another page :
href = "edit.php?id= ' " .$row['customer'] . " ' "
Now, edit.php page includes a form that admin can make changes in customers data.
To do this I need to get the id of each customer while clicking on the "edit" button and direct to edit.php page, to show their information in the form as a default values (that admin can change them) .
How would I do this?
I hope I make myself understood.
In the edit button pass the id as a parameter in the url and then retrive the id from the url in edit.php and fetch the user from the table again based on the id and then make changes in the data and post the user data with new data back to the mysql table
As you are using anchor tag Pass the id like this
There should be a column id on the table and just to make it easy make this column auto Inreamentable
Hope it works
#faro621, You can get the id in edit.php page and run the query for select the customer data by that id and show the information.Like this-
$q = 'Select * from table_name where id ="'.$_REQUEST['id'].'" ';
Execute this query now you will get the data on the same page.
Hope it will help you.

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:
<?php echo $row['title']; ?>
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.
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
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.
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.

add a field to each record that shows when and how many people visited it?

I have a mysql table and in that is individual records. What I'd like to do is be able to have a field for each record that shows how many people have visited each record.
For example:
Jame's Record - Visited 10 times
Joe's Record - Visited 3 times
Is this possible at all?
Solution 1:
Add a field in user row named view_count and on every view update this field with +1 using
UPDATE table SET field = field + 1 WHERE [...]
Solution 2:
Create a lable view_log
Fields id,table,row_id
And on every view function insert a new row into this table with table and row id.
And when u wana get count just run
Select count(I'd) from view_log where table = 'users' and row_I'd = 'x'
Then u will get counts :)
If you want to monitor update,insert, delete then u can use mysql trigger to do it.
It is possible, You can simply do this using session.Make a view field in table that record the no of times record has been viewed.
Make session for new user's ,Whenever new user is on that user's record page increment that view field by 1 and if same user is visiting that record destroy the session.

Two Part Question on Creating a Simple Page Counter in PHP

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 total. 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.

Increment a record's score when user clicks "vote" w/php & mysql

I'm creating a jokes website. Each joke is stored as a record in mysql db. I'm trying to develop a vote link which increments the joke record's "score" column when a "vote" link is clicked.
I'm just hoping for some general direction as to how to do this... I'm planning on using jquery's ajax to send the joke's id to a php page to process. I'm just not sure how exactly to process it... something like this?
$id = $_POST['id']
mysql_query("UPDATE jokes SET score='++' WHERE id='$id'");
Each joke will be within a uniquely identified <div> (the id= is generated with the record's unique id).
Thanks a lot!
mysql_query("UPDATE jokes SET score= score + 1 WHERE id='$id'");
As per "The Scrum Meister"'s suggestion
mysql_query("UPDATE jokes SET score= score + 1 WHERE id='".mysql_real_escape_string($id)."'");

Categories