简体   繁体   中英

include <?php /?> in php variable

I want to include the following code (and copies of it for each question) into a php variable as part of an array that i can shuffle to randomize the order of questions in a survey.

text of question  
<br> 
<input type="radio" name="q1" <?php if (isset($q1) && $q1=="yes") echo "checked";?> value='1'>Yes 
<input type="radio" name="q1" <?php if (isset($q1) && $q1=="no") echo "checked";?> value='-1'>No 
<br><br>

So far the only solution I found is to save only that code into a php file and feed the file into a variable. The final result looks like this:

$q1Text = file_get_contents('\q1.php')
$q2Text = file_get_contents('\q2.php')
$q3Text = file_get_contents('\q3.php')
$q4Text = file_get_contents('\q4.php')
$q5Text = file_get_contents('\q5.php')
$q6Text = file_get_contents('\q6.php')
$questions=array($q1Text, $q2Text, $q3Text, $q4Text, $q5Text, $q6Text);
shuffle($questions);
$count='1';
foreach ($questions as $value) {
    echo $count;
    echo ") $value ";
    $count++;
}

This method works great, but I need to create a file for each question (I have 35 questions).

What is a more concise, elegant, and proper way of doing this? Thank you!!

In fact you can include directly your question file. But you need to edit it a little for adding PHP start tag.

So your question file became :

<?php

?>
text of question  
<br> 
<input type="radio" name="q1" <?php if (isset($q1) && $q1=="yes") echo "checked";?> value='1'>Yes 
<input type="radio" name="q1" <?php if (isset($q1) && $q1=="no") echo "checked";?> value='-1'>No 
<br><br>
<?php

?>

And your foreach became like that you doesn't need to make file_get_contents() because the include will read and execute the code in your question file :

It's became :

$q1Text = '\q1.php';
$q2Text = '\q2.php';
$q3Text = '\q3.php';
$q4Text = '\q4.php';
$q5Text = '\q5.php';
$q6Text = '\q6.php';

$questions=array($q1Text, $q2Text, $q3Text, $q4Text, $q5Text, $q6Text);

shuffle($questions);
$count='1';

foreach ($questions as $value) {

    echo $count;
    echo ") ";

    include($value);

    $count++;
}

Becarfull to the path of inclusion for your question file and that's all.

If you need more explain don't hesitate to ask !

Store your questions as data , not code, and then use code to output them:

$questions = [
    ['Text of question', ['Yes', 'No']],
    ...
];

This just uses an array of arrays where the first item is the question and the second item is an array of answers. Use your own convention to best express the data structure you need…

shuffle($questions);  // or whatever

foreach ($questions as $i => $question) : ?>

<p><?= $question[0]; ?></p>
<br>
<php foreach ($question[1] as $answer) : ?>
    <input type="radio" name="q<?= $i ?>" <?php if (isset(${"q$i"}) && ${"q$i"} == $answer): ?>checked<?php endif; ?> value="<?= $answer ?>"><?= $answer ?>
<?php endforeach; ?>

<?php endforeach; ?>

This is by no means perfect code, but it goes into the right direction. Adjust to your situation as needed.

If the questions are all yes/no questions, I'd try another approach. Instead of including files you could just put the question numbers and text in an associative array and generate the form in the loop.

This way you don't have to write many files and you write the form's code once.

$questions = [
     ["number" => 1, "text" => "First question's text"],
     ["number" => 2, "text" => "Second question's text"],
     ["number" => 3, "text" => "Third question's text"]
];

$count = 1;

foreach ($questions as $value) {
   $question = "q".$value['number'];

   echo "$count) ".$value["text"];

   echo "<br>";
   echo '<input type="radio" name="'.$question.'" ';
   if (isset($$question) && $$question=="yes") {
      echo "checked";
   }
   echo ' value="1">Yes';
   echo '<input type="radio" name="'.$question.'"';
   if (isset($$question) && $$question=="no") {
      echo "checked";
   }       
   echo ' value="-1">No';
   echo '<br><br>';

   $count++;
}

However if you have different kinds of questions, I'd go with a modified version of Inazo's answer.

$questions = array();
$number_of_questions = 35;

/* generate questions array */
for($i = 1; $i <= $number_of_questions; $i++){
  if file_exists("./q$i.php"){
    array_push($questions,"./q$i.php");
  }
}

shuffle($questions);
$count = 1;

/* generate your form */
foreach ($questions as $value) {

   echo "$count) ";

   include($value);

   $count++;
}

var_export is able to write your array data out as parseable PHP source code. You will need to add <?php $varname = and ; ?> ; ?> around what this function outputs, to create a file you can simply embed using include/require.

Instead of writing executable PHP code to a file directly, you could also encode your data as JSON, write that to a file, and decode it again after you read it back from the file.

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