Page 1 of 1

Newbie Here

Posted: Thu Mar 26, 2009 11:51 am
by Div
Firstly i'd like to say hi all.

I have been learning php for about 3 weeks now and its going well. Here is some code i have put together

Code: Select all

//Stores the strings to search in the database
$satisfaction = array(  "Extremely satisfied", "Very satisfied", "Fairly satisfied",
            "Neither satisfied nor dissatisfied", "Fairly dissatisfied",
            "Very dissatisfied", "Extremely dissatisfied");
 
/*Loops through the satisfaction array and counts how many times each satisfaction appears in the column
and then creates another array called countScores and uses the satisfaction array values as the key and
the sum as the value*/
foreach($satisfaction as $value){
    $countNo = mysql_query("SELECT `q17b_1`, COUNT(*) AS count FROM `csi` GROUP BY `q17b_1`"); 
    while($row=mysql_fetch_assoc($countNo)) {   
        $query = mysql_query("SELECT q17b_1 FROM csi WHERE q17b_1 = '$value'") or die(mysql_error());
    }
    $countScores[$value] = mysql_num_rows($query);
}
 
//Counts how many values are in the countScores array. Use this to calculate the percentages
$total = array_sum($countScores);
 
echo "<table border=1><tr><th><b>Satisfaction</b></th><th><b>Responses</b></th></tr>";
 
/*Loops through the countScores array, calculates the percentage by using the total variable, and then
puts the key and value into a table row*/
foreach($countScores as $key => $value){
    $result = round(($value / $total) * 100,1);
    echo "<tr><td>";
    echo $key;
    echo "</td><td>";
    echo $result;
    echo "%</td></tr>";
}
 
echo "</table>";
It produces a table with the following:
Satisfaction Responses
Extremely satisfied 16.5%
Very satisfied 54.5%
Fairly satisfied 20.1%
Neither satisfied nor dissatisfied 1%
Fairly dissatisfied 3%
Very dissatisfied 2%
Extremely dissatisfied 3%

But what i want to do is sum together some of the values of the countScores array.

What is the easiest way to do this?

Also seeing as this is the first piece of code i have put together, any pointers / comments / suggestions would be welcome (just don't get too technical :) ).

Many Thanks

Re: Newbie Here

Posted: Thu Mar 26, 2009 4:23 pm
by Div
Figured it out now.

Code: Select all

$overallSat = round((($countScores['Extremely satisfied'] + $countScores['Very satisfied']) / $total)*100,1);
echo $overallSat."%";