twilitegxa Posted March 27, 2010 Share Posted March 27, 2010 I have a sort of confusing column I need the sum of. I have the following code: //get levels $get_levels = "select * from scout_neutral_attributes where identity = '$identity'"; $get_levels_res = mysql_query($get_levels, $conn) or die(mysql_error()); while ($level_info = mysql_fetch_array($get_levels_res)){ $neutral_id = $level_info['attribute_id']; $level_id = $level_info['level_id']; //get points per level $get_points = "select * from neutral_attributes where id = '$neutral_id'"; $get_points_res = mysql_query($get_points, $conn) or die(mysql_error()); while ($point_info = mysql_fetch_array($get_points_res)){ $point_id = $point_info['id']; $points = $point_info['points']; } } I need to get the sum of the column that is $points. If I echo $points, it's like 1114 (this is four record values in a line). I need to get the total of this: 7 instead of 1114. How can I do that? Can anyone help? This is driving me crazy! I have been trying for a while and can't get it. $sum_col = "select *, SUM(points) from neutral_attributes where id = '$neutral_id'"; $sum_col_res = mysql_query($sum_col) or die(mysql_error()); while ($row = mysql_fetch_array($sum_col_res)){ $sum = $row['SUM(points)']; } But it's still returning 1114 instead of the total of 7 that I want. Can anyone help, please!!!???? Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/ Share on other sites More sharing options...
Kieran Menor Posted March 27, 2010 Share Posted March 27, 2010 Perhaps the points column doesn't have a numeric type? Is it VARCHAR or something? It would be nice if you posted your table structure, and possibly some sample rows. Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032673 Share on other sites More sharing options...
AdRock Posted March 27, 2010 Share Posted March 27, 2010 What about using COUNT Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032681 Share on other sites More sharing options...
MatthewJ Posted March 27, 2010 Share Posted March 27, 2010 while ($row = mysql_fetch_array($sum_col_res)){ $total = 0; $sum = $row['SUM(points)']; for ($i=0;$i<strlen($row['SUM(points)']);$i++) { $total = $total + $row['SUM(points)'][$i]; } } echo $total; Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032684 Share on other sites More sharing options...
twilitegxa Posted March 27, 2010 Author Share Posted March 27, 2010 Perhaps the points column doesn't have a numeric type? Is it VARCHAR or something? It would be nice if you posted your table structure, and possibly some sample rows. points is and int type. Here are both table structures: neutral_attributes: CREATE TABLE IF NOT EXISTS `neutral_attributes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `attribute` varchar(150) DEFAULT NULL, `points` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ; sample rows: (1, 'Acrobatics', 1), (2, 'Appearance', 1), (3, 'Art Of Distraction', 1) scout_neutral_attributes: CREATE TABLE IF NOT EXISTS `scout_neutral_attributes` ( `id` int(11) NOT NULL, `identity` varchar(150) DEFAULT NULL, `attribute_id` int(11) DEFAULT NULL, `level_id` int(11) DEFAULT NULL, `notes` longtext ) ENGINE=MyISAM DEFAULT CHARSET=latin1; sample rows: (2, 'Sailor Moon', 2, 4, NULL), (3, 'Sailor Moon', 3, 4, NULL), (4, 'Sailor Moon', 4, 2, NULL) Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032776 Share on other sites More sharing options...
twilitegxa Posted March 27, 2010 Author Share Posted March 27, 2010 while ($row = mysql_fetch_array($sum_col_res)){ $total = 0; $sum = $row['SUM(points)']; for ($i=0;$i<strlen($row['SUM(points)']);$i++) { $total = $total + $row['SUM(points)'][$i]; } } echo $total; You example is still bringing the same result: 1114 Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032779 Share on other sites More sharing options...
twilitegxa Posted March 27, 2010 Author Share Posted March 27, 2010 What about using COUNT I looked up count and it seems it's just for counting the number of rows, not adding the values of the rows. :-( Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032780 Share on other sites More sharing options...
greatstar00 Posted March 27, 2010 Share Posted March 27, 2010 what is sum of column means? can u provide your own calculation? maybe we misunderstood you Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032783 Share on other sites More sharing options...
twilitegxa Posted March 27, 2010 Author Share Posted March 27, 2010 I think I was totally over tired last night/this morning when I was doing the calculation. I got it now: //get points per level $get_points = "select * from neutral_attributes where id = '$attribute_id'"; $get_points_res = mysql_query($get_points, $conn) or die(mysql_error()); while ($points_info = mysql_fetch_array($get_points_res)){ $points = $points_info['points']; //get attribute names $get_names = "select * from neutral_attributes where id = '$attribute_id'"; $get_names_res = mysql_query($get_names, $conn) or die(mysql_error()); while ($names = mysql_fetch_array($get_names_res)){ $attribute = $names['attribute']; $points_used = ($level_id * $points); $display_block .= "<tr><td>$attribute</td> <td>$level_id - $points - $points_used</td> <td><a href=remove_attribute.php?id=$_GET[id]>Remove</a></td></tr>"; } } } Thanks for the help guys. Sorry for the confusion!!! Quote Link to comment https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032784 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.