2013-04-22 57 views
0

我發佈波紋管的代碼工作得很好,並且頁面顯示正確。我該如何改進我的Codeigniter代碼?

控制器:

class Articles extends Frontend_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->homeMetaTags = $this->configOptionsModel->get(1); 
     $this->bgimage = $this->configOptionsModel->get(2); 
     $this->db->where('catid = "1"'); 
     $this->services = $this->articlesModel->get(); 
     $this->projects = $this->projectsModel->get(); 
    } 

    public function displayArticle() 
{ 

     $data['default_metatags'] = $this->homeMetaTags; 
     $data['bgimage'] = $this->bgimage; 
     $data['services'] = $this->services; 
     $data['projects'] = $this->projects; 
     $this->load->model('articlesModel'); 
     $artid = $this->uri->segment(3); 
     $this->db->where('artid ='.$artid); 
     $data['article'] = $this->articlesModel->get(); 
     $data['main_content'] = 'articles'; 
     $this->load->model('articlesImagesModel'); 
     $this->db->where('artid ='.$artid); 
     $data['artimg'] = $this->articlesImagesModel->get(); 
    $this->load->view('frontEnd/template',$data);  
} 
} 

查看:

<?php echo $article[0]['title'];?> 
<?php echo $article[0]['articletext'];?> 

在地址欄中會顯示以下網址:

www.mysite.com/articles/displayArticle/3 到目前爲止, 太好了。

但是......如果我在地址欄輸入:www.mysite.com/articles/displayArticle/3546666

而是被重定向到自定義404錯誤頁面,我得到以下錯誤:

A PHP Error was encountered 
Severity: Notice 
Message: Undefined offset: 0 
Filename: frontEnd/articles.php 
Line Number: 12 

A PHP Error was encountered 
Severity: Notice 
Message: Undefined offset: 0 
Filename: frontEnd/articles.php 
Line Number: 48 

果然,第12行和第48行是視圖中代碼所在的兩行。

我想如果有人鍵入非現有的ID被重定向到404頁面,而不是看到這些errors.Anyone可以幫助嗎?

問候,卓然

+0

發佈你的模型代碼有一些問題,它 – 2013-04-22 08:55:40

回答

1
class Articles extends Frontend_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->homeMetaTags  = $this->configOptionsModel->get(1); 
     $this->bgimage   = $this->configOptionsModel->get(2); 
     $this->db->where('catid',1);// What is this doing here. This should be in model 
     $this->services   = $this->articlesModel->get(); 
     $this->projects   = $this->projectsModel->get(); 
    } 

    public function displayArticle() 
    { 
     $this->load->model('articlesModel'); 
     $this->load->model('articlesImagesModel'); 

     $data['default_metatags'] = $this->homeMetaTags; 
     $data['bgimage']   = $this->bgimage; 
     $data['services']   = $this->services; 
     $data['projects']   = $this->projects; 

     $artid = $this->uri->segment(3); 

     $this->db->where('artid',$artid);// What is this doing here. This should be in model 

     $data['article']   = $this->articlesModel->get(); 
     $data['main_content']  = 'articles'; 
     /* What is it doing here */ 
     $this->db->where('artid ='.$artid); 

     $data['artimg']    = $this->articlesImagesModel->get(); 

     $this->load->view('frontEnd/template',$data);  
    } 
} 

當你調用$this->articlesModel->get()要返回 row_array(),而不是result_array()我承擔。該函數返回的對象不是 對象數組。改變你的模型row_array()result_array()或視圖

<?php echo $article['title'];?> 
<?php echo $article['articletext'];?> 

這應該做工精細

+0

確實,這在我的模型中:$ method = $ single? 'row_array':'result_array'; – Zoran 2013-04-22 09:21:35

0

嘗試正確使用視圖模型控制器模型。 當前您正在檢索控制器中的數據庫訪問權限。我建議你看這個開始。 http://www.youtube.com/watch?v=mWsAruzW3Jw

無論如何,展現出404頁使用此:

show_404(); 

404處理器:

/** 
* 404 Page Handler 
* 
* This function is similar to the show_error() function above 
* However, instead of the standard error template it displays 
* 404 errors. 
* 
* @access public 
* @return void 
*/ 
function show_404($page = '', $log_error = TRUE) 
{ 
    $_error =& load_class('Exceptions', 'core'); 
    $_error->show_404($page, $log_error); 
    exit; 
} 
0

1)您必須重新因子代碼。模型代碼不應該像控制器一樣搞亂(get,where函數)。你應該通過傳遞一些值來處理模型本身。 2)在這一行'$ this-> db-> where('artid ='。$ artid);'您應該在傳遞此函數之前驗證$ artid。

3)如果你有這樣的錯誤信息,使用try catch語句來顯示404頁面。

4)如果您正在重定向到404頁面,則應該正確記錄錯誤以便稍後進行修復。

5)如果需要通用功能,請將其保存在助手中並調用函數名稱。不要將整個代碼保留在構造函數中。這是最佳做法。

如果您需要其他任何東西,請問我。謝謝。我希望這能幫到您。

+0

嗨Sakthi ...你可以發佈驗證身份證的任何示例(代碼片段)嗎? – Zoran 2013-04-22 09:11:52

+0

當然。在這裏,你可以驗證ctype_digit()。請檢查codeignitor幫助程序庫。我不記得在編號。好久不見。 – Sakthi 2013-04-22 09:16:50