简体   繁体   中英

adding weighted avg to returned mysql query and adding all the data up

I'm relatively new to php...I have two queries that are returning an average of the column by another field name. I need to add weights to these averages and then add up the results for an overall score...I can't get it to work...here are the queries:

$engresults = $con->query("SELECT AVG(activity) as avgscore FROM combinedna WHERE coachname = '$coachname'");    
$timeresults = $con->query("SELECT AVG(timely) as avgtime FROM combinedna WHERE coachname = '$coachname'");

The averages return correctly and I can add the weight to this function but I can't add the two together

while($row = $engresults->fetch_assoc()) {
    echo "<tr>";
    echo "<td>Did you identify an effective integrated activity?</td>";
    echo "<td><center>" .$row['avgscore'] *.05 . "</center></td>";
    echo "</tr>";
}

while($row = $timeresults->fetch_assoc()) {
    echo "<tr>";
    echo "<td>Did you spend the appropriate amount of time?</td>";
    echo "<td><center> " .$row['avgtime'] *.10 . "</center></td>";
    echo "</tr>";
}

I need to add .05 to the first and .10 to the second then add the results together...I just can't figure out the PHP to use. Thanks in advance for your patience and help

Create a single query: (no need to do something twice :) )

$results = $con->query("SELECT AVG(activity) as avgscore, AVG(timely) as avgtime FROM combinedna WHERE coachname = '$coachname'"); Then get the result (only one row) $row = $results ->fetch_assoc()

echo "<tr>";
echo "<td>Did you identify an effective integrated activity?</td>";
echo "<td><center>" .$row['avgscore'] *.05 . "</center></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Did you spend the appropriate amount of time?</td>";
echo "<td><center> " .$row['avgtime'] *.10 . "</center></td>";
echo "</tr>";
$total = $row['avgscore'] *.05 + $row['avgtime'] *.10;

Put the values in variables and then add them.

$row = $engresults->fetch_assoc(); // No loop needed
$avgscore_weighted = $row['avgscore'] * .05;
$row = $timeresults->fetch_assoc();
$avgtime_weighted = $row['avgtime'] * .10;
$weighted_sum = $avg_score_weighted + $avgtime_weighted;

echo "<tr>";
echo "<td>Did you identify an effective integrated activity?</td>";
echo "<td><center>" .$avgscore_weighted . "</center></td>";
echo "</tr>";

echo "<tr>";
echo "<td>Did you spend the appropriate amount of time?</td>";
echo "<td><center> " .$avgtime_weighted . "</center></td>";
echo "</tr>";

echo "<tr>";
echo "<td>The total was: </td>";
echo "<td><center> " .$weighted_sum . "</center></td>";
echo "</tr>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM