Reputation: 33
I am very new to both php and HTML and have been trying to wrap my head around them for the past few days. I am trying to create a questionnaire with radio buttons that is 56 questions long.
This is the generic layout I have right now
<form action="index.php" name="My Form" method="POST">
<ol>
<li>
<p>Do you eat cheese?</p><br />
<input type="radio" name="Q1" value="0"/>Never<br />
<input type="radio" name="Q1" value="1"/>In the past<br />
<input type="radio" name="Q1" value="2"/>Sometimes<br />
<input type="radio" name="Q1" value="4"/>Often<br /><br />
</li>
<input type="submit" name="submit" value="Submit the questionnaire"></input>
</ol>
</form>
Now, I need the 4 buttons on each question but I would prefer not to have to write it out 56 times with only the name changing (the plan is to change the name to "Q1", "Q2" etc). So, I was wondering if there is a way to create a function that saves me having to repeat it so often.
I tried variants of this
<html>
<body>
<?php
function inputs($counter)
$questions = array("Q1","Q2");
echo '
<input type="radio" name=.$questions[$counter]; value="0"
/>Never
<br />
<input type="radio" name=.$questions[$counter]; value="1" />In
the past
<br />
<input type="radio" name=.$questions[$counter]; value="2"
/>Sometimes
<br />
<input type="radio" name=.$questions[$counter]; value="4"
/>Often
<br />
<br /> ';
?>
</body>
</html>
With the intention of then going like this in the list item
<p>Do you eat cheese?<p><br />
<?php inputs(0);?>
(with the file containing the function already included)
And did at one point manage to get it to print out to the page (all correct but didn't transfer over into the index.php for calculations). I have a suspicion I should make inputs(0) equal to something, but I don't know.
So my questions are ->
Cheers
Upvotes: 3
Views: 66
Reputation: 5708
Use this:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<?php
// Make sure you have 56 elements in the array
$questions = array("Do you always eat cheese?", "Are you human?",
"Do you have a dog?", "Mr. Murphy is funny?",
...);
for($i=1; $i < 57; $i++)
{
echo '<table>
<tr>
<td><p>'.$questions[$i-1].'</p></td>
</tr>
<tr>
<td><input type="radio" name="Q'.$i.'" value="0"/>Never</td>
</tr>
<tr>
<td><input type="radio" name="Q'.$i.'" value="1"/>In the past</td>
</tr>
<tr>
<td><input type="radio" name="Q'.$i.'" value="2"/>Sometimes</td>
</tr>
<tr>
<td><input type="radio" name="Q'.$i.'" value="4"/>Often</td>
</tr>
</table>';
}
?>
</body>
</html>
Upvotes: 1
Reputation: 33823
You could do like this perhaps:
function inputs( $i ){
$answers=array(
'Never','In the past','Sometimes','Often'
);
foreach( $answers as $key => $answer ) echo "<div><input type='radio' name='Q{$i}' value='{$key}' />{$answer}</div>";
}
<p>Do you eat cheese?<p>
<?php inputs(0);?>
<p>Do you eat bananas?<p>
<?php inputs(1);?>
Assuming the values assign to each radio button are important ( 0,1,2,4
) then rather than using the default numerical index ($key
) as before then
function inputs( $i ){
$answers=array(
0 => 'Never',
1 => 'In the past',
2 => 'Sometimes',
4 => 'Often'
);
foreach( $answers as $key => $answer ) echo "<div><input type='radio' name='Q{$i}' value='{$key}' />{$answer}</div>";
}
Upvotes: 1
Reputation: 3796
this function do what you need .
<?php
function inputs($counter){
$labels = ['Never','In the past','Sometimes','Often'];
$questions = array("Q1","Q2");
for($n=0; $n < $counter; $n++){
echo '<input type="radio" name="'. $questions[0] .'" value="'. $n .'" /> '. $labels[$n] .' <br /> ';
}
}
?>
<!-- html -->
<p> Do you eat cheese? <p> <br />
<?php echo inputs(5); ?>
//where 5 is the number of inputs you need
Upvotes: 1
Reputation: 6953
I'll write this as an answer instead of a comment, because it will be too much code in it.
What you should do (at this state of learning):
Let the function return something to be echoed later. Second let's correct a small misstake:
<?php
function inputs($question) { // you were missing these { here
// missing ' here and here + the dot
$html = '<input type="radio" name='.$question.' value="0"/>Never';
// add some more html // better with " around the string
$html += '<br/><input type="radio" name="'.$question.'" value="1" />In the past';
// add whatever you like to add to that...
// at the end let's return all we've got to use it later:
return $html;
}
// and now you can do:
$questions = array("Q1","Q2");
for($i=0;$i<count($questions);$i++) {
echo inputs($questions[$i]); // send only one question to the function
}
?>
Upvotes: 1
Reputation: 3783
Should I just bite the bullet and write the questions?
Definitely not! That's way too much code!
If I understand your question correctly, you simply want the name
to change for each of the 56 different questions. While you were on the right track with an array, I think it would be easier to increment $i
each time, like this:
function inputs($counter) {
$i = 0
while($i < 57) {
echo'
<input type="radio" name="Q'.$i.'" value="0"
/>Never
<br />
<input type="radio" name="Q'.$i.'" value="1" />In
the past
<br />
<input type="radio" name="Q'.$i.'" value="2"
/>Sometimes
<br />
<input type="radio" name="Q'.$i.'" value="4"
/>Often
<br />
<br />
';
$i++
}
}
Upvotes: 1