I have my reports viewed in a page. I want to make function where the reports will be viewed/downloaded as an excel file when an export button is clicked. I've tried this: Reports in Codeigniter but I have no luck. Can someone help me on this? Thanks!
My Controller:
public function export_reports(){
$this->load->dbutil();
$this->load->helper('file');
$filter = $this->input->post('filter');
$year_date = $this->input->post('year_val');
$today = $this->input->post('today');
$month_start = $this->input->post('month_start');
$month_end = $this->input->post('month_end');
$last_month_start = $this->input->post('last_month_start');
$last_month_end = $this->input->post('last_month_end');
$this_year_start = $this->input->post('this_year_start');
if($filter == "this_month"){
$report = $this->getdata_model->get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start);
$new_report = $this->dbutil->xml_from_result($report);
write_file('billing_report_this_month.xml',$new_report);
}else{
$report = $this->getdata_model->default_report($month_start, $month_end);
$new_report = $this->dbutil->xml_from_result($report);
write_file('monthly_billing_report.xml',$new_report);
}
}
My Model:
public function default_report($month_start, $month_end){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}
public function get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start){
if($filter==""){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "this_month"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "last_month"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $last_month_start)
->where('billing_date <=', $last_month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "monthly" || $filter == "annual"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_year', $year_date)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "ytd"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $this_year_start)
->where('billing_date <=', $today)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}
}
I've discovered the solution. I just followed the instructions from http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework and it works.
Related
I am beginner of laravel. I am involving in a small project and facing some issue. I want to get the id from the function 'getVoucher' in order to display it in my function 'index'. But I have tried my code at the below, I can't get any id from it. The dd displayed 'null' and I don't know any other solution. Any comments are appreciated. Thanks in advance.
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
return $voucher;
}
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Display';
$btn = $btn. 'Edit';
$btn = $btn.' <i class="far fa-trash-alt btn-outline-danger"></i>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = $this->getVoucher($request);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
you have to use self:: not $this.
like this:
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
return $voucher;
}
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Display';
$btn = $btn. 'Edit';
$btn = $btn.' <i class="far fa-trash-alt btn-outline-danger"></i>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = self::getVoucher($request);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
or even better, you can do this instead:
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Display';
$btn = $btn. 'Edit';
$btn = $btn.' <i class="far fa-trash-alt btn-outline-danger"></i>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = Voucher::find($request->id);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
$this->index($voucher->id);`enter code here`
}
public function index(Request $request, $voucher)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Display';
$btn = $btn. 'Edit';
$btn = $btn.' <i class="far fa-trash-alt btn-outline-danger"></i>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
dd($voucher);
I create query dependent for multiple parameters. When controller function get for example 'price' and 'status' parameter I want to return results based on that two parameters.
When I use one parameter everything works well.
There is example of my controller.
$productList = Product::
where([
['lat', '>', $calculateDistanceDifference->getData()->latDifferenceBottom],
['lat', '<', $calculateDistanceDifference->getData()->latDifferenceTop],
['lng', '>', $calculateDistanceDifference->getData()->lngDifferenceBottom],
['lng', '<', $calculateDistanceDifference->getData()->lngDifferenceTop]
])
->when(request('price') !== "", function ($q) {
if(request('price') === "0-20"){
return $q->where([['price', '>', 0], ['price', '<=', 20]]);
}else if(request('price') === "21-50"){
return $q->where([['price', '>', 20], ['price', '<=', 50]]);
}else if(request('price') === "51-100"){
return $q->where([['price', '>', 50], ['price', '<=', 100]]);
}else if(request('price') === "100-200"){
return $q->where([['price', '>', 100], ['price', '<=', 200]]);
}else if(request('price') === "201+"){
return $q->where('price', '>', 200);
}
})
->when(request('status') !== "", function ($q) {
if(request('status') === "new"){
return $q->where('status', 0);
}else if(request('status') === "old"){
return $q->where('status', 1);
}
})
->when(request('active') !== "", function ($q) {
if(request('active') == true){
return $q->where('state', 0);
}else{
var_dump(request('active'));
return $q->where('state', 1);
}
})
->with('productPhotos')
->with('categories')
->with('users')
->get();
How can I avoid return statements and return result after all transformations?
I appreciate any help.
You can split and merge the query by if/else or any programming statement.
For example
$productList = Product::select('*');
if (request('price') !== ""){
if(request('price') === "0-20"){
$productList = $productList->where('price', '>', 0)->where('price', '<=', 20);
}
//any other conditions that you want to handle....
}
if (request('status') !== ""){
if(request('status') === "new){
$productList = $productList->where('status', '=', 0);
}
//any other conditions that you want to handle....
}
$productList = $productList
->with('productPhotos')
->with('categories')
->with('users')
->get();
Hope this help.
I am performing a search in table with multiple condition which can exist individually and in group. These are my codes for the process.
Controller Code
if(\Input::has('title','category_id','type','price_max','price_min','seller_type_id','division_id','district_id','upazila_id')) {
$data['products'] = Custom::getProducts(\Input::get('title'), \Input::get('category_id'), \Input::get('type'),\Input::get('price_max'),\Input::get('price_min'),\Input::get('seller_type_id'),\Input::get('division_id'),\Input::get('district_id'),\Input::get('upazila_id'));
} else {
$data['products'] = \DB::table('products')->where('is_active', '=', 1)->get();
}
return view('pages.posts.list')->with($data);
Custom::getProducts
public static function getProducts($title = NULL, $category_id = NULL, $type = 0, $price_max = NULL, $price_min = NULL, $seller_type_id = 0, $division_id = NULL, $district_id = NULL, $upazila_id = NULL) {
$products = DB::table('products')
->where(function($query) use ($title, $category_id, $type, $price_max, $price_min, $seller_type_id, $division_id, $district_id, $upazila_id) {
if ($title)
$query->where('title', 'like', $title);
if ($category_id)
$query->where('category_id', $category_id);
if ($type != 0)
$query->where('type', $type);
if ($price_max)
$query->where('price', '<=', $price_max);
if ($price_min)
$query->where('price', '>=', $price_min);
if ($seller_type_id != 0)
$query->where('seller_type_id', $seller_type_id);
if ($division_id)
$query->where('division_id', $division_id);
if ($district_id)
$query->where('district_id', $district_id);
if ($upazila_id)
$query->where('upazila_id', $upazila_id);
})
->where('is_active', '=', 1)->get();
return $products;
}
But it is not working as expected. What I am doing wrong?
When I search for a pages in my categories it displays the result of what pages are in that category.
For testing I have set the limit per page to 1 on my search_for
function.
Question/Problem: If there a only 2 pages that show up in result for some reason the pagination links display 5 links where it should only display 2 pagination links.
functions for model are on controller for testing.
I think the problem lies where the db->like and db->or_like in model. Why does it display 5 pagination links when should only display 2 if only 2 results found.
Controller
<?php
class Category_search extends Catalog_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Search Category";
$data['categories'] = array();
$results = $this->get_categories();
foreach ($results as $result) {
$data['categories'][] = array(
'category_id' => $result['category_id'],
'name' => $result['name']
);
}
$this->load->library('form_validation');
$this->form_validation->set_rules('page', 'Category');
$this->form_validation->set_rules('name', 'Name');
if ($this->form_validation->run() == FALSE) {
$this->load->view('theme/default/template/pages/search_category_view', $data);
} else {
redirect('pages/category_search/search_for' . '/' . $this->input->post('category'));
}
}
public function search_for($offset = NULL) {
$data['title'] = "Caregory Results";
$data['heading_title'] = "Search" .' - '. $this->get_category_name($this->uri->segment(4));
$data['pages'] = array();
$this->load->library('pagination');
$limit = 1;
$config['base_url'] = base_url('pages/category_search/search_for') .'/'. $this->uri->segment(4);
$config['total_rows'] = $this->get_total_pages();
$config['per_page'] = $limit;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 5;
$config['num_links'] = "16";
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$results = $this->get_pages_within($config['per_page'], $page);
$this->load->model('catalog/tool/model_tool_image');
foreach ($results as $result) {
$data['pages'][] = array(
'category_id' => $result['category_id'],
'parent_id' => $result['parent_id'],
'name' => $result['name'],
'description' => $result['description'],
'image' => $this->model_tool_image->resize($result['image'], 280, 200)
);
}
$data['pagination'] = $this->pagination->create_links();
$this->load->view('theme/default/template/pages/search_category_search_for_view', $data);
}
// Todo move model functions to new model file when complete
public function get_categories() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category c', 'LEFT');
$this->db->join($this->db->dbprefix . 'category_description cd', 'cd.category_id = c.category_id', 'LEFT');
$query = $this->db->get();
return $query->result_array();
}
public function get_pages_within($limit, $offset) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'page_to_category p2c', 'LEFT');
$this->db->join($this->db->dbprefix . 'page p', 'p.page_id = p2c.page_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
$this->db->like('p2c.parent_id', (int)$this->uri->segment(4));
$this->db->or_like('p2c.category_id', (int)$this->uri->segment(4));
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result_array();
}
public function get_total_pages() {
return $this->db->count_all($this->db->dbprefix . 'page');
}
public function get_category_name($category_id = 0) {
$this->db->where('category_id', (int)$category_id);
$query = $this->db->get($this->db->dbprefix . 'category_description');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->name;
} else {
return FALSE;
}
}
}
I suppose 5 is all the table rows with no where/like filter and it probably come from this function that count everything:
public function get_total_pages() {
return $this->db->count_all($this->db->dbprefix . 'page');
}
Here you should add the same where clause.
Simple fix thanks to #KyleK suggestion I copied the code from the get_pages_within function and then returned return $query->num_rows();
All working
public function get_total_pages() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'page_to_category p2c', 'LEFT');
$this->db->join($this->db->dbprefix . 'page p', 'p.page_id = p2c.page_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
$this->db->like('p2c.parent_id', (int)$this->uri->segment(4));
$this->db->or_like('p2c.category_id', (int)$this->uri->segment(4));
$query = $this->db->get();
return $query->num_rows();
}
I am sitting in my chair for 2 hours and I cannot find what is wrong with this. I'm new to codeigniter things and I only got this error for the first time. Can you guys help me? Any help would be appreciated. Thanks in advance so much ...
****A PHP Error was encountered
Severity: Notice
Message: Undefined property: Facebook_model::$facebook
Filename: models/facebook_model.php
Line Number: 15
Fatal error: *Call to a member function getUser() on a non-object in C:\xampp\htdocs\hpbge\hpbge_files\system\hpbgestrong text_web_app\models\facebook_model.php on line 15*****
<?php
class Facebook_model extends Model {
public function get_user() {
$query = $this->facebook->getUser();
if ($query) {
$data['is_true'] = TRUE;
$data['facebook_uid'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_access_token() {
$query = $this->facebook->getAccessToken();
if ($query) {
$data['is_true'] = TRUE;
$data['access_token'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_api_secret() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['api_secret'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_app_id() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['app_id'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_logout_url() {
$query = $this->facebook->getLogoutUrl(array('next' => base_url()));
if ($query) {
$data['is_true'] = TRUE;
$data['logout_url'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_signed_request() {
$query = $this->facebook->getSignedRequest();
if ($query) {
$data['is_true'] = TRUE;
$data['signed_request'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_access_token($access_token) {
$query = $this->facebook->setAccessToken($access_token);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_api_secret($app_secret) {
$query = $this->facebook->setApiSecret($app_secret);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_app_id($app_id) {
$query = $this->facebook->setAppId($app_id);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
//function is formatted for the following
//https://graph.facebook.com/ID/CONNECTION_TYPE?access_token=123456
function get_facebook_object($object, $facebook_uid, $access_token) {
$fb_connect = curl_init();
curl_setopt($fb_connect, CURLOPT_URL, 'https://graph.facebook.com/'.$facebook_uid.'/'.$object.'?access_token='.$access_token);
curl_setopt($fb_connect, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($fb_connect);
curl_close($fb_connect);
$result = json_decode($output);
if (isset($result->error)) {
$data['is_true'] = FALSE;
$data['message'] = $result->error->message;
$data['type'] = $result->error->type;
$data['code'] = $result->error->code;
return $data;
} else {
$data['is_true'] = TRUE;
$data['data'] = $result->data;
return $data;
}
}
}
It seems that you have not loaded the facebook library put this line in your construct of model file
$this->load->library('facebook', array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET'
));
and make sure that you have put the facebook.php and base_facebook.php in libraries folder under application.