<?php

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

class Adsmodel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    protected $table = 'ads';

    // protected $user_details = ['id userId', 'name', 'email', 'wallet', 'image', 'password'];

    var $order = array('id', 'title', 'occurance', 'thumbnail', 'target', 'watched');

    private function _get_data_query($where)
    {
        $this->db->select("$this->table.*");
        $this->db->from($this->table);

        if (isset($_POST['search']['value'])) {
            $searchTerm = $_POST['search']['value'];
            $this->db->group_start();
            $this->db->like('' . $this->table . '.title', $searchTerm);
            $this->db->or_like('' . $this->table . '.target', $searchTerm);
            $this->db->or_like('' . $this->table . '.watched', $searchTerm);
            $this->db->group_end();
        }

        
        // print_r($_POST);
        $this->db->where($where);
        if (isset($_POST['order'])) {
            $this->db->order_by($this->order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else {

            $this->db->order_by('id', 'desc');
        }
    }

    public function get_data_table($where)
    {
        // print_r($where);exit;
        $this->_get_data_query($where);
        if ($_POST['length'] != -1) {
            $this->db->limit($_POST['length'], $_POST['start']);
        }

        $query = $this->db->get();
        // $b = $query->result_array();
        // print_r($this->db->last_query());
        // exit;
        return $query->result_array();
    }

    public function count_filtered_data($where)
    {
        $this->_get_data_query($where);
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all_data()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }



    

    
  
}
