Welcome Guest, Not a member yet? Register   Sign In
Using a model to insert data
#1

[eluser]Todlerone[/eluser]
Hello all and thank-you in advance for any help/suggestions. I'm new to CI but not to PHP and OO programming. I have successfully made a CI site with a simple template (header, content, sidebars and footer). Everything works well until now. I'm trying to implement the database information now. I have autoloaded the database library (my site will be mostly viewing/updating data). I don't get any errors therefore I'm certain the database connection is fine. When I select my menu item to go to the form I created onscreen all I get is the code from my model class. Not sure if my call to the model is in the wrong location or not?

Code:
class Patient_add extends Controller {
    var $data;
    function Patient_add(){
        parent::Controller();
    }
    
    function index(){
        $this->load->model('add_new_pt');
        $this->title="Admin";
        $this->thisPage="Admin";
        
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('jcc_demo_NameL', 'Last Name', 'trim|required|alpha|max_length[30]');
        $this->form_validation->set_rules('jcc_demo_NameF', 'First Name', 'trim|required|alpha|max_length[25]');
        $this->form_validation->set_rules('jcc_demo_JCCid', 'JCC ID Number', 'trim|required|alpha_dash|max_length[9]');
        $this->form_validation->set_rules('jcc_demo_Gender', 'Gender', 'required|alpha|max_length[6]');
        $this->form_validation->set_rules('jcc_demo_Age', 'Age', 'trim|numeric|is_natural|max_length[3]');
        $this->form_validation->set_rules('jcc_demo_PrimaryDis', 'Primary Disease', 'trim|alpha');
        $this->form_validation->set_rules('jcc_demo_TNMstage', 'TNM stage', 'trim|alpha_numeric|max_length[15]');
        $this->form_validation->set_rules('jcc_demo_ConsultDate', 'Consult Date');
        $this->form_validation->set_rules('jcc_demo_ReferralDate', 'Referral Date');
        $this->form_validation->set_rules('jcc_demo_ReferralSource', 'Referral Source');
        $this->form_validation->set_rules('jcc_demo_ReferralImaging', 'Consult Date');
        $this->form_validation->set_rules('jcc_demo_ECOGstatus', 'ECOG status', 'trim|numeric|is_natural|max_length[2]');
        $this->form_validation->set_rules('jcc_demo_KPSstatus', 'KPS status', 'trim|numeric|is_natural|max_length[2]');
        $this->form_validation->set_rules('jcc_demo_NumLesions', '# of SRS Lesions', 'trim|numeric|is_natural|max_length[2]');
            
        if ($this->form_validation->run() == FALSE){
            $this->load->view('header');
            $this->load->view('admin/patient_add');
            $this->load->view('sidebars/sidebar_patientAdd');
            $this->load->view('footer');
        }else{
            $this->add_new_pt->add_pt();
            $this->load->view('header');
            $this->load->view('admin/patient_addsuccess');
            $this->load->view('sidebars/sidebar_patientAdd');
            $this->load->view('footer');
        }// end form_validation
        
          }// end index

    
}// end class

Here is my model code

Code:
class Add_new_pt extends Model {

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

    function add_pt(){
          
        $data = array(
            'jcc_demo_NameL' => $this->input->post('jcc_demo_NameL'),
            'jcc_demo_NameF' => $this->input->post('jcc_demo_NameF'),
            'jcc_demo_JCCid' => $this->input->post('jcc_demo_JCCid'),
            'jcc_demo_Gender' => $this->input->post('jcc_demo_Gender'),
            'jcc_demo_Age' => $this->input->post('jcc_demo_Age'),
            'jcc_demo_PrimaryDis' => $this->input->post('jcc_demo_PrimaryDis'),
            'jcc_demo_TNMstage' => $this->input->post('jcc_demo_TNMstage'),
            'jcc_demo_ConsultDate' => $this->input->post('jcc_demo_ConsultDate'),
            'jcc_demo_ReferralDate' => $this->input->post('jcc_demo_ReferralDate'),
            'jcc_demo_ReferralSource' => $this->input->post('jcc_demo_ReferralSource'),
            'jcc_demo_ReferralImaging' => $this->input->post('jcc_demo_ReferralImaging'),
            'jcc_demo_ECOGstatus' => $this->input->post('jcc_demo_ECOGstatus'),
            'jcc_demo_KPSstatus' => $this->input->post('jcc_demo_KPSstatus'),
            'jcc_demo_NumLesions' => $this->input->post('jcc_demo_NumLesions'));
        
        //$this->content = $_POST['userID'];
        //$this->date    = time();

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

} //end main
#2

[eluser]kaejiavo[/eluser]
Hi,

do you have the php tag at the very beginning of your model file?
Code:
<? php ...

Marco
#3

[eluser]kaejiavo[/eluser]
Another thing,
if you are using Codeigniter 2.0 you have to extend CI_Model in your Models (look here)

Code:
class Add_new_pt extends CI_Model {...



Marco
#4

[eluser]Todlerone[/eluser]
TY Marco. Do I feel dumb. It totally works now. I'm using CI 1.7.x

TY...TY...TY...
#5

[eluser]Watermark Studios[/eluser]
You might also want to consider using an Object Relational Mapper (ORM) like Doctrine or Datamapper OverZealous Edition. This would really simplify your database interaction.




Theme © iAndrew 2016 - Forum software by © MyBB