nuttycoder Posted August 7, 2007 Share Posted August 7, 2007 Hi I need some help am trying to count how many times a tutorial has been viewed in total for all tutorials. I can count how many tutorial views they've been from the rows but as my tutorial views increment each time the tutorial is viewed its not accurate. What I need is to be able to count the values from the table from each row and add them together. here is my table scheme: `tutorialID` int(4) NOT NULL auto_increment, `tutorialTitle` varchar(255) NOT NULL default '', `tutorialDesc` text, `tutorialCont` text, `category` varchar(40) NOT NULL default '', `tutorialDate` timestamp NOT NULL default CURRENT_TIMESTAMP, `tutorialviews` varchar(30) NOT NULL default '0', PRIMARY KEY (`tutorialID`) Here is the code am using to count <?php $count_sql = "SELECT * FROM tutorials WHERE tutorialviews"; $count_result = mysql_query($count_sql); $count = mysql_num_rows($count_result); ?> <p>Total Views <?php echo $count;?></p> This is the code I use to update my views when veiwing a tutorial //first update views $update = "UPDATE tutorials ". "SET tutorialviews = tutorialviews + 1 ". "WHERE tutorialID = '$tutorialID'"; mysql_query($update) or die('Error : ' . mysql_error()); at present I have 80 views on one tutorial 30 views on another just not sure how to add them together to display the total views. Any advice would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/ Share on other sites More sharing options...
NArc0t1c Posted August 7, 2007 Share Posted August 7, 2007 mysql_query("SELECT SUM(tutorial_views + other_tutorial_views) FROM table"); Is that what you wanted? Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317451 Share on other sites More sharing options...
nuttycoder Posted August 7, 2007 Author Share Posted August 7, 2007 okay am sure am doing this completly wrong... <p>Total Tutorials <?php echo $count;?></p> <?php $result = mysql_query("SELECT SUM(tutorialviews + tutorialviews) FROM tutorials"); ?> <p>Total Views <?php echo $result;?></p> as I get "Total Views Resource id #6" instead of the value Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317463 Share on other sites More sharing options...
Daniel0 Posted August 7, 2007 Share Posted August 7, 2007 Try <p>Total Tutorials <?php echo $count;?></p> <?php $result = mysql_query("SELECT SUM(tutorialviews + tutorialviews) FROM tutorials"); $row = mysql_fetch_row($result); ?> <p>Total Views <?php echo $row[0];?></p> instead. Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317465 Share on other sites More sharing options...
nuttycoder Posted August 7, 2007 Author Share Posted August 7, 2007 That works great! Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317467 Share on other sites More sharing options...
nuttycoder Posted August 7, 2007 Author Share Posted August 7, 2007 when I tried the following code I thought it worked great but it some how comes to the number 186 but when i look in the db and add the value of each tutorialviews up its 93 so i don't know where its getting 186 from so i still need a way to add all tutorialviews up so i can get the total number. $result = mysql_query("SELECT SUM(tutorialviews * tutorialviews) FROM tutorials"); $row = mysql_fetch_row($result); echo "<p>Total Views $row[0]</p>"; Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317738 Share on other sites More sharing options...
NArc0t1c Posted August 7, 2007 Share Posted August 7, 2007 Try: mysql_query("SELECT SUM(tutorialviews) FROM tutorials"); Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317745 Share on other sites More sharing options...
nuttycoder Posted August 7, 2007 Author Share Posted August 7, 2007 excellent its showing the right number of views now thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/63704-solved-count-values-rather-then-rows/#findComment-317748 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.