简体   繁体   中英

questions and radio buttons loop

I'm trying to create a form where people can leave feedback about the product. There are around 10 question... and maybe increase.. I want to use loop which for the radio buttons. Instead of creating 6 new radio buttons for each question. Kind stuck. here the code... also any help with how i could use a loop to collect the results from this page onto the next page? stackoverflow isn't allowing me to paste the code here... something to do with indentation. spent like half an hour and couldnt work out what was wrong haha. so i pasted the code here,

<?php
$questions = array(
  ("Question 1 - What did you think of the product?", "Question 2 - Would you use it again?", "Question 2 - How likely will you recommend this product to your friends/family?"

);

?>

<?php

    for ($questions = 0; $questions <= 3; ++$i) {
        $echo .$questions and .$i
    }
?>


<?php
    for ($i = 0; $questions <= 3; ++$i) {
        $questions[] = $i;
    }
"<input type='radio' name='Question[]' value='6'>6";
"<input type='radio' name='Question[]' value='5'>5";
"<input type='radio' name='Question[]' value='4'>4";
"<input type='radio' name='Question[]' value='3'>3";
"<input type='radio' name='Question{]' value='2'>2";
"<input type='radio' name='Question[]' value='1'>1";
?>

thanks

If you need to loop through arrays to generate a radio form, you can do something like this:

$questions = array(
    1 => "Question 1 - What did you think of the product?",
    2 => "Question 2 - Would you use it again?",
    3 => "Question 3 - How likely would you recommend this product?"
);

foreach ($questions as $k => $v) {
    echo "<input type='radio' name='Question".$k."' value='".$k."' />"." ".$v."<br>";
}

When dealing with arrays it's best to use the foreach loop because it iterates through an array. Furthermore, your implementation of the FOR loop is incorrect. You should check your syntax before using that moving forward.

<?php
$questions = array(
  "Question 1 - What did you think of the product?",
  "Question 2 - Would you use it again?",
  "Question 2 - How likely will you recommend this product to your friends/family?"
);
// You had the right idea... Just use a different variable (since "questions"
// has already been used). Also, you can have PHP just count the questions
// for you instead of hard-coding "3":
for ($q = 0; $q <= count($questions); $q++) {
  // I assume "$people" has been defined somewhere else...?
  // No $ in front of "echo"
  // A dot "." means "and" (kind of).
  echo $people . $q;
}

// Different variable other than $people
// Less than as opposed less than or equal to (arrays start at 0 but the count starts at
// 1 just like anything else you count), but we'll start $i as 1 so we have to up the
// counted value by adding 1
$count_questions = count($questions) + 1;
for ($i = 1; $i < $count_questions; $i++) {
  // Remember to close input tags
  echo "<input type='radio' name='Question[]' value='$i'>$i</input>";
}

?>

for radio buttons with questions

 <?php
    try
    {
    //Storing no. of Total Questions for next page in session
    session_start();
    $ttlque = count($questions);
    $_SESSION['ttlquestions'] = $ttlque;
    for ($i = 0; $i < $ttlque; $i++) 
    {
    echo $questions[$i].'<br /><label><input type="radio" name="Question'.$i.'" value="6">6</label><br /><label><input type="radio" name="Question'.$i.'" value="5">5</label><br /><label><input type="radio" name="Question'.$i.'" value="4">4</label><br /><label><input type="radio" name="Question'.$i.'" value="3">3</label><br /><label><input type="radio" name="Question'.$i.'" value="2">2</label><br /><label><input type="radio" name="Question'.$i.'" value="1">1</label>';
    }
    }
    catch(Exception $e)
    {
       echo 'Message: ' .$e->getMessage();
    }
?>

on page of result generating do this

<?php
   $answer = 0;
   for($i = 0; $i<$_SESSION['ttlquestions']; $i++)
   {
       $answer += $_POST['Question'.$i];
   }
   echo $answer/$_SESSION['ttlquestions'];
?>

I havn't tested, just give it a try and let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM