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.
Related
I want to make a PHP page that will changing it's content based on some sort of an ID.
The idea is: the index page will have 4 squares, the content of those 4 squares will be the top 4 records in the database, they obviously have their own IDs in the DB.
What I want to happen is when I click on one of the squares it will pass the ID to another PHP page that will get all the details about it in the page.
To be more clear:
Lets say it is a cars website, the 4 squares would be an image of top 4 cars in the database with the IDs 1,2,3 and 4 respectively, when I click on the car's image (lets say 1) I will be directed to a PHP page called CarInfo.php
what i want to happen here is for the ID of the square to be passed to this page (maybe page will appear something like carInfo.php?id=1) and the page will load all the information from the database where the ID will match the recieved ID (in this case the record with ID = 1).
The problem is I don't even know how to start doing it... How can I pass the ID? How can the other page receive it? And can I use a variable to pass it to so I can use it in the query carteria? If so how?
Note: there will be 1 PHP page that displays the information ONLY (aside from index), all content will be dynamic.
very easy. exactly as you have said. you will use address with parameter, like
carInfo.php?id=1
and on carinfo page you can use php get variable http://php.net/manual/en/reserved.variables.get.php
$_GET["id"]
it will give you selected id, and you can then use is in database
In the first page you have to do something like this for each car:
Click Here
Click Here
Click Here
Click Here
Then, in show.php you need to write a code like this to recive information form the provided ID if the database you're using is MySQL:
<?php
$db = mysqli_connect('localhost','USERNAME', 'PASSWORD','DATABASE');
$result = mysqli_query($db, "SELECT * FROM YOUR-TABLE WHERE id = '{$_GET["id"]}'");
while($row = mysqli_fetch_assoc($result)){
echo "Color : {$row['color']}";
// ...
}
?>
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.
I write some simple article scripts. Now I want add some article counter.
If there have 3 possibility it should make an article counter.
the article has been opened for read.(only one whole article per page, it will count 1 time)
the article has been searched in a content search list(with title and short content description 5 items per page, it will count 1 time. then if open to read whole article content, it will count another 1 time.)
the article has been showed in the home page by Random (with title and short description, it will count 1 time)
Is there any good suggestion how to do these better?
Which database design is better? put article and count number in one table? or make two tables?
Can anyone recommend me some php article counter script, if the main rules write into a file like class.php then include into my every page.
Thanks.
If you are counting the number of hits on an article, create a column and add one to it every time somebody accesses the page.
Something like:
$sql = "UPDATE table SET count=count+1 WHERE id='$id'";
mysql_query($sql);
Would increment the column count in the table table by 1. Then you could just retrieve that value.
What you could do is use the database that auto increments and just increment that value when the page is viewed, and display that value if you want.
Here is how you can setup the auto increment.
Then update the "value" (you don't have to actually update anything it will AI)
mysql_query("UPDATE COUNTER SET HITS = ''");
Then just display the view
$result = mysql_query("SELECT * FROM PAGEVIEWS");
while($row = mysql_fetch_array($result))
{
echo $row['COUNTER'];
}
This is off the top of my head - it should work.
I have a table posts with columns post_id, user_id, post.
When a user visits his page, I retrieve a list of his posts (select post from posts where user_id=$uid). He can then add one or more posts to the table through a form.
Is there any way I can retrieve these newly-added posts without querying the entire table again?
I'm particularly interested in using this with jQuery to auto-refresh his posts periodically (like YouTube comments when 'auto-refresh' is selected, for example).
Thanks, Albert
If I were you, I would keep track of when the posts were made, e.g.
post_id, user_id, post, made
You can use AJAX to ping a script to check for posts made since the last ping~
query you db like this:
"select * post from posts where user_id=$uid order by post_id desc limit 0,20"
this will get the most recent 20 posts.
Assuming the 'post_id' field is an auto increment int, you can keep track of the largest value ID when you first load the page. For subsequent jquery ajax requests, just do a
SELECT * FROM posts WHERE user_id = '{$user_id}' AND post_id > '{$last_id}' ORDER BY post_id ASC
If the user is submitting posts on the same page that you want to update, why not just update the page as soon as the post is successfully submitted (via ajax)? You avoid re-fetching information you already know, and you avoid re-querying the database all together.
You would dynamically add a post to the page as it is submitted.
function onPostSubmitted(post){
// AJAX submit of the post
$("#posts").prepend( $("<div />").text( postData ) );
}
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']