I’ve been working on a survey system that reads the records(questions sorted by “chapters” you’ll see what I mean later) out of database and adds a score per record. The questions are stored in an array. so one record can hold for example five questions. let’s dive into the code
//echo the question list from the database
$query_questions = mysql_query("SELECT * FROM `vraag`");
while($result = mysql_fetch_array($query_questions))
{
//displaying the questions
echo "<br /><br /><b>";
echo $result['header']. "</b><br />";
echo $result['beschrijving']. "<br /><br />";
echo $result['onderwerp']. "<br />";
$aantal = $result['aantal_vragen'];
$id = $result['id'];
echo "<input type='hidden' id='number_of_questions' value=".$aantal." mousedown='calculate_score(this.value,0)'/>";
$array = unserialize($result['vraag']);
//retrieve the content from the array
for($i = 0; $i < $array; $i++)
{
foreach($array as $value)
{
$array = $value;
echo "<form><table><td>". $array ."</td>";
echo "
<td>1<input type='radio' name='vraag[]' value=1 onclick='calculate_score(".$aantal.",this.value)'/></td>
<td>2<input type='radio' name='vraag[]' value=2 onclick='calculate_score(".$aantal.",this.value)'/></td>
<td>3<input type='radio' name='vraag[]' value=3 onclick='calculate_score(".$aantal.",this.value)'/></td>
<td>4<input type='radio' name='vraag[]' value=4 onclick='calculate_score(".$aantal.",this.value)'/></td>
<td>5<input type='radio' name='vraag[]' value=5 onclick='calculate_score(".$aantal.",this.value)'/></td></table></form>";
}
echo "Uw totaal score is: <span id='total_score[]' ></span>";
break;
}
}
what goes wrong is that it uses the last record of the query as the value to calculate with, it ignores the first record and the second and only picks up the last(third) record I want it to assign a score per record so shortly drawn out. The point of the radio elements is to make a point system ranking from 1 to 5 so it looks like: Question 1-2-3-4-5
this is my intention
[record 1]
score
[record 2]
score
[record 3]
score
but currently it works like this
[record 1]
[record 2]
[record 3]
score
I’m not entirely sure about the break command I have but I was testing if it’d fix my issue. For the score label thing I know I should increment the array by one each time it’s filled but I don’t know the syntax or if it is even possible at all to do so.
I’m new to stack overflow so forgive me any missing tags or anything other that’s unreadable or unpleasant on the eyes.
The outer
forloop makes no sense. In the inner loop you are re-assigning$arraywhile iterating over it (don’t really know what it does, but it’s just plain wrong).