【问题标题】:Output MySQL variables in PHP array在 PHP 数组中输出 MySQL 变量
【发布时间】:2014-06-24 13:34:57
【问题描述】:

我有 MySQL 变量,它们正在回显,但我想在数组中使用它们并随机显示结果 - 这是为了比赛 - 随机工作很好,但我无法让变量在它们时正确显示'重新在数组()中。

我写了以下内容:

<?php
    $answer = array('$this->item->correct_answer','$this->item->false_answer1','$this->item->false_answer2');
    shuffle($answer);
?>

<ol class="answers">
    <?php
        for( $i = 0; $i < 3; $i++)
            echo "<li>$answer[$i]</li>";
    ?>  
</ol>

这只是输出:

  • $this->item->correct_answer
  • $this->item->false_answer2
  • $this->item->false_answer1
  • 【问题讨论】:

    • 您将 $this->item->correct_answer 视为字符串。但那不是字符串...根据 db 值,它的输出可以是字符串/浮点数/整数。

    标签: php mysql arrays


    【解决方案1】:

    那是因为您有 ' 用于数组输入。这意味着你正在设置一个字符串。修改为:

    $answer = array($this->item->correct_answer,$this->item->false_answer1,$this->item->false_answer2);
    

    如图所示On codepad.org

    【讨论】:

    • @RustyIngles 随时!:) 完成后请将其标记为正确答案。
    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多