2013-03-07 206 views
1

我在這裏失去了一些東西,我無法通過控制器獲取變量通過模型傳遞到視圖。CodeIgniter PHP查詢MySQL查詢

我收到:該網站在檢索網站時遇到錯誤。它可能因維護而關閉或配置不正確。當前。

如果我刪除第二$這個 - >負載>模型( 'testingsearch');然後我收到以下錯誤信息:

遇到

一個PHP錯誤

嚴重性:注意

消息:未定義的屬性:HW :: $ testingsearch

文件名:控制器/hw.php

行號:63

MODEL:

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

} 

} 

VIEW:

<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> 
+0

評論回聲$結果的擴展名錯誤;返回後;步驟和print_r($結果)從模型,看看你到達那裏。 – Nish 2013-03-07 07:06:40

+0

不應該'擴展模型'是'擴展CI_Model'? – 2013-03-07 07:09:26

+0

@Nish沒有變化......神祕不錯,但是仍然沒有變化 – Andrew 2013-03-07 07:12:20

回答

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> 

這將解決您的問題.................

+1

完美!謝謝!!! – Andrew 2013-03-07 07:24:56

0

您還沒有發送任何數據到控制器。只有你發送模型函數返回true或false的指示符。返回對象

MODEL:

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

VIEW:

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

您的MODEL

// 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'); 
    } 
}