So I am trying to create a loop that will sum up the values that i have entered in a mysql database.
I have a table with 70 rows, one for each student. The columns in the table are c1, c2, c3...etc. and could go up to an infinite amount of columns. all that is stored in these columns is a 1 or 0. I am trying to use a loop to add up all of these columns and display a final number for each student vs typing out $row['c1'] + $row['c2'] etc.
Here is what i have so far....
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("
SELECT *
FROM student
INNER JOIN babatt
ON student.id=babatt.id");
while($row = mysql_fetch_array($result))
{
echo $row['firstname'];
for ( $counter = 1; $counter <= 500; $counter += 1) {
$c = "c" . "" . $counter;
$class = $row[$c];
}}
$c allows the name of the column to change from c1 to c2 as it goes through the loop
$class gets the value from the database where the column equals whatever $c equals
but I'm not sure what to put after this to add them up
thanks for the help