model

 <?php




defined('BASEPATH') or exit('No direct script access allowed');







class my_model extends CI_Model



{



    public function insert_post($data)

    {

        $this->db->insert('post', $data);

        if ($this->db->affected_rows() > 0) {

            return $this->db->insert_id();

        } else {

            return false;

        }

    }



    public function get_category()

    {

        $query = $this->db->get('characters');

        return $query->result();

    }

    public function get_tag1()

    {

        $query = $this->db->get('tags');

        return $query->result();

    }

    public function get_tag($slug)

    {

        $this->db->where('tag_slug', $slug);

        $query = $this->db->get('tags');

        return $query->result();

    }

    public function get_character($slug)

    {

        $this->db->where('characters_slug', $slug);

        $query = $this->db->get('characters');

        return $query->result();

    }

    public function get_author()

    {

        // $this->db->get

        $query = $this->db->get('artists');

        return $query->result();

    }

    public function get_posts_by_character($character_id)

    {

        // Use the 'like' method to search for posts containing the tag inside the 'tags' JSON field

        $this->db->like('characters_id', '"' . $character_id . '"'); // Searching for the tag in JSON format

        $query = $this->db->get('post'); // Execute the query to get data from the 'post' table

        // Return the result as an array of objects

        if ($query->num_rows() > 0) {

            return $query->result(); // Return the posts that have the tag

        } else {

            return []; // Return an empty array if no posts are found

        }

    }

    public function get_posts_by_tag($tag_id)

    {

        // Use the 'like' method to search for posts containing the tag inside the 'tags' JSON field

        $this->db->like('tags', '"' . $tag_id . '"'); // Searching for the tag in JSON format

        $query = $this->db->get('post'); // Execute the query to get data from the 'post' table

        // Return the result as an array of objects

        if ($query->num_rows() > 0) {

            return $query->result(); // Return the posts that have the tag

        } else {

            return []; // Return an empty array if no posts are found

        }

    }

    public function get_post($page = 1, $limit = 20)

    {

        $offset = ($page - 1) * $limit;

        // Select fields from the posts table and related tables
        $this->db->select('post.*');
        $this->db->from('post');
        // Limit and offset for pagination
        $this->db->limit($limit, $offset);
        $this->db->order_by('post.created_at', 'DESC');
        // Fetch the result
        $query = $this->db->get();
        // echo $this->db->last_query();die;
        return $query->result();
    }
    public function get_single_post($slug)
    {
        $this->db->select('post.*');
        $this->db->from('post');
        $this->db->where('post.slug', $slug);
        $query = $this->db->get();
        // echo $this->db->last_query();die;
        return $query->result();
    }
    // Fetch Recent posts
    public function get_recent_posts($limit = 13)
    {
        $this->db->order_by('created_at', 'desc');
        $this->db->limit($limit);
        $query = $this->db->get('post');
        return $query->result();
    }

    // Fetch Similar posts
    public function getSimilarCategoryPosts($category_id, $post_id, $limit = 3)
    {
        // Fetch posts where category_id is 1 and exclude post_id 2
        $this->db->where('category_id', $category_id);
        $this->db->where('post_id !=', $post_id); // Exclude post with post_id 2
        $this->db->limit($limit);
        $query = $this->db->get('post');
        return $query->result();
    }
    public function get_recom_post($limit = 5)
    {
        // Select fields from both tables as necessary
        $this->db->select('engagement.*, post.*,category.*,author.*');
        $this->db->from('engagement');
        // Join the 'post' table on the modified 'page_url' which matches 'post_slug'
        $this->db->join('post', 'REPLACE(engagement.url, "https://banglapanugolpo.online/Post/", "") = post.post_slug');
        $this->db->join('author', 'post.author_id = author.author_id', 'left');
        $this->db->join('category', 'post.category_id = category.category_id', 'left');
        // Order by 'visit_time' in descending order
        $this->db->order_by('engagement.status', 'DESC');
        $this->db->limit($limit);
        $query = $this->db->get();
        // echo $this->db->last_query();die;
        return $query->result();
    }
    public function get_first_post()
    {
        $query = $this->db->get('dpost', 1); // Fetches the first row
        return $query->row_array(); // Returns the row as an associative array
    }
    public function delete_post($id)
    {
        $this->db->where('post_id', $id);
        return $this->db->delete('dpost');
    }

}


Comments

Popular posts from this blog

If the current time is between 10 AM and 7 PM, the button is enabled; otherwise, it is disabled JAVASCRIPT code

Unleashing the PHP Developer in You: How to Crack Any Interview with Confidence