【问题标题】:Codeigniter - quiz implementation issuesCodeigniter - 测验实施问题
【发布时间】:2016-02-14 11:04:16
【问题描述】:

我正在尝试创建一个我是谁的测验,用户在其中显示图像,他们选择他们认为图像在 Codeigniter 框架内的人,我遇到了几个错误。代码如下:

视图(guessview.php):

        <div>
            <img src="<?php echo $image ?>" width=400>
        </div> <!-- div/img -->
        <div>
            <?php
            if (!isset($iscorrect)) {
            ?>
            <p>Who is this?
            <form class="form">
                <input type=hidden name=id value="<?php echo $id ?>">
                <div class="radio">
                  <label>
                    <input type="radio" name="name" value="<?php echo $name1 ?>">
                    <?php echo $name1 ?>
                  </label>
                </div>
                <div class="radio">
                  <label>
                    <input type="radio" name="name" value="<?php echo $name2 ?>">
                    <?php echo $name2 ?>
                  </label>
                </div>
                <div>
                    <input type=submit value="Guess!">
                </div>
            </form>
            <?php
            }
            else {
                if ($iscorrect === true) {
                    // they guess correctly
                    ?>
                        <h2 class="bg-success">
                            Correct!  It's <?php echo $name ?>!
                        </h2>
                    <?php
                }
                else {
                    // they guessed wrongly
                    ?>
                    <h2 class="bg-warning">
                        Wrong!  It's not <?php echo $name ?>!
                    </h2>
                    <?php
                }
                ?>
                    <a href="guess"><button class="btn btn-primary btn-sm">Next<button></a>
                <?php
            }
            ?>
        </div>
</div>

型号:

<?php

class People extends CI_Model {
    private $imageurls
        = array(
            "http://cdn2.thr.com/sites/default/files/imagecache/675x380/2014/06/harrison_ford_enders_game.jpg",
            "http://i.kinja-img.com/gawker-media/image/upload/s--dLK5k-Xp--/1986hd9w3ho26jpg.jpg",
            "http://static.dnaindia.com/sites/default/files/2015/04/29/332116-salman-khan-prem-ratan.jpg",
            "http://media2.popsugar-assets.com/files/2013/01/01/4/192/1922398/45cf0b9c01b047cb_155566259_10.xxxlarge_2.jpg",
            "http://i.telegraph.co.uk/multimedia/archive/03343/corbyn1_3343657b.jpg",
            "http://cdn-img.instyle.com/sites/default/files/styles/684xflex/public/1430838021/050515-anne-hathaway-lead.jpg?itok=qZ72SsQ-",
            "http://www.thetvcollective.org/wp-content/uploads/2014/07/Meera-Syal.jpg",
            "http://www.lovebscott.com/wp-content/uploads/2015/03/featured4.jpg"
        );
    private $names
        = array("Harrison Ford","Morgan Freeman","Salman Khan","Halle Berry","Jeremy Corbyn",
            "Anne Hathaway","Meera Syal","Kanye West"
        );

    function __construct()
    {
        parent::__construct();
    }


    function getwho()
    {
        $max = count($this->names);
        $whopos = rand(0,$max - 1); // randomly-chosen number

        // use this randomly-chosen number to select a person
        $image = $this->imageurls[$whopos];
        $name = $this->names[$whopos];

        // now get a wrong name
        // can you work out what this is doing?
        $wrong = $whopos;
        while ($wrong == $whopos) {
            $wrong = rand(0,$max - 1);
        }

        // now get a wrong name
        $wrongname = $this->names[$wrong];

        // now decide which name comes first and second
        $choice = rand(0,1);
        if ($choice == 0) {
            $name1 = $name;
            $name2 = $wrongname;
        }
        else {
            $name1 = $wrongname;
            $name2 = $name;
        }
        // return $whopos as part of the result - this will make checking later easier
        return array('id' => $whopos,'image' => $image,'name1' => $name1,'name2' => $name2);
    }

    function getPerson($id)
    {
        return array('id' => $id,'image' => $this->imageurls[$id]);
    }

    function isCorrectAnswer($id,$name)
    {
        $answer = $this->names[$id];
        if ($answer == $name) {
            return true;
        }
        else {
            return false;
        }
    }
}

控制器:

<?php

class Guesser extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('people');
    }

    function guess()
    {
        $guess = $this->input->get('name',false);
        $personid = $this->input->get('id',false);
        if ($guess === false) {
            // no guess provided, so create a guess and display it
            $newguess = $this->people->getwho();
            $this->load->view('guessview',$newguess);
        }
        else {
            $res = $this->people->isCorrectAnswer($personid,$guess);
            $person = $this->people->getPerson($personid);
            $this->load->view('guessview',
                              array('image' => $person['image'],
                                    'iscorrect' => $res,'name' => $guess));
        }
    }
}

我收到的错误如下:

遇到了 PHP 错误

严重性:通知

消息:未定义索引:

文件名:models/People.php

行号:66

回溯:

文件: /home/student/927/w1375927/public_html/CI1/application/models/People.php 行:66 函数:_error_handler

文件: /home/student/927/w1375927/public_html/CI1/application/controllers/Guesser.php 行:20 功能:isCorrectAnswer

文件:/home/student/927/w1375927/public_html/CI1/index.php 行:292 函数:require_once

遇到了 PHP 错误

严重性:通知

消息:未定义索引:

文件名:models/People.php

行号:61

回溯:

文件: /home/student/927/w1375927/public_html/CI1/application/models/People.php 行:61 函数:_error_handler

文件: /home/student/927/w1375927/public_html/CI1/application/controllers/Guesser.php 行:21 函数:getPerson

文件:/home/student/927/w1375927/public_html/CI1/index.php 行:292 函数:require_once

我已经编辑了从模型和控制器之间传输图像的数组。

我的假设是在运行之前配置 codeigniter。如果有人以前遇到过这些错误,我将不胜感激。

谢谢。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您没有正确地将参数传递给视图,在控制器中更改为:

        <?php
    
    class Guesser extends CI_Controller {
        function __construct()
        {
            parent::__construct();
            $this->load->model('people');
        }
    
        function guess()
        {
            $guess = $this->input->get('name',false);
            $personid = $this->input->get('id',false);
            if ($guess === false) {
                // no guess provided, so create a guess and display it
                $data['newguess'] = $this->people->getwho();
                $this->load->view('guessview',$data);
            }
            else {
                $res = $this->people->isCorrectAnswer($personid,$guess);
                $person = $this->people->getPerson($personid);
                $this->load->view('guessview',
                                  array('image' => $person['image'],
                                        'iscorrect' => $res,'name' => $guess));
            }
        }
    }
    

    然后在视图中您可以访问$data['newguess'];$newguess注意,您的表单也没有任何动作,您需要在 html 中指定:

    <form action="http://yourdomain/controller/method" method="post" accept-charset="utf-8">
    

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2013-09-16
      • 1970-01-01
      • 2016-11-28
      相关资源
      最近更新 更多