2010-12-12 19 views
0

嗨 我下面的http://www.ibm.com/developerworks/web/library/wa-codeigniter/笨MVC示例代碼

給出Codeigniterr的入門指南我按照指令創建前視圖,並添加控制器來處理表單提交。理想情況下,當我提交表單時,它應該加載模型類並執行該函數以在數據庫上放置細節,而只是在瀏覽器中打印模型的代碼。

**Code of view (Welcome.php)** 
---------------- 
<?php 

class Welcome extends Controller { 

function Welcome() 
{ 
    parent::Controller(); 
} 

function index() 
{ 

    $this->load->helper('form'); 
    $data['title'] = "Welcome to our Site"; 
    $data['headline'] = "Welcome!"; 
    $data['include'] = 'home'; 
    $this->load->vars($data); 
    $this->load->view('template'); 

} 
function contactus(){ 
    $this->load->helper('url'); 
    $this->load->model('mcontacts','',TRUE); 
    $this->mcontacts->addContact(); 
    redirect('welcome/thankyou','refresh'); 
} 


function thankyou(){ 
    $data['title'] = "Thank You!"; 
    $data['headline'] = "Thanks!"; 
    $data['include'] = 'thanks'; 
    $this->load->vars($data); 
    $this->load->view('template'); 
} 



} 

/* End of file welcome.php */ 
/* Location: ./system/application/controllers/welcome.php */ 

**Code of Model** 
-------------- 
class mcontacts extends Model{ 

    function mcontacts(){ 
    parent::Model(); 
    } 
} 

function addContact(){ 
    $now = date("Y-m-d H:i:s"); 
    $data = array( 
    'name' => $this->input->xss_clean($this->input->post('name')), 
    'email' => $this->input->xss_clean($this->input->post('email')), 
    'notes' => $this->input->xss_clean($this->input->post('notes')), 
    'ipaddress' => $this->input->ip_address(), 
    'stamp' => $now 
); 

    $this->db->insert('contacts', $data); 
} 

**OUTPUT after clicking submit** 
----------------------------- 
class mcontacts extends Model{ function mcontacts(){ parent::Model(); } } function addContact(){ $now = date("Y-m-d H:i:s"); $data = array('name' => $this->input->xss_clean($this->input->post('name')), 'email' => $this->input->xss_clean($this->input->post('email')), 'notes' => $this->input->xss_clean($this->input->post('notes')), 'ipaddress' => $this->input->ip_address(), 'stamp' => $now); $this->db->insert('contacts', $data); } 

我曾嘗試做這些事情 1.使所有PHP代碼的可執行文件的 2.更改所有權www數據 3.整個WWW

的化妝許可777但是,代碼的模型似乎只是打印...請幫助

+1

您可以嘗試重新格式化您的問題以使其更具可讀性嗎? – musoNic80 2010-12-12 22:18:43

回答

0

很難看到格式化,因爲它是,但在模型中的構造方法(mcontacts())之後有一個額外的大括號?這會造成問題!此外,雖然代碼看起來總體上沒問題,但如果您做的任何事情比您展示的更復雜,則可能有更好的方法來使用該框架。例如,自動加載,表單驗證等。我可以建議你閱讀用戶指南嗎?這是非常徹底和清晰,應該幫助你很多。 http://codeigniter.com/user_guide/index.html

1

只是一些小點,可以幫助你:

  1. 在您的控制器,指向你想在該網頁上調用該方法的指數方法。例如:

    function index() 
    { 
        $this->welcome(); 
    } 
    

    這將有助於保持事情的清晰和明確,特別是如果有其他人在以後與您一起處理代碼時。我選擇了歡迎詞,因爲這是您的控制器類的名稱,這將使事情變得簡單。

  2. 在你的模型,這樣的:

    class mcontacts extends Model{ 
    

    應該是:

    class Mcontacts extends Model{ 
    

    大寫的類名!這可能會給你造成麻煩。

    在這裏看到更多的信息在這:http://codeigniter.com/user_guide/general/models.html

  3. 不要在類或方法名使用駱駝情況。這不會導致你的代碼失敗,但這是普遍接受的做法。有關詳細信息,請參閱Codeigniter的PHP樣式指南:http://codeigniter.com/user_guide/general/styleguide.html