开发者

Codeigniter MVC Sample Code

Hi I am following the getting started guide for Codeigniterr given at http://www.ibm.com/developerworks/web/library/wa-codeigniter/

I have followed the instruction to create the front view and added controller to handle form submission. Ideally, when i submit the form, it should load the model class and execute the function to put details on the database, but instead it is just printing out the code of the model in the browser.

**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-&开发者_开发百科gt;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); } 

I have tried doing these things 1. Making all PHP codes executable 2. Change ownership of files to www-data 3. make permission 777 for whole of www

But, the code of model seems to be just printed ... PLEASE HELP


Just a few minor points that might help you:

  1. In your controller, point the index method at the method you would like to call on that page. For example:

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

    That will help keep things clean and clear, especially if anyone else comes in to work on the code with you later. I chose welcome because that's the name of your controller class and that will keep things simple.

  2. In your model, this:

    class mcontacts extends Model{
    

    Should be:

    class Mcontacts extends Model{
    

    Capitalize those class names! That could be giving you the trouble you describe.

    See here for more info on this: http://codeigniter.com/user_guide/general/models.html

  3. Don't use camel case in your class or method names. This isn't something that will cause your code to fail, but it's generally accepted practice. See Codeigniter's PHP Style guide for more information on this: http://codeigniter.com/user_guide/general/styleguide.html


It's difficult to see with the formatting as it is, but do have an extra curly brace after the constructor method (mcontacts()) in the model? This would cause problems! Also although the code looks generally ok, there are probably better ways to use the framework especially if you do anything more complicated than what you've shown. For example, autoloading, form validation etc. Can I suggest you have a read of the userguide? It's very thorough and clear and should help you alot. http://codeigniter.com/user_guide/index.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