未定义的变量CodeIgniter模型

时间:2016-09-02 17:33:20

标签: php codeigniter

我需要理解为什么country_id变量未在

中定义

模型

public function getCountry($country_id) {
    $this->db->select()->where('country_id', $country_id);
    $this->db->from('country');
    $query = $this->db->get();
    return $query->result(); 
}

控制器

public function country() {
     $json = array();
     $country_info = $this->country->getCountry($country_id);

     if ($country_info) {
         $json = array(
             'country_id'        => $country_info['country_id'],
             'name'              => $country_info['name'],
             'zone'              => $this->country->getZonesByCountryId($country_id),
             'status'            => $country_info['status']
         );
     }
     echo json_encode($json);
 }

结果:

  

消息:未定义的变量:country_id

     

文件名:localization / Countries.php

     

行号:12

1 个答案:

答案 0 :(得分:1)

  

消息:未定义的变量:country_id

这里没有定义$country_id变量。它没有任何意义,因为还没有分配给它。它突然冒出来。

public function country() {
     $json = array();
     $country_info = $this->country->getCountry($country_id);
     ....

您必须通过为其指定值来定义它....

public function country() 
{
     $json = array();
     $country_id = 3;  // <- define it here
     $country_info = $this->country->getCountry($country_id);
     ....

或者您可以将其作为函数的参数传递...

public function country($country_id) // <- pass it in
{
     $json = array();
     $country_info = $this->country->getCountry($country_id);
     ....