Jump to content

How can i make this hit counter code unique for each video id?


thme01

Recommended Posts

How can i make this hit counter code, so that it doesnt update the views on ALL of my videos. i want it to be unique for each video id. any ideas?

 

<?php
                $querySelect = mysql_query("SELECT * FROM `mydatabase` WHERE `counter`");
                $row = mysql_fetch_assoc($querySelect);
                $counter = $row['counter'];
               
                if (empty($counter)) {
                       
                        $counter = 1;
                        $insert = mysql_query ("INSERT INTO counter VALUES ('counter')");
                }
               
                        $add = $counter+1;
                        $insertNew = mysql_query("UPDATE mydatabase SET counter=('$add')");
                                echo "Video Views";
                                echo ":";
                                echo" ";
                                echo"<br />";
                                echo "$counter";
                ?>

www.game4vids.com if you pick a video you can see that it changes the views on ALL the videos i have.

Link to comment
Share on other sites

You would need to make a new table in your database lets call it "hit_counter".

 

That table would look like this:

id - unique, INT, 10

hits - INT, 10, DEFAULT 1

 

Now on the video.php page, you would have something like:

//## Get views
$getViews = mysql_query("SELECT `hits` FROM `hit_counter` WHERE `id` = {$videoID}");
if(mysql_num_rows($getViews)) {
  $result = mysql_fetch_assoc($getViews);
  $videoViews = $result['hits']+1; 
} else {
  $videoViews = 1;
}

mysql_query("INSERT INTO `hit_counter` (`id`) VALUES ({$videoID}) ON DUPLICATE KEY UPDATE hits = hits+1");

echo 'Total Views: '.number_format($videoViews);

 

Then whenever the page is loaded, it will either insert the video ID number into the table or update the table and add a hit if the video ID number already exists.

 

Regards, PaulRyan.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.