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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.