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" Quote Link to comment 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"); Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted October 21, 2007 Author Share Posted October 21, 2007 Orio your method worked great thanks! Quote Link to comment 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.