1

I have following code (it's piece of bigger code):

<?php
  include_once 'init/init.funcs.php';
  $x = $_SESSION['answering']['index'];
  echo $_SESSION['answering']['questions'][$x-1];
  $result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused 
             where kysimus= "' .$_SESSION['answering']['questions'][$x] . '"');
  $question_id = mysql_result($result4, 0);
  $result5 = mysql_query('SELECT * from katse_valik_vastused 
                                   where kysimus_id="'. $question_id . '"');
  if($result5 === FALSE) {
    die(mysql_error());
  }
  while($row = mysql_fetch_assoc($result5)) {
    $options[] = $row['vasuts'];
  }
  //foreach($options as $option=>$option_value) {
  //echo $option_value;
  $count=count($options);
?>
<html>
  <br>
  <form method="post" action="answering.php">
    <input type="radio" name="1"><?php echo $options[0]?><br>
    <input type="radio" name="2"><?php echo $options[1]?><br>
    <input name= "submit" type="submit" value="Vasta">
  </form>
</html>

Right now there are two fixed radio buttons. But I want it to have as many buttons, as many elements are in array "options" and each of them to have a value of one element written next to it. How could I do it?

5 Answers 5

3

Use a for loop for this: http://www.php.net/manual/en/control-structures.for.php

for ($i = 1; $i < count($options); $i++) {
?>
<input type="radio" name="<?php echo $i; ?>"><?php echo $options[$i]?><br>
<?php
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try like this:

<?php
  while($row = mysql_fetch_assoc($result5)) {
    $options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php 
foreach($options as $option=>$option_value) {
    ?>
   <input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>

<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>

Comments

1
<html>
<br>
<form method="post" action="answering.php">
<?php
    foreach ($options as $index=>$option) {
        echo "<input type='radio' name='{$index}'>{$option}<br>";
    }
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>

Comments

0

Try using the below code.

<html>
    <br>
    <form method="post" action="answering.php">
        <?php
        foreach ($options as $key => $value) {
            ?>
            <input type="radio" name="<?php echo $key; ?>"><?php echo $options[$key] ?><br>
            <?php
        }
        ?>


        <input name= "submit" type="submit" value="Vasta">
    </form>
</html>

Comments

0

you can do this using for each, like this:

<form method="post" action="answering.php">
    <?php foreach ($options as $key => $value): ?>
        <input type="radio" name="<?php echo $key; ?>" /><?php echo $value; ?><br />
    <?php endforeach; ?>
    <input name= "submit" type="submit" value="Vasta">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.