Skip to content
Advertisement

Call to undefined method CI_Loader::library()

When I want to submit a post a get this type of error. I try to change everything starting from database field to my view but every time when I run a code I get same error but can not see where is the problem

Posts Controller

<?php


class Posts extends CI_Controller{

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

    }

    public function index($page='home'){

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

        $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');

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



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

            $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->libary('upload', $config);

          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("Posts_model");
            $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($mjestoOdredista){
        if(!$this->session->userdata('logged_in')){
            redirect('users/login');

        }

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

        //Check if user is logged in
        if($this->session->userdata('user_id') !=$this->Posts_model->get_posts($mjestoOdredista)['user_id'])
            redirect('posts');
        $data['categories'] = $this->Posts_model->get_categories();

        if(empty($data['mjestoOdredista'])){
            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');

    }

    }

?>

Posts Model

<?php


class Posts_Model extends CI_Model{

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

    }

    public function get_posts($mjestoOdredista=FALSE){
        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' => $this-> input -> post('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(){
        $mjestoPolaska=url_title($this->input->post('Mjesto Polaska'));
        $data=array(
                'mjestoPolaska' => $mjestoPolaska,
                'mjestoOdredista' => $this->input ->post('Mjesto Odredista'),
                'datumPolaska' => $this->input ->post('Datum Polaska'),
                'datumPovratka' => $this->input ->post('Datum Povratka'),
                'brojMjesta' => $this->input ->post('Broj Mjesta'),
                '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

        );
        $this->db->where('id',$this->input->post('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();

    }

}



?>

The error is in line 74, error message: Call to undefined method CI_Loader::libary()

Advertisement

Answer

try this in your constructor

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

replace the line 74

 $this->upload->initialize($config);
 $this->load->library('upload', $config);

Enjoy! I hope. It will work. 🙂

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement