Autor Zpráva
veredico
Profil
Dobrý večer,
začal jsem si hrát s php frameworkem Codeigniter, rovnou jsem to chtěl pojmout jako projekt, který bych chtěl dokončit.
Bohužel jsem se zasekl na problému, jak získat data z jednoho ID, které ve view mám.
Popis:
1, Jsem v náhledu samotného příspěvku a data vidím.
2, Data z DB příspěvky vidím a v pořádku načtu.
3, Příspěvek, má ještě v jiné tabulce doplňující informace, něco jako katergorie.
4, ID kategorie vidím a správně.
Potřebuji:
V php view příspěvku na základě ID kategorie vytáhnou ostatní data z kategorie daného ID

Nevím zda jsem to popsal dostatečně, dle mého v controlleru potřebuji udělat join na carriers_id a post ID, ale ani za boha na to nemohu přijít.
Prosím, je zde dobrá duše, která mě naučí a vyřeší můj problém, abych mohl dál pokračovat ve svém vývoji?
Moc děkuji!


view.php
<h3 xmlns="http://www.w3.org/1999/html"><?php echo $post['title']; ?></h3>
<div class="row">
    <div class="col-3">
        <?php if (!$post['post_image'] or $post['post_image'] == "noimage.jpg") {
            ?>
            <img
                src="<?php echo site_url() ?>assets/css/no-photo.png"
                class="img-fluid">
        <?php } else { ?>
            <img
                src="<?php echo site_url() ?>assets/images/posts/<?php echo $post['post_image']; ?>"
                class="img-fluid">
        <?php } ?>

    </div>
    <div class="col-9">

        <small>Posted on: <?php echo $post['created_at']; ?></small> <?php echo $post['carriers_id']; ?></strong></small>

        <p><?php echo $post['body']; ?></p>
        <div id="printableArea">
            <table class="table printTable table-hover">
                <tbody>
                <tr>

                    <td colspan="2">
                        
                    </td>
                    <td colspan="2" class="storageLogistics">
                        <img
                            src="<?php echo site_url() ?>assets/css/storageLogistics.jpg"
                            class="">
                    </td>

                </tr>
                <tr>
<!--                    toto funguje-->
                    <td><?php echo $post['carriers_id']; ?></td>
<!--                    toto jiz ne-->
                    <td><?php echo $post['name']; ?></td>
<!--                    carriers je nazev db a name nazev colum-->
<!--                    --><?php //echo $post['carriers.name']; ?>
                    <td>Thornton</td>
                    <td>@fat</td>
                </tr>
                <tr>

                    <td>Larry</td>
                    <td>Larry</td>
                    <td>the Bird</td>
                    <td>@twitter</td>
                </tr>
                </tbody>
            </table>




        </div>
        <hr>
        <a href="<?php echo base_url() ?>posts/edit/<?php echo $post['slug']; ?>" class="btn btn-success float-left"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
        <a href="#" class="btn btn-warning float-left" onclick="printDiv('printableArea')"><i class="fa fa-print float-left" aria-hidden="true"></i></a>
        <?php
        echo form_open('/posts/delete/' . $post['id']);
        ?>
        <button type="submit" onclick="return confirm ('Opravdu mám záznam smazat? Po této akci se data momentálně nedají obnovit!')" class="btn btn-danger  float-right" value="Smazat"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
        </form>
    </div>
</div>

controllers
<?php
class Posts  extends CI_Controller{
    public function index(){

        $data['title'] = 'Případy';
        $data['posts'] = $this -> Post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = NULL){
        $data['post'] = $this-> Post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'Vytvořit případ';

        $data['carriers'] = $this->Post_model->get_carriers();

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('body', 'Body ');
        $this->form_validation->set_rules('speditionWarehouse', 'Doprava / Sklad', 'required');
        $this->form_validation->set_rules('importExport', 'Import / Export', 'required');
        $this->form_validation->set_rules('country', 'Stát', 'required');
        if ($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_FILES['userfile']['name'];
            }

            $this->Post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->Post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> Post_model -> get_posts($slug);
        $data['carrier'] = $this->Post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'Upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->Post_model->update_post();
        redirect('posts');
    }
}

