【问题标题】:I could not retrive a value from controller page我无法从控制器页面检索值
【发布时间】:2015-08-14 06:32:55
【问题描述】:

我正在使用 codeigniter 框架。我有一张名为client 的表。我需要从表中获取名字并将其发送到我的视图页面。我在模型文件中有一个函数来检索客户端数据。 这是我的模型函数:

public function get_cli($id)
    {
    $this->db->select('*');
        $this->db->from('client');
        $this->db->where('id', $id);
        $res = $query->result();
        $row = $res[0];
        return $row->first_name;

    }

在这里,我得到了我的客户的名字。 这是我的控制器文件。这里我有一个函数可以将数据从客户端表发送到我的视图页面。

public function ticket($id)
    {
        $id=$this->uri->segment(4);
        $data['id']=$this->uri->segment(4);
        $data['client']=$this->billing_model->get_cli($id);
        $data['employee']=$this->employee_model->emp_name();
        $data['main_content'] = 'admin/billing/ticket_page';
        $this->load->view('includes/template', $data);  
     }

我的查看页面。

<label>Client Id:<?=$id?></label>
<input type="text" value="<?=$client['first_name'];?>"/>

我得到一个白屏。为什么会这样?有人可以帮我写代码吗?

【问题讨论】:

  • 你的error_reporting 开启了吗?如果不是,请使用error_reporting(E_ALL) 并在此处发布您遇到的错误
  • 将以下项目添加到您的 index.php 文件中:error_reporting(E_ALL)ini_set('display_errors',1)
  • 不,更改后我无法获得 first_name @sabinadhikari
  • 你在控制器中得到你的名字了吗?
  • 把它放在你的__construct函数error_reporting(E_ALL);

标签: php mysql codeigniter


【解决方案1】:

把你的模型函数改成这样:

public function get_cli($id)
{
    $this->db->select('*');
    $this->db->from('client');
    $this->db->where('id', $id);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result[0]['first_name']; //returning only the first name key value of first row(0th row) of the result;
}

然后在查看页面上:

<input type="text" value="<?=$client;?>"/>

这将只打印名字;

另一种方法:

把你的模型函数改成这样:

public function get_cli($id)
{
    $this->db->select('*');
    $this->db->from('client');
    $this->db->where('id', $id);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result; //returning the whole array;
}

然后在查看页面上:

<input type="text" value="<?=$client[0]['first_name'];?>"/>

这将只打印数组第 0(第一行)的名字值;

【讨论】:

  • 我试过这个,但我得到一个这样的错误。遇到 PHP 错误 严重性:通知消息:未定义变量:查询文件名:models/billing_model.php 行号:49 致命错误:调用到 C:\xampp\htdocs\elfanto\elfanto_billing\application\models\billing_model.php 中非对象的成员函数 result_array() 第 49 行
【解决方案2】:

你应该把你的视图页面改成这个

&lt;input type="text" value="&lt;?=$client;?&gt;" /&gt;

当您返回模型中的名字而不是整个数组时,这将起作用。 因此,您的 $client 变量将包含名字。你不需要引用它。 只需回显$client 即可。

【讨论】:

  • 不,更改后我无法获得 first_name @sabinadhikari
【解决方案3】:

当结果中只有单行时,我建议您使用 row_object() 而不是 result()

public function get_cli($id)
{
    $this->db->select('*');
    $this->db->from('client');
    $this->db->where('id', $id);
    $res = $query->row_object();
    //$row = $res[0];  no need to write this line
    return $res->first_name;

}

关于你的观点的问题 您将在$client 中获得first_name

<input type="text" value="<?=$client?>"/>

如果您没有得到任何结果,请检查您从数据库中获取的结果是否为空。

【讨论】:

    【解决方案4】:

    使用

    $row['first_name'] 而不是 $row->first_name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2021-08-21
      • 1970-01-01
      相关资源
      最近更新 更多