2015-11-12 143 views
1

我正在嘗試創建一個我是誰的測驗,其中用戶顯示圖像,並且他們選擇他們認爲圖像在Codeigniter框架內的人,並且我收到了幾個錯誤。代碼如下: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

文件:/家庭/學生/ 927/w1375927 /的public_html/CI1 /指數.php行: 292 功能:require_once

甲PHP錯誤遇到

嚴重性:注意

消息:未定義指數:

文件名:型號/ 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的配置有關。如果有人遇到過這些錯誤,我將不勝感激。

謝謝。

回答

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">