新文章未在codeigniter中提交

时间:2016-04-19 21:09:36

标签: php codeigniter-2

<?php
class Admin extends MY_Controller
    {
        public function dashboard()
            {
                /*
                if(! $this->session->userdata('user_id')) // This condition is check condition which will show dashboard only if person is logged in else will redirect to login page.
                    return redirect('login');
                */

                $this->load->model('articlesmodel','articles');  // This "articles" is the secondary name which we have given here and will be refering to.
                $articles = $this->articles->articles_list();   // If we have given the secondary name then we need to use that seconary name here too.
                // In above line, we will get the titles/title which will get stored in $articles.


                $this->load->view('admin/dashboard.php',['articles'=>$articles]); // we are passing the received articles in here.
                // The above 'articles' will be received by "dashboard.php" in the form of "$articles".
            }


            public function add_article()
            {
                $this->load->helper('form');
                $this->load->view('admin/add_article.php');
            }


            public function store_article()
            {
                // Previously we use to form validate from the same file. But, now we have set the rules and all in config's form_validation file.
                // We just need to pass the rule in the run like below.
                $this->load->library('form_validation');
                if($this->form_validation->run('add_articles_rules'))
                {
                    // If we pass the parameter inside post then we will get that value only else it will give all the inputs in the array. 
                    $post = $this->input->post();
                    unset($post['submit']); // this is to remove submit value received in $post array else it will give error.

                    $this->load->model('articlesmodel','articles');
                    if($this->articles->add_article($post))
                    {
                        echo "Insert sucessful";
                    }
                    else
                    {
                        echo "Insert uncessful";
                    }
                }

                else
                {
                    return redirect('admin/add_article');
                }
            }


            public function edit_article()
            {

            }

            public function delete_article()
            {

            }

            public function __construct()
                {
                    parent::__construct();
                    if(! $this->session->userdata('user_id'))
                        return redirect('login');
                }
                // Above constructor can be placed in MY_Controller but then we need to undo the "login extends My_Controller" or make it as 
                // "login extends CI_Controller" as in this "admin" file we can have more methods.
    }           
?>

0 个答案:

没有答案