Given the code you’ve posted, here’s how I’d handle this.
First, I’d create an associative lookup array whose keys are the column names and whose values are the corresponding point values; it would look something like this:
$pointVals = array('email1' => 2, 'email2' => 5, 'email3' => 2, ... );
You can generate this array however you wish; I’d probably read it from a lookup table in the database. Note that this array contains only the columns for which the calculation is relevant.
Next, I would include the following code inside your while
loop. $row
is the record array returned by your call to mysql_fetch_array()
.
while ($row = mysql_fetch_array($result)) {
... // existing code goes here
$rowSum = 0;
foreach($pointVals as $colName => $val)
{
if(isset($row[$colName]) && !empty($row[$colName]))
$rowSum += $val;
}
... // do stuff with $rowSum here
}
Having said that, I would strongly encourage you to convert from the mysql driver to mysqli or PDO, as mysql is deprecated and has been removed from PHP 7. Also, I strongly suspect your query could be simplified and improved, but I’d need to see the base table to suggest anything.
0
solved How to Add last column in mysql table