/    Sign up×
Community /Pin to ProfileBookmark

auto display correct number of radio buttons

Hi,
I have a poll table that contains the questions and the code below displays the questions with radio buttons. I get $q values from database. Now, I dont have 5 questions all the time. Sometimes I have less. Lets say I have three questions, the code below displays 4th and 5th buttons regardless. How can I make it automatically display the appropriate number of options? (i.e. if I have 3 questions, immediately after displaying the third option, it needs to display the submit button)
I tried if statement, but my php knowledge is not enough how to make it work.

<form method=”post” action=”updateanswers.php”>

<?php echo $title; ?>
</br>
<input type=”radio” name=”answer” value=”answer1″>
<?php echo $q1; ?>
</br>
<input type=”radio” name=”answer” value=”answer2″>
<?php echo $q2; ?>
</br>
<input type=”radio” name=”answer” value=”answer3″>
<?php echo $q3; ?>
</br>
<input type=”radio” name=”answer” value=”answer4″>
<?php echo $q4; ?>
</br>
<input type=”radio” name=”answer” value=”answer5″>
<?php echo $q5; ?>
</br>

<input type=”submit” value=”submit my answer”/>
</form>

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Oct 04.2008 — Find out how many answers there are and then loop through them with a for loop.
Copy linkTweet thisAlerts:
@ketancoauthorOct 04.2008 — what do you mean exactly by saying "loop through them" please?
Copy linkTweet thisAlerts:
@skywalker2208Oct 05.2008 — because this part of your code is similar.
[code=php]
<input type="radio" name="answer" value="answer1">
<?php echo $q1; ?>
</br>
[/code]


All you need to do is loop through each answer, but just changing the value each time it loops through.
Copy linkTweet thisAlerts:
@ketancoauthorOct 05.2008 — thanks
×

Success!

Help @ketanco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...