thme01 Posted October 27, 2011 Share Posted October 27, 2011 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 https://forums.phpfreaks.com/topic/249936-how-can-i-make-this-hit-counter-code-unique-for-each-video-id/ Share on other sites More sharing options...
PaulRyan Posted October 27, 2011 Share Posted October 27, 2011 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 https://forums.phpfreaks.com/topic/249936-how-can-i-make-this-hit-counter-code-unique-for-each-video-id/#findComment-1282812 Share on other sites More sharing options...
thme01 Posted October 27, 2011 Author Share Posted October 27, 2011 but would that be able to match up to the hundreds of videos i have? Link to comment https://forums.phpfreaks.com/topic/249936-how-can-i-make-this-hit-counter-code-unique-for-each-video-id/#findComment-1282819 Share on other sites More sharing options...
PaulRyan Posted October 27, 2011 Share Posted October 27, 2011 Yes. It will automatically add a video's hits to the table, then if it exists update the table and add 1 hit to the hits value. Link to comment https://forums.phpfreaks.com/topic/249936-how-can-i-make-this-hit-counter-code-unique-for-each-video-id/#findComment-1282821 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.