Model
<?php
class Post_model extends CI_Model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = FALSE){
        if ($slug === FALSE){
            $this->db->order_by('posts.id', 'DESC');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timeStamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionWarehouse' => $this->input->post('speditionWarehouse'),
            'importExport' => $this->input->post('importExport'),
            'country' => $this->input->post('country'),
            'timeStamp' => $this->input->post('timeStamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'DESC');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}

<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}

<?php
class posts  extends ci_controller{
    public function index(){

        $data['title'] = 'případy';
        $data['posts'] = $this -> post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = null){
        $data['post'] = $this-> post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'vytvořit případ';

        $data['carriers'] = $this->post_model->get_carriers();

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('body', 'body ');
        $this->form_validation->set_rules('speditionwarehouse', 'doprava / sklad', 'required');
        $this->form_validation->set_rules('importexport', 'import / export', 'required');
        $this->form_validation->set_rules('country', 'stát', 'required');
        if ($this->form_validation->run() === false){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_files['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> post_model -> get_posts($slug);
        $data['carrier'] = $this->post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->post_model->update_post();
        redirect('posts');
    }
}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}

controllers
<?php
class posts  extends ci_controller{
    public function index(){

        $data['title'] = 'případy';
        $data['posts'] = $this -> post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = null){
        $data['post'] = $this-> post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'vytvořit případ';

        $data['carriers'] = $this->post_model->get_carriers();

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('body', 'body ');
        $this->form_validation->set_rules('speditionwarehouse', 'doprava / sklad', 'required');
        $this->form_validation->set_rules('importexport', 'import / export', 'required');
        $this->form_validation->set_rules('country', 'stát', 'required');
        if ($this->form_validation->run() === false){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_files['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> post_model -> get_posts($slug);
        $data['carrier'] = $this->post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->post_model->update_post();
        redirect('posts');
    }
}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}

<h3 xmlns="http://www.w3.org/1999/html"><?php echo $post['title']; ?></h3>
<div class="row">
    <div class="col-3">
        <?php if (!$post['post_image'] or $post['post_image'] == "noimage.jpg") {
            ?>
            <img
                src="<?php echo site_url() ?>assets/css/no-photo.png"
                class="img-fluid">
        <?php } else { ?>
            <img
                src="<?php echo site_url() ?>assets/images/posts/<?php echo $post['post_image']; ?>"
                class="img-fluid">
        <?php } ?>

    </div>
    <div class="col-9">

        <small>posted on: <?php echo $post['created_at']; ?></small> <?php echo $post['carriers_id']; ?></strong></small>

        <p><?php echo $post['body']; ?></p>
        <div id="printablearea">
            <table class="table printtable table-hover">
                <tbody>
                <tr>

                    <td colspan="2">
                        
                    </td>
                    <td colspan="2" class="storagelogistics">
                        <img
                            src="<?php echo site_url() ?>assets/css/storagelogistics.jpg"
                            class="">
                    </td>

                </tr>
                <tr>
<!--                    toto funguje-->
                    <td><?php echo $post['carriers_id']; ?></td>
<!--                    toto jiz ne-->
                    <td><?php echo $post['name']; ?></td>
<!--                    carriers je nazev db a name nazev colum-->
<!--                    --><?php //echo $post['carriers.name']; ?>
                    <td>thornton</td>
                    <td>@fat</td>
                </tr>
                <tr>

                    <td>larry</td>
                    <td>larry</td>
                    <td>the bird</td>
                    <td>@twitter</td>
                </tr>
                </tbody>
            </table>




        </div>
        <hr>
        <a href="<?php echo base_url() ?>posts/edit/<?php echo $post['slug']; ?>" class="btn btn-success float-left"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
        <a href="#" class="btn btn-warning float-left" onclick="printdiv('printablearea')"><i class="fa fa-print float-left" aria-hidden="true"></i></a>
        <?php
        echo form_open('/posts/delete/' . $post['id']);
        ?>
        <button type="submit" onclick="return confirm ('opravdu mám záznam smazat? po této akci se data momentálně nedají obnovit!')" class="btn btn-danger  float-right" value="smazat"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
        </form>
    </div>
</div>

controllers
<?php
class posts  extends ci_controller{
    public function index(){

        $data['title'] = 'případy';
        $data['posts'] = $this -> post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = null){
        $data['post'] = $this-> post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'vytvořit případ';

        $data['carriers'] = $this->post_model->get_carriers();

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('body', 'body ');
        $this->form_validation->set_rules('speditionwarehouse', 'doprava / sklad', 'required');
        $this->form_validation->set_rules('importexport', 'import / export', 'required');
        $this->form_validation->set_rules('country', 'stát', 'required');
        if ($this->form_validation->run() === false){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_files['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> post_model -> get_posts($slug);
        $data['carrier'] = $this->post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->post_model->update_post();
        redirect('posts');
    }
}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}


view.php
<h3 xmlns="http://www.w3.org/1999/html"><?php echo $post['title']; ?></h3>
<div class="row">
    <div class="col-3">
        <?php if (!$post['post_image'] or $post['post_image'] == "noimage.jpg") {
            ?>
            <img
                src="<?php echo site_url() ?>assets/css/no-photo.png"
                class="img-fluid">
        <?php } else { ?>
            <img
                src="<?php echo site_url() ?>assets/images/posts/<?php echo $post['post_image']; ?>"
                class="img-fluid">
        <?php } ?>

    </div>
    <div class="col-9">

        <small>posted on: <?php echo $post['created_at']; ?></small> <?php echo $post['carriers_id']; ?></strong></small>

        <p><?php echo $post['body']; ?></p>
        <div id="printablearea">
            <table class="table printtable table-hover">
                <tbody>
                <tr>

                    <td colspan="2">
                        
                    </td>
                    <td colspan="2" class="storagelogistics">
                        <img
                            src="<?php echo site_url() ?>assets/css/storagelogistics.jpg"
                            class="">
                    </td>

                </tr>
                <tr>
<!--                    toto funguje-->
                    <td><?php echo $post['carriers_id']; ?></td>
<!--                    toto jiz ne-->
                    <td><?php echo $post['name']; ?></td>
<!--                    carriers je nazev db a name nazev colum-->
<!--                    --><?php //echo $post['carriers.name']; ?>
                    <td>thornton</td>
                    <td>@fat</td>
                </tr>
                <tr>

                    <td>larry</td>
                    <td>larry</td>
                    <td>the bird</td>
                    <td>@twitter</td>
                </tr>
                </tbody>
            </table>




        </div>
        <hr>
        <a href="<?php echo base_url() ?>posts/edit/<?php echo $post['slug']; ?>" class="btn btn-success float-left"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
        <a href="#" class="btn btn-warning float-left" onclick="printdiv('printablearea')"><i class="fa fa-print float-left" aria-hidden="true"></i></a>
        <?php
        echo form_open('/posts/delete/' . $post['id']);
        ?>
        <button type="submit" onclick="return confirm ('opravdu mám záznam smazat? po této akci se data momentálně nedají obnovit!')" class="btn btn-danger  float-right" value="smazat"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
        </form>
    </div>
</div>

controllers
<?php
class posts  extends ci_controller{
    public function index(){

        $data['title'] = 'případy';
        $data['posts'] = $this -> post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = null){
        $data['post'] = $this-> post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'vytvořit případ';

        $data['carriers'] = $this->post_model->get_carriers();

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('body', 'body ');
        $this->form_validation->set_rules('speditionwarehouse', 'doprava / sklad', 'required');
        $this->form_validation->set_rules('importexport', 'import / export', 'required');
        $this->form_validation->set_rules('country', 'stát', 'required');
        if ($this->form_validation->run() === false){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_files['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> post_model -> get_posts($slug);
        $data['carrier'] = $this->post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->post_model->update_post();
        redirect('posts');
    }
}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slug = url_title($this->input->post('timestamp'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),
            'post_image' => $post_image,
            'speditionwarehouse' => $this->input->post('speditionwarehouse'),
            'importexport' => $this->input->post('importexport'),
            'country' => $this->input->post('country'),
            'timestamp' => $this->input->post('timestamp'),
            'week' => $this->input->post('week'),
        );
        return $this->db->insert('posts', $data);
    }

    public function delete_post($id){
         $this->db->where('id', $id);
         $this->db->delete('posts');
         return true;
    }
    public function  update_post(){
//        echo $this->input->post('id'); die();

        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'carriers_id' => $this->input->post('carriers_id'),

        );
        $this->db->where('id', $this->input->post('id'));
        return $this->db->update('posts', $data);

    }

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

    public function get_posts_by_carriers($carriers_id){
        $this->db->order_by('posts.id', 'desc');
        $this->db->join('carriers', 'carriers.id = posts.carriers_id');
        $query = $this->db->get_where('posts', array('carriers_id' => $carriers_id));
        return $query->result_array();
    }

}



view.php
<h3 xmlns="http://www.w3.org/1999/html"><?php echo $post['title']; ?></h3>
<div class="row">
    <div class="col-3">
        <?php if (!$post['post_image'] or $post['post_image'] == "noimage.jpg") {
            ?>
            <img
                src="<?php echo site_url() ?>assets/css/no-photo.png"
                class="img-fluid">
        <?php } else { ?>
            <img
                src="<?php echo site_url() ?>assets/images/posts/<?php echo $post['post_image']; ?>"
                class="img-fluid">
        <?php } ?>

    </div>
    <div class="col-9">

        <small>posted on: <?php echo $post['created_at']; ?></small> <?php echo $post['carriers_id']; ?></strong></small>

        <p><?php echo $post['body']; ?></p>
        <div id="printablearea">
            <table class="table printtable table-hover">
                <tbody>
                <tr>

                    <td colspan="2">
                        
                    </td>
                    <td colspan="2" class="storagelogistics">
                        <img
                            src="<?php echo site_url() ?>assets/css/storagelogistics.jpg"
                            class="">
                    </td>

                </tr>
                <tr>
<!--                    toto funguje-->
                    <td><?php echo $post['carriers_id']; ?></td>
<!--                    toto jiz ne-->
                    <td><?php echo $post['name']; ?></td>
<!--                    carriers je nazev db a name nazev colum-->
<!--                    --><?php //echo $post['carriers.name']; ?>
                    <td>thornton</td>
                    <td>@fat</td>
                </tr>
                <tr>

                    <td>larry</td>
                    <td>larry</td>
                    <td>the bird</td>
                    <td>@twitter</td>
                </tr>
                </tbody>
            </table>




        </div>
        <hr>
        <a href="<?php echo base_url() ?>posts/edit/<?php echo $post['slug']; ?>" class="btn btn-success float-left"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
        <a href="#" class="btn btn-warning float-left" onclick="printdiv('printablearea')"><i class="fa fa-print float-left" aria-hidden="true"></i></a>
        <?php
        echo form_open('/posts/delete/' . $post['id']);
        ?>
        <button type="submit" onclick="return confirm ('opravdu mám záznam smazat? po této akci se data momentálně nedají obnovit!')" class="btn btn-danger  float-right" value="smazat"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
        </form>
    </div>
</div>

controllers
<?php
class posts  extends ci_controller{
    public function index(){

        $data['title'] = 'případy';
        $data['posts'] = $this -> post_model -> get_posts();
//        print_r($data['posts']);


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

    public function view($slug = null){
        $data['post'] = $this-> post_model -> get_posts($slug);
        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = $data['post']['title'];
//        $data = $this->db->get("carriers")->row();
        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');
    }





    public function create(){
        $data['title'] = 'vytvořit případ';

        $data['carriers'] = $this->post_model->get_carriers();

        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('body', 'body ');
        $this->form_validation->set_rules('speditionwarehouse', 'doprava / sklad', 'required');
        $this->form_validation->set_rules('importexport', 'import / export', 'required');
        $this->form_validation->set_rules('country', 'stát', 'required');
        if ($this->form_validation->run() === false){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './assets/images/posts';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048 ';
            $config['max_width'] = '1920';
            $config['max_height'] = '1080';

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

            if (!$this->upload->do_upload()){
                $errors = array('error'=>$this->upload->display_errors());
                $post_image = 'noimage.jpg';
            } else {
                $data = array('upload_data'=>$this->upload->data());
                $post_image = $_files['userfile']['name'];
            }

            $this->post_model->create_post($post_image);
            redirect('posts');
        }
    }

    public function delete($id){
        $this->post_model->delete_post($id);
        redirect('posts');
    }

    public function edit($slug){
        $data['post'] = $this-> post_model -> get_posts($slug);
        $data['carrier'] = $this->post_model->get_carriers();

        if (empty($data['post'])){
            show_404();
        }
        $data['title'] = 'upravit případ';
        $this->load->view('templates/header');
        $this->load->view('posts/edit', $data);
        $this->load->view('templates/footer');
    }
    public function update(){
        $this->post_model->update_post();
        redirect('posts');
    }
}

model
<?php
class post_model extends ci_model{
    public function __construct()
    {
        $this->load->database();
    }


    public function get_posts($slug = false){
        if ($slug === false){
            $this->db->order_by('posts.id', 'desc');
            $this->db->join('carriers', 'carriers.id = posts.carriers_id');
            $query = $this->db->get('posts');
            return $query->result_array();
        }
        $query = $this->db->get_where('posts', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_post($post_image){
        $slu
    

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0