JSHINER Posted October 21, 2007 Share Posted October 21, 2007 I have a "views" field in my table for each record. Looks something like this: ID | Views ------------- 1 | 20 2 | 50 3 | 30 What is the best way to display: "Total Views: 100" Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/ Share on other sites More sharing options...
only one Posted October 21, 2007 Share Posted October 21, 2007 mysql_query("SELECT * FROM `table` ORDER BY `field` DESC LIMIT 100"); Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-374990 Share on other sites More sharing options...
JSHINER Posted October 21, 2007 Author Share Posted October 21, 2007 Maybe I wasn't clear? ??? I want it to count the total "views" in the table. 20 + 30 + 50 = 100 Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-374991 Share on other sites More sharing options...
only one Posted October 21, 2007 Share Posted October 21, 2007 oh, i wouldn't do what you are doing the way you are Add a new row for every view with the id of the thing it's on, then just use the mysql_num_rows() function. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-374997 Share on other sites More sharing options...
Orio Posted October 21, 2007 Share Posted October 21, 2007 <?php $sql = "SELECT SUM(Views) as 's' FROM table_name"; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo "Total views: ".$row['s']; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-374998 Share on other sites More sharing options...
lewis987 Posted October 21, 2007 Share Posted October 21, 2007 hmm... try this: <?PHP $SQL = "SELECT * FROM `table`;"; $Q = mysql_query($SQL); if($Q){ $i = 0; while($views = mysql_fetch_assoc){ $num[$i] = $views['views']; $i++; } $total = count($num); $i = 0; $outcome = 0; while($i < $total){ $outcome = $num[$i] + $outcome; } echo($outcome); }else{ echo('NOTHING TO SHOW'); } ?> there is probably an easier way to do it, but thats the way i'd do it. hope it helps Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-374999 Share on other sites More sharing options...
JSHINER Posted October 21, 2007 Author Share Posted October 21, 2007 Would a new row for each view be the best way to do this in terms of speed and resources used? Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375000 Share on other sites More sharing options...
lewis987 Posted October 21, 2007 Share Posted October 21, 2007 no, the best way would make one row with just the views in it. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375002 Share on other sites More sharing options...
Orio Posted October 21, 2007 Share Posted October 21, 2007 The best way is to use SQL's sum() function, just as I showed a few posts above. Orio. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375003 Share on other sites More sharing options...
only one Posted October 21, 2007 Share Posted October 21, 2007 Would a new row for each view be the best way to do this in terms of speed and resources used? Yes. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375004 Share on other sites More sharing options...
Orio Posted October 21, 2007 Share Posted October 21, 2007 Would a new row for each view be the best way to do this in terms of speed and resources used? Yes. No This way you will have a very large table, that means alot of wasted disk space... Orio. Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375005 Share on other sites More sharing options...
JSHINER Posted October 21, 2007 Author Share Posted October 21, 2007 Orio your method worked great thanks! Link to comment https://forums.phpfreaks.com/topic/74232-solved-best-way-to-count/#findComment-375008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.