1
votes

I'm attempting to create a who am i quiz where the user is show an image and they select who they think the image is within the Codeigniter framework and I'm getting several errors. The code is as follows:

The view (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>

The model:

<?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;
        }
    }
}

The controller:

<?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));
        }
    }
}

The errors i'm receiving are as follows:

A PHP Error was encountered

Severity: Notice

Message: Undefined index:

Filename: models/People.php

Line Number: 66

Backtrace:

File: /home/student/927/w1375927/public_html/CI1/application/models/People.php Line: 66 Function: _error_handler

File: /home/student/927/w1375927/public_html/CI1/application/controllers/Guesser.php Line: 20 Function: isCorrectAnswer

File: /home/student/927/w1375927/public_html/CI1/index.php Line: 292 Function: require_once

and

A PHP Error was encountered

Severity: Notice

Message: Undefined index:

Filename: models/People.php

Line Number: 61

Backtrace:

File: /home/student/927/w1375927/public_html/CI1/application/models/People.php Line: 61 Function: _error_handler

File: /home/student/927/w1375927/public_html/CI1/application/controllers/Guesser.php Line: 21 Function: getPerson

File: /home/student/927/w1375927/public_html/CI1/index.php Line: 292 Function: require_once

I've edited the arrays in which the images are transferred from in between the model and the controller.

My assumption is that is involved with the configuration of codeigniter prior to running it. If anyone has encountered these errors before, I would appreciate any help given.

Thanks.

1

1 Answers

1
votes

You are not passing the arguments to the view correctly, in controller change to:

    <?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));
        }
    }
}

So then in the view you can access $data['newguess']; as $newguessnote that your form have no action too you need to specify in the html:

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