CodeIgniter编辑记录按钮不起作用

时间:2017-08-17 13:31:30

标签: php codeigniter edit

开始使用CodeIgniter,当我按下edin帖子时,我收到错误 “没找到对象!

在此服务器上找不到请求的URL。引用页面上的链接似乎是错误的或过时的。请告知该页面的作者有关错误的信息。

如果您认为这是服务器错误,请与网站管理员联系。“

我改变了一切,从控制器,模型和视图开始,但没有任何事情发生,我不知道我在哪里犯了错误

帖子控制器

<?php


class Posts extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->database();   
        $this->load->library('upload');

    }

    public function index($offset = 0){

        $config['base_url'] = base_url() . 'posts/index/';
        $config['total_rows'] = $this->db->count_all('posts');
        $config['per_page'] = 3;
        $config['uri_segment'] = 3;
        $config['attributes'] = array('class' => 'pagination-link');

        $this->pagination->initialize($config);

        $data['posts']= $this->Posts_model->get_posts(FALSE,$config['per_page'],$offset);

        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }


    public function view($mjestoOdredista=NULL){


        $data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
        $post_id = $data['posts']['id'];
        $data['comments'] = $this->comment_model->get_comments($post_id);




        if(empty($data['posts'])){
            show_404();
        }
        $data['id'] =$data['posts'];

        $this->load->view('templates/header');
        $this->load->view('posts/view',$data);
        $this->load->view('templates/footer');
    }


    public function create(){
        //check if user is logged in
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

       $this->form_validation->set_rules('mjestoPolaska', 'Mjesto Polaska', 'required');
       $this->form_validation->set_rules('mjestoOdredista', 'Mjesto Odredista', 'required');
       $this->form_validation->set_rules('datumPolaska', 'Datum Polaska', 'required');
       $this->form_validation->set_rules('datumPovratka', 'Datum Odredista', 'required');
       $this->form_validation->set_rules('cijena', 'Cijena', 'required');
       $this->form_validation->set_rules('brojMjesta', 'Broj mjesta', 'required');
      // $this->form_validation->set_rules('opis', 'Opis', 'required');
      // $this->form_validation->set_rules('post_image', 'Image', 'required');

        $data['title'] ='Create Posts';
        $data['categories'] = $this->Posts_model->get_categories();



        if($this->form_validation->run()===FALSE){

            $this->load->view('templates/header');
            $this->load->view('posts/create',$data);
            $this->load->view('templates/footer');

        }else {

            //upload image
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '500';
            $config['max_height'] = '500';

            $this->load->library('upload');


            if(!$this->upload->do_upload()){
                $error=array('error'=>$this->upload->display_errors());
                $post_image='noimage.jpg';
            }else{
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_FILES['userfile']['name'];
            }
            $this->Posts_model->create_post($post_image);
            $this->session->set_flashdata('post_creted', 'You post has been created') ;
            redirect('posts');
        }

    }

    public function delete($id){

        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }


        $this->Posts_model->delete_post($id);
        $this->session->set_flashdata('post_deleted', 'You post has been deleted  ') ;
        redirect('posts');
    }



    public function edit($slug){
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

        $data['mjestoOdredista']= $this->Posts_model->get_posts($slug);

        //Check if user is logged in
        if($this->session->userdata('user_id') != $this->Posts_model->get_posts($slug)['user_id']){
            redirect('posts');
        }

        $data['categories'] = $this->Posts_model->get_categories();

        if(empty($data['slug'])){
            show_404();
        }

        $data['id'] = 'Edit Post';
        $this->load->view('templates/header');
        $this->load->view('posts/edit',$data);
        $this->load->view('templates/footer');
    }


    public function update(){
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }

        $this->Posts_model->update_post();
        $this->session->set_flashdata('post_updated', 'You post has been updated ') ;
        redirect('posts');

    }

    }

?>

帖子模型

<?php


class Posts_Model extends CI_Model{

    public function __construct(){
        $this->load->database();

    }

    function get_posts($mjestoOdredista=FALSE, $limit = FALSE,$offset = FALSE){
        if($limit){
            $this->db->limit($limit,$offset);
        }
        if($mjestoOdredista === FALSE){

            $this->db->order_by('posts.id','DESC');
            $this->db->join('categories','categories.id = posts.category_id');
            $query=$this->db->get('posts');
            return $query->result_array();
        }

        $query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
        return $query->row_array();
    }

