I've been following an online tutorial to build a quiz which calculates the result from an sql table.
The way the quiz currently works is to create a string depending upon how often each category in the table appears but what I'd like to try and do is populate the string with the single most commonly occuring category.
Can anyone help me achieve this?
My code so far:
$query = "SELECT result FROM quiz_key WHERE question IN ('$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10')";
$result = mysql_query($query);
$cat_a = $cat_b = $cat_c = $cat_d = $cat_e = $cat_f = 0;
while($row = mysql_fetch_array($result)) {
$cat = $row['category'];
if ($cat == "A") {
$cat_a += 1;
} elseif ($cat == "B") {
$cat_b += 1;
} elseif ($cat == "C") {
$cat_c += 1;
} elseif ($cat == "D") {
$cat_d += 1;
} elseif ($cat == "E") {
$cat_e += 1;
} elseif ($cat == "F") {
$cat_f += 1;
}
}
$array = array('A' => $cat_a, 'B' => $cat_b, 'C' => $cat_c, 'D' => $cat_d, 'E' => $cat_e, 'F' => $cat_f);
$str = "";
Then followed by this:
foreach ($array as $i => $value) {
if ($value >= 6) {
$str = $i;
break;
} elseif ($value >= 3) {
$str .= $i;
}
}
$var = sort($str);
Any help would be much appreciated.