Jump to content

SUM of column Help!!!


twilitegxa

Recommended Posts

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!!!????

Link to comment
https://forums.phpfreaks.com/topic/196686-sum-of-column-help/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032776
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032779
Share on other sites

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!!!

Link to comment
https://forums.phpfreaks.com/topic/196686-sum-of-column-help/#findComment-1032784
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.