TheFilmGod Posted March 18, 2007 Share Posted March 18, 2007 Hi, I have been working on a page view counter. Now I haver researched google five times already and can't find a script that calculates a specific page's hits (not the whole site's page htis). So I made my own. Here is the code. I have tried it and it doesn't seem to work. Can you please help me? <?php // Page ID $page="1"; ?> <?php require("db/connect.php"); // Already connected to mysql $query = "SELECT * FROM `table` WHERE id='$page'; $result = mysql_query($query); $cnt = $row["count"]; $cnt++; mysql_query( "UPDATE table SET count = '$cnt' WHERE id= $page";"); // echo the number of htis echo $cnt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/ Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 try: <?php // Page ID $page="1"; ?> <?php require("db/connect.php"); // Already connected to mysql $query = "SELECT * FROM table WHERE id = $page"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $cnt = $row["count"]; $cnt++; } mysql_query( "UPDATE table SET count = $cnt WHERE id = $page"); // echo the number of htis echo $cnt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-209982 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 I tried it and it didn't work. Here is the code I tried: <?php // Page ID $page="1"; require("connect.php"); // Already connected to mysql $query = "SELECT * FROM view WHERE id = $page"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $cnt = $row["count"]; $cnt++; } mysql_query( "UPDATE view SET count = $cnt WHERE id = $page"); // echo the number of hits echo $cnt; echo Hello; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210002 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 did you get an error or anything? what happened? ...also your hello needs to be echo "Hello"; Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210005 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 No. There was no errors. Nothing happended. It is a blank page with "hello." The test page is at: http://www.TheFilmGod.com/test/vote Maybe my mysql table is completely messed up, I don't know. Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210010 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 what does your DB structure look like? Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210018 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 Thanks for all your help so far. I greatly appreciate it. Here's the sql code I used for mysql: CREATE TABLE `view` ( `ID` bigint(20) NOT NULL auto_increment, `count` varchar(6) default NULL, PRIMARY KEY (`ID`), KEY `count` (`count`) ) TYPE=MyISAM AUTO_INCREMENT=1 ; # # Dumping data for table `hitcounter` # I'm not sure if the table is structured properly. Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210019 Share on other sites More sharing options...
shaunrigby Posted March 18, 2007 Share Posted March 18, 2007 <?php // Page ID $page="1"; require("connect.php"); // Already connected to mysql $query = "SELECT `count` FROM view WHERE `id` = '" . $page . "'"; $result = mysql_query($query) or die("ERROR: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $cnt = $row["count"]; $cnt++; } mysql_query( "UPDATE `view` SET `count` = '" . $cnt . "' WHERE `id` = '" . $page . "'") or die("ERROR: " . mysql_error()); // echo the number of hits echo $cnt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210055 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 Try this: Drop the old table and use this one: CREATE TABLE `view` ( `page_id` int(11) NOT NULL default '0', `page_count` int(11) default NULL, PRIMARY KEY (`page_id`) ) ENGINE=MyISAM; And use this as your php: <?php // Page ID $page = 1; require("connect.php"); $result = mysql_query("SELECT page_count FROM view WHERE page_id = $page"); $t_count = mysql_num_rows($result); if($t_count > 0){ $cnt = mysql_result($result, 0, 'page_count'); $cnt++; mysql_query("UPDATE view SET page_count = $cnt WHERE page_id = $page"); }else{ mysql_query("INSERT INTO view ( page_id , page_count ) VALUES ($page, '1')"); $cnt = 1; } // echo the number of htis echo $cnt; ?> It worked for me Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210082 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 Thank you soo much for all your help! I'm going to try it out and see if it works Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210095 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 You can use something like this: $result = mysql_query("SELECT page_count FROM view WHERE page_id = $page"); $t_count = mysql_num_rows($result); if($t_count > 0){ $cnt = mysql_result($result, 0, 'page_count'); echo "Threre are $cnt page views."; }else{ echo "There are no page views."; } Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210100 Share on other sites More sharing options...
TheFilmGod Posted March 18, 2007 Author Share Posted March 18, 2007 I'll try that one out too. But the one before that you gave me worked perfectly! Thanks soo much. Thanks to everyone else to help me out on getting it to work. You don't know how relieved I am that it finally started working. You can check out the test out at: http://www.TheFilmGod.com/test/scripty Now I'm going to add it into my currently running pages on my site. Check out TheFilmGod.com out soon to it action (underneath its movie it will have total views: (Number of views) - Thanks to all you)! Again, without phpfreaks and this forum I don't know what I would do. Is there any way to recommend those who helped me? Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210111 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 I'm glad we can help you out. We do not have a rating system anymore. We started with one when we converted over to this board (SMF) but some people abused it a little too much so the mods took it off. But most people in here know who's good and who's not. You'll know that too when you are around a little bit longer. Whenever you need something answered just post and you shall receive Quote Link to comment https://forums.phpfreaks.com/topic/43247-solved-simple-page-view-counter/#findComment-210114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.