在std对象中加入变量php

时间:2017-01-13 08:45:25

标签: php loops foreach

我有以下代码问题和答案,

<?php
foreach ($ques_perso as $q) {
?>
<div class="row">
   <div class="col-md-12">
      <div class="form-group">
         <label class="control-label"><?php echo $q['ques_text'] ?><span class="required"> * </span></label>
         <textarea class="form-control" id="ques_<?php echo $q['ques_id'] ?>" name="ques_<?php echo $q['ques_id'] ?>" autocomplete="off" rows="3" readonly><?php echo $answers->ques_X; ?></textarea>
       </div>
    </div>
</div>
<?php
} ?>

这是10个问题及其答案的代码。

我有$answers对象,如下所示,

stdClass Object
(
[ans_id] => 1
[user_id] => 1
[ques_1] => check1
[ques_2] => check2
[ques_3] => check3
[ques_4] => check4
[ques_5] => check5
[ques_6] => check6
[ques_7] => check7
[ques_8] => check8
[ques_9] => check9
[ques_10] => check10
)

现在我想把这个答案放在textarea中,但我无法通过使用上面的代码来输入..我可以使用我的答案,如echo $answers->ques_X

谢谢,

2 个答案:

答案 0 :(得分:1)

哇你有一个问题ID为$q['ques_id'],现在可以这样改变。

$answer_id = "ques_".$q['ques_id'];
<textarea class="form-control" id="ques_<?php echo $q['ques_id'] ?>" name="ques_<?php echo $q['ques_id'] ?>" autocomplete="off" rows="3" readonly><?php echo $answers->$answer_id; ?></textarea>

答案 1 :(得分:0)

我不确定我是否完全理解这个问题,但也许你可以做这样的事情来显示$answers对象的内容。

$properties = get_object_vars( $answers );
foreach( $properties as $property => $value )echo $property.'='.$value;