    //Kreiranje post
    public function create_post($post_image){
        $mjestoPolaska = url_title($this->input->post('title'));
        $data=array(
                'mjestoPolaska' => $mjestoPolaska,
                'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
                'datumPolaska' => $this->input ->post('datumPolaska'),
                'datumPovratka' => $this->input ->post('datumPovratka'),
                'brojMjesta' => $this->input ->post('brojMjesta'),
                'cijena' => $this->input ->post('cijena'),
                'opis' => $this->input ->post('opis'),
                'category_id'=>$this->input->post('category_id'),
                'user_id' =>$this->session->userdata('user_id'),
                'post_image'=>$post_image

        );

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

    //Brisanje posta
    public function delete_post($id){
        $this->db->where('id',$id);
        $this->db->delete('posts');
        return true;
    }


    //editovanje posta
    public function update_post(){
        $slug = url_title($this->input->posts('title'));
        $data=array(
                'slug' =>            $slug,
                'id'=>               $this->input->posts('id'),
                'category_id'=>      $this->input->post('category_id'),
                'user_id' =>         $this->session->userdata('user_id'),
                'mjestoPolaska' =>   $this->input->posts('mjestoPolaska'),
                'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
                'datumPolaska' =>    $this->input ->post('datumPolaska'),
                'datumPovratka' =>   $this->input ->post('datumPovratka'),
                'cijena' =>          $this->input ->post('cijena'),
                'brojMjesta' =>      $this->input ->post('brojMjesta'),
                'opis' =>            $this->input ->post('opis'),
                'post_image'=>       $post_image   
        );
        $this->db->where('id', $this->input->posts('id'));
        return $this->db->update('posts', $data);

    }

    public function get_categories(){
        $this->db->order_by('name');
        $query = $this->db->get('categories');
        return $query->result_array();
    }

    public function get_posts_by_category($category_id){

        $this->db->order_by('posts.id','DESC');

        $this->db->join('categories','categories.id = posts.category_id');
        $query=$this->db->get_where('posts',array('category_id'=>   $category_id));
        return $query->result_array();

    }

}



?>

修改视图

<h2><?= $title;?></h2>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
  $( function() {
    $( "#datepicker" ).datepicker();
    $( "#datepicker1" ).datepicker();
  } );
  </script>


<?php echo form_open_multipart('/posts/edit');?>
<?php echo validation_errors();?>

    <div class="row">
        <div class="col-md-4 col-md-offset-4">

                <div class="form-group">
                    <label>Mjesto Polaska</label>
                    <input type="text" class="form-control" name="mjestoPolaska" placeholder="Mjesto Polaska">
                </div>


                <div class="form-group">
                    <label>Mjesto Odredista</label>
                    <input type="text" class="form-control" name="mjestoOdredista" placeholder="Mjesto Odredista">
                </div>

                <div class="form-group">
                    <label>Datum Polaska</label>
                     <input type="text" id="datepicker" class="form-control" name="datumPolaska" placeholder ="Datum Polaska" >
                </div>

                <div class="form-group">
                    <label>Datum Povratka</label>
                     <input type="text" id="datepicker1" class="form-control" name="datumPovratka" placeholder="Datum Povratka">
                </div>



                <div class="form-group">
                <label>Cijena</label>
                <input type="text" class="form-control" name="cijena" placeholder="Cijena">
                </div>

                <div class="form-group">
              <label for="select">Broj slobodnih mjesta</label>
                  <select class="form-control" id="select", name="brojMjesta">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                  </select>
                </div>


            <div class="form-group">
            <label for="Kategorije">Kategorija</label>
            <div>
            <select name="category_id" class="form_control">
            <?php foreach($categories as $category):?>
            <option value="<?php echo $category['id'];?>"><?php echo $category['name'];?></option>
            <?php endforeach;?>
            </select>
            </div>
            </div>

            <div class="form-group">
             <label>Postavi sliku:</label>
             <p><b>samo oni koji nude prevoz nek postave sliku svojeg vozila</b></p>
             <input type="file" name="userfile" size="20">
             </div>

             <div class="form-group">
              <label>Opis:</label>
              <textarea class="form-control" rows="5" id="comment" name="opis"></textarea>
            </div>
                <button type="submit" class="btn btn-primary btn-block">Submit</button>

            </div>
    </div>

.htaccess文件

RewriteEngine on RewriteCond $ 1!^(index.php | assets | images | js | css | uploads | favicon.png) RewriteCond%(REQUEST_FILENAME)! - f RewriteCond%(REQUEST_FILENAME)! - d RewriteRule ^(。*)$。/ index.php / $ 1 [L]

1 个答案:

答案 0 :(得分:0)

用这个

替换你的htaccess文件
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /CS/index.php/$1 [L,QSA]
</IfModule>

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /CS/index.php?$1 [L]