ridwanskaterock / cicool

Cicool is a multifunctional application that is used to facilitate your work in creating a system, CMS, E-Comerce and others
20 stars 13 forks source link

Reference table - show other field than ID #86

Closed akuljana closed 7 years ago

akuljana commented 7 years ago

Hi there, in this comment (https://codecanyon.net/comments/16672799) user wanted to show other field, not id in details page.

This was your answer:

class Model_blog extends MY_Model { public function get($q = null, $field = null, $limit = 0, $offset = 0, $select_field = []) { .... $this->db->join('blog_category', 'blog.category = blog_category.id', 'LEFT'); .... $query = $this->db->get($this->table_name); return $query->result(); } }

Can you tell me where do you put this class? Thank you!

ridwanskaterock commented 7 years ago

You can out this code on file

Application/models/model_blog.php

Thanks

Pada tanggal 28 Jun 2017 02:22, "akuljana" notifications@github.com menulis:

Hi there, in this comment (https://codecanyon.net/comments/16672799) user wanted to show other field, not id in details page.

This was your answer:

`class Model_blog extends MY_Model { public function get($q = null, $field = null, $limit = 0, $offset = 0, $select_field = []) { .... $this->db->join('blog_category', 'blog.category = blog_category.id', 'LEFT'); .... $query = $this->db->get($this->table_name);

return $query->result();

}

}`

Can you tell me where do you put this class? Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ridwanskaterock/cicool/issues/86, or mute the thread https://github.com/notifications/unsubscribe-auth/AI10rfbM5CZnszph8Z7ha0AETSlieOiiks5sIVZrgaJpZM4OHEz7 .

akuljana commented 7 years ago

this is model_blog.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_blog extends MY_Model {

    private $primary_key    = 'id';
    private $table_name     = 'blog';
    private $field_search   = ['title', 'content', 'image', 'category', 'created_at'];

    public function __construct()
    {
        $config = array(
            'primary_key'   => $this->primary_key,
            'table_name'    => $this->table_name,
            'field_search'  => $this->field_search,
         );

        parent::__construct($config);
    }

    public function count_all($q = null, $field = null)
    {
        $iterasi = 1;
        $num = count($this->field_search);
        $where = NULL;
        $q = $this->scurity($q);
        $field = $this->scurity($field);

        if (empty($field)) {
            foreach ($this->field_search as $field) {
                if ($iterasi == 1) {
                    $where .= $field . " LIKE '%" . $q . "%' ";
                } else {
                    $where .= "OR " . $field . " LIKE '%" . $q . "%' ";
                }
                $iterasi++;
            }

            $where = '('.$where.')';
        } else {
            $where .= "(" . $field . " LIKE '%" . $q . "%' )";
        }

        $this->db->where($where);
        $query = $this->db->get($this->table_name);

        return $query->num_rows();
    }

    public function get($q = null, $field = null, $limit = 0, $offset = 0, $select_field = [])
    {
        $iterasi = 1;
        $num = count($this->field_search);
        $where = NULL;
        $q = $this->scurity($q);
        $field = $this->scurity($field);

        if (empty($field)) {
            foreach ($this->field_search as $field) {
                if ($iterasi == 1) {
                    $where .= $field . " LIKE '%" . $q . "%' ";
                } else {
                    $where .= "OR " . $field . " LIKE '%" . $q . "%' ";
                }
                $iterasi++;
            }

            $where = '('.$where.')';
        } else {
            $where .= "(" . $field . " LIKE '%" . $q . "%' )";
        }

        if (is_array($select_field) AND count($select_field)) {
            $this->db->select($select_field);
        }

        $this->db->where($where);
        $this->db->limit($limit, $offset);
        $this->db->order_by($this->primary_key, "DESC");
        $query = $this->db->get($this->table_name);

        return $query->result();
    }

}

/* End of file Model_blog.php */
/* Location: ./application/models/Model_blog.php */

Where do you add:

$this->db->join('blog_category', 'blog.category = blog_category.id', 'LEFT');

ridwanskaterock commented 7 years ago

Hii you can add join code before this code

$query = $this->db->get($this->table_name); On function count_all() and get()

akuljana commented 7 years ago

This is not working. Could you please post here whole code of Model_blog.php that you tested and returning right joined tables?

ridwanskaterock commented 7 years ago

What if it does not work? If it does not work, can you show it?

akuljana commented 7 years ago

Can't show you, working on localhost and test server. But when I add code where you said, I'm getting this in error log: PHP Fatal error: Call to a member function result() on boolean in .../application/models/Model_blog.php on line 86

ridwanskaterock commented 7 years ago

on new version you will get code on model like this

still waiting for new version

you can copy code on model_blog.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_blog extends MY_Model {

    private $primary_key    = 'id';
    private $table_name     = 'blog';
    private $field_search   = ['title', 'content', 'image', 'category', 'created_at'];

    public function __construct()
    {
        $config = array(
            'primary_key'   => $this->primary_key,
            'table_name'    => $this->table_name,
            'field_search'  => $this->field_search,
         );

        parent::__construct($config);
    }

    public function count_all($q = null, $field = null)
    {
        $iterasi = 1;
        $num = count($this->field_search);
        $where = NULL;
        $q = $this->scurity($q);
        $field = $this->scurity($field);

        if (empty($field)) {
            foreach ($this->field_search as $field) {
                if ($iterasi == 1) {
                    $where .= "blog.".$field . " LIKE '%" . $q . "%' ";
                } else {
                    $where .= "OR " . "blog.".$field . " LIKE '%" . $q . "%' ";
                }
                $iterasi++;
            }

            $where = '('.$where.')';
        } else {
            $where .= "(" . "blog.".$field . " LIKE '%" . $q . "%' )";
        }

        $this->join_avaiable();
        $this->db->where($where);
        $query = $this->db->get($this->table_name);

        return $query->num_rows();
    }

    public function get($q = null, $field = null, $limit = 0, $offset = 0, $select_field = [])
    {
        $iterasi = 1;
        $num = count($this->field_search);
        $where = NULL;
        $q = $this->scurity($q);
        $field = $this->scurity($field);

        if (empty($field)) {
            foreach ($this->field_search as $field) {
                if ($iterasi == 1) {
                    $where .= "blog.".$field . " LIKE '%" . $q . "%' ";
                } else {
                    $where .= "OR " . "blog.".$field . " LIKE '%" . $q . "%' ";
                }
                $iterasi++;
            }

            $where = '('.$where.')';
        } else {
            $where .= "(" . "blog.".$field . " LIKE '%" . $q . "%' )";
        }

        if (is_array($select_field) AND count($select_field)) {
            $this->db->select($select_field);
        }

        $this->join_avaiable();
        $this->db->where($where);
        $this->db->limit($limit, $offset);
        $this->db->order_by('blog.'.$this->primary_key, "DESC");
        $query = $this->db->get($this->table_name);

        return $query->result();
    }

    public function join_avaiable() {
        $this->db->join('blog_category', 'blog_category.category_id = blog.category', 'LEFT');

        return $this;
    }

}

/* End of file Model_blog.php */
/* Location: ./application/models/Model_blog.php */