1 thought on “Models in CodeIgniter PART-1

  1. db->insert($table, $data);
    if($flag)
    {
    return $this->db->insert_id();
    }
    else
    {
    return false;
    }
    }

    function get_single($table, $filter)
    {
    $data = $this->db->get_where($table, $filter);
    if($data->num_rows() > 0)
    {
    $data = $data->first_row(“array”);
    return $data;
    }
    else
    {
    return false;
    }
    }

    function get_by_condition($table, $filter)
    {
    $data = $this->db->get_where($table, $filter);
    if($data->num_rows() > 0)
    {
    $data = $data->result_array();

    return $data;
    }
    else
    {
    return false;
    }
    }

    function get_all($table, $order_by=”id”, $order=”ASC”)
    {
    $this->db->order_by($order_by, $order);
    $data = $this->db->get($table);
    if($data->num_rows() > 0)
    {
    $data = $data->result_array();
    return $data;
    }
    else
    {
    return false;
    }
    }

    function update_data($table, $data, $filter)
    {
    $this->db->where($filter);
    $flag = $this->db->update($table, $data);
    if($flag)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    function delete_data($table, $filter)
    {
    $this->db->where($filter);
    $flag = $this->db->delete($table);
    return $flag;
    }

    function randomstring($length = 6)
    {
    $characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
    $charactersLength = strlen($characters);
    $randomString = ”;
    for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, $charactersLength – 1)];
    }
    return $randomString;
    }

    function delete_multiple($table, $records = "")
    {
    $flag = false;
    if($records != "")
    {
    if(is_array($records))
    {
    for($i = 0;$i $records[$i]);
    $flag = $this->delete_data($table, $filter);
    if($flag)
    {
    if($table == ‘users’)
    {
    $extra_userata = array(‘userid’ => $records[$i]);
    $this->delete_data(“personal_information”, $filter);
    $this->delete_data(“employ_information”, $filter);
    $this->delete_data(“applications”, $filter);
    $this->delete_data(“education_details”, $filter);
    }

    $total = $i + 1;
    $flag = $total;
    }
    else
    {
    $flag = false; break;
    }
    }
    }
    else
    {
    $filter = array(“id” => $records);
    $flag = $this->delete_data($table, $filter);
    }
    }
    return $flag;
    }

    function send_mail2($subject = “testing”, $content = “testing”, $email = “”)
    {
    $flag = false;
    if($email != “”)
    {
    $config[‘charset’] = ‘iso-8859-1’;
    $config[‘wordwrap’] = TRUE;
    $config[‘mailtype’] = “html”;

    $this->email->initialize($config);
    $this->email->to($email);
    $this->email->from(‘[email protected]’);

    $this->email->subject($subject);
    $this->email->message($content);
    $flag = $this->email->send();
    }
    return $flag;
    }

    function fileupload($path = “upload”, $types = “”, $input = “”)
    {
    $config[‘upload_path’] = ‘./’.$path.’/’;
    $config[‘allowed_types’] = $types;
    $config[‘max_size’] = 2048;
    $config[‘file_name’] = date(‘YmdHis’);
    $this->load->library(‘upload’, $config);

    if($this->upload->do_upload($input))
    {
    $uploaddata = $this->upload->data();
    $this->load->library(‘image_lib’);
    $img_thumb = array(
    ‘image_library’ => ‘gd2’,
    ‘source_image’ => $uploaddata [‘full_path’],
    ‘new_image’ => $uploaddata [‘file_path’].”thumbs”,
    ‘maintain_ratio’ => TRUE,
    ‘create_thumb’ => TRUE,
    ‘thumb_marker’ => “”,
    ‘width’ => 60,
    ‘height’ => 60
    );

    $this->image_lib->initialize($img_thumb);
    if(!$this->image_lib->resize())
    {
    $error = $this->image_lib->display_errors();
    print_r($error);
    }
    $this->image_lib->clear();
    $view_thumb = array(
    ‘image_library’ => ‘gd2’,
    ‘source_image’ => $uploaddata [‘full_path’],
    ‘new_image’ => $uploaddata [‘file_path’].”view_img”,
    ‘maintain_ratio’ => TRUE,
    ‘create_thumb’ => TRUE,
    ‘thumb_marker’ => “”,
    ‘width’ => 200,
    ‘height’ => 200
    );
    $this->image_lib->initialize($view_thumb);
    if(!$this->image_lib->resize())
    {
    $error = $this->image_lib->display_errors();
    print_r($error);
    }
    else
    {
    return $uploaddata[‘orig_name’];
    }
    }
    else
    {
    $error = $this->upload->display_errors();
    print_r($error);
    }
    return false;
    }

    function create_log($user = ”, $log = ”)
    {
    $data = array(‘userid’ => $user, ‘log’ => $log);
    $table = ‘logs’;
    $flag = $this->insert_data($table, $data);
    return $flag;
    }

    function get_export_users($job = “”)
    {
    $this->db->select(‘US.id as uid, US.created as ucreated, US.*, AP.*, JB.*’, ‘DT.*’, ‘SQ.*’);
    $this->db->from(‘users as US’);
    $this->db->join(‘applications as AP’, ‘AP.userid = US.id’);
    $this->db->join(‘jobs as JB’, ‘JB.id = AP.jobid’);

    if($job != “” && $job != NULL)
    {
    $this->db->where(‘AP.jobid’, $job);
    }
    $data = $this->db->get();
    if($data->num_rows() > 0)
    {
    return $data->result_array();
    }
    else
    {
    return false;
    }
    }

    function send_mail($to, $from, $name, $subject, $msg = “”)
    {
    $config[‘protocol’] = ‘sendmail’;
    $config[‘mailpath’] = ‘/usr/sbin/sendmail’;
    $config[‘charset’] = ‘iso-8859-1’;
    $config[‘wordwrap’] = TRUE;
    $config[‘mailtype’] = ‘html’;
    $this->email->initialize($config);

    $this->email->to($from);
    $this->email->from($to, $name);
    $this->email->subject($subject);

    $msg_header = “”;
    $msg_footer = “”;

    $msg = $msg_header.$msg;
    $msg .= $msg_footer;

    $this->email->message($msg);

    $flag = $this->email->send();

    return $flag;
    }

    function count_new_users()
    {
    $this->db->select(“US.id AS uid, US.*, JE.*, JB.*”);
    $this->db->from(“users AS US”);
    $this->db->join(“employ_information AS JE”, “US.id = JE.userid”);
    $this->db->join(“applications AS AP”, “AP.userid = US.id”);
    $this->db->join(“jobs AS JB”, “AP.jobid= JB.id”);

    $this->db->where(“US.new_user”, 1);
    $data = $this->db->get();
    if($data->num_rows() > 0 )
    {
    return $data->result_array();
    }
    else
    {
    return false;
    }
    }

    function get_templete($tempate_for = “”)
    {
    if($tempate_for != “”)
    {
    $filter = array(“mail_for” => $tempate_for);
    $table = “mail_templates”;
    $data = $this->get_single($table, $filter);

    if($data != false)
    {
    $template = $data[‘template’];
    return $template;
    }
    else
    {
    return false;
    }
    }
    else
    {
    return false;
    }
    }

    public function notification($slug = “”)
    {
    $msg = “”;
    if($slug != “”)
    {
    $filter = array(“msg_slug” => $slug);
    $table = “notifications”;
    $data = $this->get_single($table, $filter);
    if($data != false)
    {
    $msg = $data[‘message’];
    }

    }
    return $msg;
    }

    function age($date)
    {
    $today = date(‘d-m-Y’);
    $date1 = date_create($today);
    $date2 = date_create($date);
    $diff = date_diff($date1,$date2);
    return $diff->y;
    }

    }
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *