Hi all!
I'm still pretty new to PHP, and am creating an online survey for work. I have succesfully managed to get the data into the database, and the only way I know to retrieve it is very very tedious as per the attached code below. I have close to 100 questions and each question needs to be sorted by 8 sets of demographic details, which means i'll have to program a total of 800 times, which will take me forever!
Here's my code for each question - it works, but seems extremely long winded
<?php
include("survey.inc");
//Count the total no of responses for the question
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result=mysql_query("Select COUNT(Q1) as res from HGsurvey")
or DIE ("Couldn't execute total responses");
$orgNoQ1=mysql_fetch_array($result);
//Count the total number of negative responses for the question
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result1=mysql_query("Select COUNT(Q1) as q1negative from HGsurvey where Q1='1' or Q1='2'")
or DIE ("Couldn't execute negative reponses");
$orgnegQ1=mysql_fetch_array($result1);
//Count the total no of neutral reponses for the question
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result2=mysql_query("Select COUNT(Q1) as q1neutral from HGsurvey where Q1='3' ")
or DIE ("Couldn't execute neutral");
$orgneutralQ1=mysql_fetch_array($result2);
//Count the total no of positive reponses for the question
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result3=mysql_query("Select COUNT(Q1) as q1positive from HGsurvey where Q1='4' or Q1='5' ")
or DIE ("Couldn't execute positive");
$orgpositiveQ1=mysql_fetch_array($result3);
//Count the total no of not applicable reponses for the question
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database");
$result4=mysql_query("Select COUNT(Q1) as q1na from HGsurvey where Q1='6'")
or DIE ("Couldn't execute not applicable");
$orgnaQ1=mysql_fetch_array($result4);
echo "Total no. of repondents: ";
echo $orgNoQ1['res'];
echo "<br>";
echo "Total negative reponses: ";
echo $orgnegQ1['q1negative'];
echo "<br>";
echo "Total neutral reponses: ";
echo $orgneutralQ1['q1neutral'];
echo "<br>";
echo "Total positive reponses: ";
echo $orgpositiveQ1['q1positive'];
echo "<br>";
echo "Total not applicable reponses: ";
echo $orgnaQ1['q1na'];
echo "<br>";
echo "<br>";
//percentage of negative responses
$percneg=number_format($orgnegQ1['q1negative']/$orgNoQ1['res']*100,2);
//percentage of neutal responses
$percneu=number_format($orgneutralQ1['q1neutral']/$orgNoQ1['res']*100,2);
//percentage of positive responses
$percpos=number_format($orgpositiveQ1['q1positive']/$orgNoQ1['res']*100,2);
//percentage of not applicable responses
$percna=number_format($orgnaQ1['q1na']/$orgNoQ1['res']*100,2);
echo "Percentage of negative responses: \t\t";
echo $percneg;
echo "<br>";
echo "Percentage of neutral responses: ";
echo $percneu;
echo "<br>";
echo "Percentage of positive responses: ";
echo $percpos;
echo "<br>";
echo "Percentage of not applicable responses: ";
echo $percna;
?>
Can someone help me by telling me a way to use functions for my code or even a neater way of doing this?
Would appreciate any help i can get!
Thanks in advance!