【问题标题】:CodeIgniter PHP MySQL Query to ViewCodeIgniter PHP MySQL 查询查看
【发布时间】:2013-02-22 06:53:50
【问题描述】:

我在这里遗漏了一些东西,我无法通过控制器将变量通过模型传递到视图。

我正在接收: 网站在检索网站时遇到错误。它可能因维护而停机或配置不正确。 当前。

如果我删除第二个 $this->load->model('testingsearch'); 然后我会收到以下错误消息:

遇到了 PHP 错误

严重性:通知

消息:未定义的属性:HW::$testingsearch

文件名:controllers/hw.php

行号:63

型号:

<?php

class TestingSearch extends Model
{
function Messages()
{
    parent::Model();
}

function getMessages($id)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where('id', $id);
    $result = $this->db->get('HWC');

    if (!$result) {
        return false;
    } else {
        return $result;
        echo $result;
    }
}
}
?>

控制器:

<?php

class HW extends CI_Controller {

function Thiscontroller()
{
  parent::Controller();
  $this->load->database(); // This should be autoloaded

  $this->load->model('testingsearch');
}


function id($id='') {
       $this->load->model('testingsearch');
         $data['records'] = $this->testingsearch->getMessages($id);

   $this->load->view('searchresults', $data);

}

}

查看:

<ul>
<?php foreach ($records->result() as $row) { ?>
<li><?php echo $row->id; ?></li>
<li><?php echo $row->ModelName; ?></li>
<li><?php echo $row->Color; ?></li>
<? } ?>
</ul>

【问题讨论】:

  • 评论 echo $result;返回后;从模型 step 和 print_r($result) 看看你得到了什么。
  • 不应该extends Modelextends CI_Model
  • @Nish 没有变化...神秘的好消息,是的,但结果仍然没有变化
  • $this-&gt;load-&gt;model('testingSearch');请检查加载模型的代码。
  • 你的模型应该扩展到 CI_MODEL 的 CORE

标签: php mysql arrays codeigniter variables


【解决方案1】:

您没有向控制器发送任何内容。只有您发送指示符,无论模型函数返回真还是假。返回对象

型号:

Class TestingSearch extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getMessages($id)
    {
        $this->db->distinct();
        $this->db->select('*');
        $this->db->where('id', $id);
        $result = $this->db->get('HWC');

        return $result->result();
    }
}

控制器:

class HW extends CI_Controller 
{
    function __construct()
    {
      parent::__construct();
      $this->load->database(); // This should be autoloaded

      $this->load->model('testingsearch');
    }

    function id($id='') 
    {
        $this->load->model('testingsearch');
        $data['records'] = $this->testingsearch->getMessages($id);
        $this->load->view('searchresults', $data);
    }
}

查看:

<ul>
    <?php foreach ($records->result() as $row) { ?>
        <li><?php echo $row->id; ?></li>
        <li><?php echo $row->ModelName; ?></li>
        <li><?php echo $row->Color; ?></li>
    <? } ?>
</ul>

【讨论】:

    【解决方案2】:

    试试这个: 请更改 ::::::

    型号代码:

    function getMessages($id)
    {
        $this->db->distinct();
        $this->db->select('*');
        $this->db->where('id', $id);
        $result = $this->db->get('HWC');
    
        return $result->result();
    }
    

    查看代码:-

    <ul>
    <?php foreach ($records as $row) { ?>
    <li><?php echo $row->id; ?></li>
    <li><?php echo $row->ModelName; ?></li>
    <li><?php echo $row->Color; ?></li>
    <? } ?>
    </ul>
    

    它会解决你的问题........

    【讨论】:

      【解决方案3】:

      你的模型扩展错误

       // model should extend CI_Model
          class TestingSearch extends CI_Model{
              // then add additional constructor to call the model
              function __construct(){
                    paret::__construct();
              }
          }
      
      
       // in controller
      class HW extends CI_Controller {
       // change your call to the constructor by using
       function HW(){
           parent::CI_Controller()
           // then load the model
           $this->load->model('testingsearch');
        }
       }
      

      【讨论】:

        猜你喜欢
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-24
        • 2011-10-18
        • 2012-01-23
        相关资源
        最近更新 更多