On my controller for development I have added error_reporting(0); just below the opening php tag on my controller. So will not show some errors.
When go to live mode and remove or comment out the added error_reporting(0); I get a couple of errors
A PHP Error was encountered Severity: Notice Message: Undefined
variable: other_sub Filename: common/Filemanager.php
A PHP Error was encountered Severity: Notice Message: Undefined
variable: histSub Filename: common/Filemanager.php
Question: I have those variables defined not sure why errors showing?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//error_reporting(0);
class Filemanager extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('pagination');
$this->lang->load('admin/common/filemanager', 'english');
$this->load->model('admin/tool/model_tool_image');
}
public function index() {
$directory = FCPATH . 'image/catalog/';
$element = $this->input->get('element'); // Element
$input = $this->input->get('input'); // Target
$sub_get = $this->input->get("sub"); // Sub Folders
$end_url = '?';
$end_url .= 'input='.$input;
$end_url .= '&element='.$element;
$endHist = $end_url;
$uris = $this->uri->segment_array();
$sub_folder = "";
if ($sub_get) {
$sub_folder = $sub_get;
}
if ($sub_folder) {
$end_url .= '&sub=' .$sub_folder;
if (sizeof($uris) >3 ) {
for ($i=3; $i < sizeof($uris); $i++) {
$directory .= $uris[$i].'/';
$other_sub .= $uris[$i].'/';
}
$directory .= $sub_folder.'/';
$other_sub .= $sub_folder.'/';
} else {
$directory .= $sub_folder.'/';
$other_sub = $sub_folder.'/';
}
}
$data['images'] = array();
// Get directories
$directories = glob($directory . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
// Get files
$files = glob($directory.'*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
if (!$files) {
$files = array();
}
// Merge directories and files
$images = array_merge($directories, $files);
// Get total number of files and directories
$image_total = count($images);
$per_page = 8;
$segment = $this->input->get('per_page');
$segment += $per_page;
foreach ($images as $key => $image) {
if ($key < $segment && $key >= $segment-$per_page) {
$name = str_split(basename($image), 18);
if (is_dir($image)) {
$data['images'][] = array(
'thumb' => '',
'name' => implode(' ', $name),
'type' => 'directory',
'path' => utf8_substr($image, utf8_strlen(FCPATH .'image/')),
'href' => site_url('admin/filemanager') .'/'. utf8_substr($image, utf8_strlen(FCPATH . 'image/catalog/')),
);
} elseif (is_file($image)) {
$subDirect = 'catalog/'.$other_sub;
$data['images'][] = array(
'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(DIR_IMAGE)), 100, 100),
'name' => implode(' ', $name),
'type' => 'image',
'path' => $subDirect.utf8_substr($image, utf8_strlen($directory)),
'href' => base_url() . 'image/' . utf8_substr($image, utf8_strlen(FCPATH . 'image/catalog/'))
);
}
}
}
$data['title'] = "Image Manager";
$data['heading_title'] = "Image Manager";
if (isset($sub_folder)) {
$data['directory'] = $this->uri->segment(3) .'/'. $this->uri->segment(4);
} else {
$data['directory'] = '';
}
if ($element) {
$data['element'] = $element;
} else {
$data['element'] = "";
}
if ($input) {
$data['target'] = $input;
} else {
$data['target'] = "";
}
$endRep = $end_url == '?' ? '' : rtrim($end_url,'&');
// Sets go to previous parent folder.
if ($other_sub) {
$arraySub = explode('/', rtrim($other_sub,'/'));
unset($arraySub[sizeof($arraySub) -1]);
$histSub = "";
foreach ($arraySub as $one) {
$histSub .= $one.'/';
}
$endHist .= "&sub=".$arraySub[sizeof($arraySub) - 1];
}
// Parent folder link
$data['parent'] = site_url('admin/filemanager') .'/'. $histSub . $endHist;
// Refesh current Page
$data['refresh'] = current_url().$endRep;
$config['base_url'] = base_url('admin/filemanager/') .'/'. $other_sub.$endRep;
$config['end_url'] = $end_url == '?' ? '' : rtrim($end_url,'&');
$config['total_rows'] = $image_total;
$config['per_page'] = $per_page;
$config['page_query_string'] = TRUE;
$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);
$data['pagination'] = $this->pagination->create_links();
return $this->load->view('template/common/filemanager_view', $data);
}
}
Problem now solved I had to change a few things and make the input->get setup a lot more simple now works fine.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Filemanager extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('pagination');
$this->lang->load('admin/common/filemanager', 'english');
$this->load->model('admin/tool/model_tool_image');
}
public function index() {
$input_get_directory = $this->input->get('directory');
$input_get_page = $this->input->get('page');
$input_get_filter = $this->input->get('filter_name');
$input_get_target = $this->input->get('target');
$input_get_thumb = $this->input->get('thumb');
if (isset($input_get_filter)) {
$filter_name = $input_get_filter .'/';
} else {
$filter_name = null;
}
// Make sure we have the correct directory
if (isset($input_get_directory)) {
$directory = FCPATH . 'image/catalog/' . $input_get_directory;
} else {
// Do not add extra tralier slash at end /
$directory = FCPATH . 'image/catalog';
}
if (isset($input_get_page)) {
$page = $input_get_page;
} else {
$page = 1;
}
$data['images'] = array();
// Get directories
$directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
if (!$files) {
$files = array();
}
// Merge directories and files
$images = array_merge($directories, $files);
// Get total number of files and directories
$image_total = count($images);
// Split the array based on current page number and max number of items per page of 10
$images = array_splice($images, ($page - 1) * 16, 16);
foreach ($images as $image) {
$name = str_split(basename($image), 14);
if (is_dir($image)) {
$url = '';
if (isset($input_get_target)) {
$url .= '&target=' . $input_get_target;
}
if (isset($input_get_thumb)) {
$url .= '&thumb=' . $input_get_thumb;
}
$data['images'][] = array(
'thumb' => '',
'name' => implode(' ', $name),
'type' => 'directory',
'path' => utf8_substr($image, utf8_strlen(FCPATH . 'image/')),
'href' => site_url('admin/common/filemanager' . '?&token=' . $this->session->userdata('token') . '?&directory=' . utf8_substr($image, utf8_strlen(FCPATH . 'image/' . 'catalog/')) . $url)
);
} elseif (is_file($image)) {
$data['images'][] = array(
'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(FCPATH . 'image/')), 100, 100),
'name' => implode(' ', $name),
'type' => 'image',
'path' => utf8_substr($image, utf8_strlen(FCPATH . 'image/')),
'href' => base_url() . 'image/' . utf8_substr($image, utf8_strlen(FCPATH . 'image/'))
);
}
}
$data['heading_title'] = "Image Manager";
$data['text_no_results'] = "No Results";
$data['text_confirm'] = "Are You Sure";
$data['entry_search'] = "Search..";
$data['entry_folder'] = "New Folder";
$data['button_parent'] = "Parent";
$data['button_refresh'] = "Refresh";
$data['button_upload'] = "Upload";
$data['button_folder'] = "New Folder";
$data['button_delete'] = "Delete";
$data['button_search'] = "Search";
// Session token for ajax
$data['token'] = $this->session->userdata('token');
if (isset($input_get_directory)) {
$data['directory'] = $input_get_directory;
} else {
$data['directory'] = '';
}
// Return the filter name
if (isset($input_get_filter)) {
$data['filter_name'] = $input_get_filter;
} else {
$data['filter_name'] = '';
}
// Return the target ID for the file manager to set the value
if (isset($input_get_target)) {
$data['target'] = $input_get_target;
} else {
$data['target'] = '';
}
// Return the thumbnail for the file manager to show a thumbnail
if (isset($input_get_thumb)) {
$data['thumb'] = $input_get_thumb;
} else {
$data['thumb'] = '';
}
// Parent
$url = '';
if (isset($input_get_directory)) {
$pos = strrpos($input_get_directory, '/');
if ($pos) {
$url .= '?&directory=' . substr($input_get_directory, 0, $pos);
}
}
$data['parent'] = site_url('admin/common/filemanager' .'?&token='. $this->session->userdata('token') . $url);
// Refresh
$url = '';
if (isset($input_get_directory)) {
$url .= '?&directory=' . $input_get_directory;
}
$data['refresh'] = site_url('admin/common/filemanager' .'?&token='. $this->session->userdata('token') . $url);
$this->load->view('template/common/filemanager_view', $data);
}
}
Related
On my controller I am trying to be able to display a message where it counts from 1 "Your On Page 1" etc
How ever it does not start of at 1 it starts at 0 "Your On Page 0"
$message = '';
$message .= '<p>' . sprintf('Your on page %d', ($config['total_rows']) ? (($start - 1) * $config['per_page']) + 1 : 0) . '</p>';
$message .= '<p>Total ( Pages ' . ceil($config['total_rows'] / $config['per_page']) . ' )</p>';
$data['total'] = $message;
Question: How can I make sure that it can count from 1 instead of 0 in message with out effecting the main pagination on controller
Controller
<?php
class Forum extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('catalog/forum/forum_model');
$this->load->library('pagination');
}
public function category() {
$this->document->set_title('Forums');
$category_id = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
if ($this->input->get('order')) {
$order = $this->input->get('order');
} else {
$order = 'asc';
}
if ($this->input->get('sort')) {
$sort = $this->input->get('sort');
} else {
$sort = 'message';
}
$url = '';
if ($this->input->get('sort')) {
$url .= '?sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/';
$config["total_rows"] = $this->forum_model->total_category($category_id);
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['reuse_query_string'] = TRUE;
$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);
$data['pagination'] = $this->pagination->create_links();
$start = ($this->input->get('per_page')) ? $this->input->get('per_page') : '';
$filter_data = array(
'limit' => $config["per_page"],
'start' => $start,
'category_id' => $category_id,
'sort' => ($this->input->get('sort')) ? $this->input->get('sort') : 'message',
'order' => ($this->input->get('order')) ? $this->input->get('order') : 'asc'
);
$data['threads'] = array();
$results = $this->forum_model->get_threads_for_forum($filter_data);
foreach ($results as $result) {
$data['threads'][] = array(
'thread_id' => $result['thread_id'],
'user_id' => $result['user_id'],
'username' => $result['username'],
'subject' => $result['subject'],
'link' => site_url('thread') . '-' . $result['thread_id'],
'total' => $this->forum_model->total_threads($result['thread_id']),
'date_created' => date('d-m-Y', strtotime($result['date_created'])),
'user_link' => site_url('user') . '-' . $result['user_id']
);
}
$data['back'] = site_url('forum');
$data['newthread'] = site_url('newthread') . '?fid=' . $category_id;
$url = '';
if ($order == 'asc') {
$url .= '&order=desc';
} else {
$url .= '&order=asc';
}
if ($this->input->get('per_page')) {
$url .= '&per_page=' . $this->input->get('per_page');
}
$data['thread_message'] = site_url('forum/category') .'/'. $category_id .'/'. '?sort=message' . $url;
$data['date_created'] = site_url('forum/category') .'/'. $category_id .'/'. '?sort=date_created' . $url;
$data['sort'] = $sort;
$data['order'] = $order;
$total_threads = $this->forum_model->get_total_threads_for_category($category_id);
$message = '';
$message .= '<p>' . sprintf('Your on page %d', ($config['total_rows']) ? (($start - 1) * $config['per_page']) + 1 : 0) . '</p>';
$message .= '<p>Total ( Pages ' . ceil($config['total_rows'] / $config['per_page']) . ' )</p>';
$data['total'] = $message;
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['menu'] = Modules::run('catalog/common/menu/index');
$this->load->view('default/template/forum/forum_thread_view', $data);
}
}
It ended up being a simple fix had brain fart should of picked it up straight away.
I changed
(($start - 1) * $config['per_page']) + 1 : 0)
To
(($start - 0) * $config['per_page']) + 1 : 0)
I am making my table head links sortable and orderable how ever my per_page does not show up first
http://localhost/project-1/forum/category/1/&order=asc?per_page=1
It should show like
http://localhost/codeigniter/forum/category/1/?per_page=1&order=asc
Question when I click on the pagination links if the have been order or sorted how can I make sure that when I click on a pagination link even though has been order/sorted will still allways show ?per_page first in url.
$url = '';
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
Controller
<?php
class Forum extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('catalog/forum/forum_model');
$this->load->library('pagination');
}
public function category() {
$this->document->set_title('Forums');
$category_id = $this->uri->segment(3);
if ($this->input->get('sort')) {
$sort = $this->input->get('sort');
} else {
$sort = 'message';
}
if ($this->input->get('order')) {
$order = $this->input->get('order');
} else {
$order = 'asc';
}
$url = '';
if ($this->input->get('per_page')) {
$url .= '?per_page=' . $this->input->get('per_page');
}
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
$config["total_rows"] = $this->forum_model->total_category($category_id);
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$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);
$start = ($this->input->get('per_page')) ? $this->input->get('per_page') : '';
$filter_data = array(
'limit' => $config["per_page"],
'start' => $start,
'category_id' => $category_id,
'sort' => $sort,
'order' => $this->input->get('order')
);
$data['threads'] = array();
$results = $this->forum_model->get_threads_for_forum($filter_data);
foreach ($results as $result) {
$data['threads'][] = array(
'thread_id' => $result['thread_id'],
'user_id' => $result['user_id'],
'username' => $result['username'],
'subject' => $result['subject'],
'link' => site_url('thread') . '-' . $result['thread_id'],
'total' => $this->forum_model->total_threads($result['thread_id']),
'date_created' => date('d-m-Y', strtotime($result['date_created'])),
'user_link' => site_url('user') . '-' . $result['user_id']
);
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['menu'] = Modules::run('catalog/common/menu/index');
$data['pagination'] = $this->pagination->create_links();
$data['back'] = site_url('forum');
$data['thread'] = site_url('newthread') . '?fid=' . $category_id;
$url = '';
if ($order == 'asc') {
$url .= '?order=desc';
} else {
$url .= '?order=asc';
}
$data['message'] = site_url('forum/category') .'/'. $category_id .'/'. $url;
$data['sort'] = $sort;
$data['order'] = '';
$this->load->view('default/template/forum/forum_thread_view', $data);
}
}
Set your default category id if url does not contain:
if ($this->uri->segment(3) == 'null') {
$category_id = 1; //set default for null
} else {
$category_id = $this->uri->segment(3);
}
Edit your config['base_url'] and add set $config['suffix'] if $_GET is not
empty.
New Pagination config:
$config["base_url"] = base_url('forum/category'); //no need custom category id
if (count($_GET) > 0)
$config['suffix']='?'.http_build_query($_GET,'',"&");//to encode requested data
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$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>";
I don't see any:
$config[‘reuse_query_string’] = TRUE;
This will allow to use mixed type of urls. This was not possible before CI 3.0.
I have a working file manager with codeIgniter 3.0.4 pagination library.
I am trying to make sure it displays the directory's first and then files in the pagination.
Currently the codeigniter pagination sorts the directories and files all by name.
Question with codeigniter pagination how can I make sure it all ways displays the directories first. Then sorts out the files.
<?php
class Filemanager extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
$this->load->helper('text');
$this->load->library('pagination');
}
public function index($results = NULL) {
$data['title'] = 'File Manager';
$this_input_get_directory = $this->input->get('directory');
if (isset($this_input_get_directory)) {
$directory = scandir(FCPATH . 'images/catalog/' . $this_input_get_directory . '/', 1);
} else {
$directory = scandir(FCPATH . 'images/catalog/', 1);
}
$files = array_diff($directory, array('.', '..'));
$files_limit = 3;
$input_get_per_page = $this->uri->segment(2);
$input_get_per_page += $files_limit;
$config['base_url'] = base_url('filemanager');
$config['total_rows'] = count($files);
$config['per_page'] = $files_limit;
$config['uri_segment'] = 2;
$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);
$data['images'] = array();
foreach ($files as $file => $value) {
$url = '';
if (isset($this_input_get_directory)) {
$url .= $this_input_get_directory . '/';
} else {
$url .= '';
}
if ($file < $input_get_per_page && $file >= $input_get_per_page - $files_limit) {
if (is_dir(FCPATH . 'images/catalog/' . $value)) {
$data['images'][] = array(
'thumb' => '',
'type' => 'directory',
'href' => site_url('filemanager') . '?directory='. $url . $value,
'name' => $value
);
} elseif (is_file(FCPATH . 'images/catalog/' . $url . $value)) {
$data['images'][] = array(
'thumb' => $this->resize($value, 100, 100),
'type' => 'image',
'name' => $value
);
}
}
}
$this->load->view('template/common/filemanager_view', $data);
}
}
Solved I have found some code from some where else that made it work for me. I have also used now array_reverse(); all working so far.
<?php
class Filemanager extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
$this->load->helper('text');
$this->load->library('pagination');
$this->load->helper('custom');
define('DIR_IMAGE', FCPATH . 'images/');
}
public function index() {
$data['title'] = 'File Manager';
if ($this->uri->segment(3)) {
$this_input_get_directory = $this->uri->segment(2) . '/' . $this->uri->segment(3);
} else {
$this_input_get_directory = $this->uri->segment(2) . '/' . $this->uri->segment(3);
}
if (isset($this_input_get_directory)) {
$directory = rtrim(DIR_IMAGE . 'catalog/' . str_replace(array('../', '..\\', '..'), '', $this_input_get_directory), '/');
} else {
$directory = DIR_IMAGE . 'catalog';
}
// Get directories
$directories = glob($directory . '/' . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
// Get files
$files = array_reverse(glob($directory . '/' . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE));
if (!$files) {
$files = array();
}
// Merge directories and files
$images = array_merge($directories, $files);
$files_limit = 6;
$input_get_per_page = $this->input->get('per_page');
$input_get_per_page += $files_limit;
$config['base_url'] = base_url('filemanager');
$config['total_rows'] = count($images);
$config['per_page'] = $files_limit;
$config['page_query_string'] = TRUE;
$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);
$data['images'] = array();
foreach ($images as $file => $value) {
$filename = str_replace(FCPATH . 'images/', '', $value);
$display_name = basename($filename);
if ($file < $input_get_per_page && $file >= $input_get_per_page - $files_limit) {
if (is_dir(DIR_IMAGE . $filename)) {
$data['images'][] = array(
'thumb' => '',
'type' => 'directory',
'path' => $value,
'name' => $display_name,
'href' => site_url('filemanager/' . $display_name)
);
} else if (is_file(DIR_IMAGE . $filename)) {
$data['images'][] = array(
'thumb' => $this->resize($filename),
'type' => 'image',
'path' => $value,
'name' => $display_name
);
}
}
}
$url = '';
if (isset($this_input_get_directory)) {
$pos = strrpos($this_input_get_directory, '/');
if ($pos) {
$url .= '?directory=' . urlencode(substr($this_input_get_directory, 0, $pos));
}
}
$data['parent'] = base_url('filemanager' . $url);
$url = '';
if (isset($this_input_get_directory)) {
$url .= '?directory=' . $this_input_get_directory;
}
$data['refresh'] = base_url('filemanager' . $url);
$this->load->view('template/common/filemanager_view', $data);
}
}
On my multiple upload library I have a function which is called upload data.
And another function called upload.
For some reason when I select multiple images and is success full when I use on my controller
$data = $this->multiple_upload->upload_data();
echo $data['file_name'];
It will only get the name of the last file selected it does not return all file names selected. It should display all file names selected.
Question: How on my library function upload_data() can I make sure can return data correctly rather than just the last one. the upload_data function just seems to only return the last file information.
Library
<?php
class Multiple_upload {
public function __construct($config = array()) {
$this->CI =& get_instance();
$this->files = $this->clean($_FILES);
empty($config) OR $this->set_config($config);
}
public function set_config($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
return $this;
}
public function upload($field = 'userfile') {
if (empty($this->upload_path)) {
$this->set_error('upload_path_not_set');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path)) {
$this->set_error('upload_path_in_correct');
return FALSE;
}
if (!empty($this->files[$field]['name'][0])) {
$check_error = 0;
foreach ($this->files[$field]['name'] as $key => $value) {
$this->file_name = $this->files[$field]['name'][$key];
$this->file_temp = $this->files[$field]['tmp_name'][$key];
$this->file_size = $this->files[$field]['size'][$key];
$this->get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($this->get_file_extension));
if (!in_array($this->get_file_extension_end, $this->allowed_types)) {
$this->set_error('file_extension_not_allowed');
$check_error++;
}
if ($this->files[$field]['size'][$key] > $this->max_size) {
$this->set_error('file_size_check');
$check_error++;
}
if ( ! #copy($this->file_temp, FCPATH . $this->upload_path . '/' . $this->file_name)) {
if ( ! #move_uploaded_file($this->file_temp, FCPATH . $this->upload_path . '/' . $this->file_name)) {
$this->set_error('upload_destination_error', 'error');
$check_error++;
}
}
}
if($check_error > 0 ) {
return FALSE;
}
// This lets me get file data in another function
return $this;
}
}
public function upload_data() {
$data = array(
'file_name' => $this->file_name,
'file_path' => FCPATH . $this->upload_path . '/'
);
return $data;
}
public function set_error($message) {
$this->CI->lang->load('upload', 'english');
$msg = "";
if ($message == 'upload_path_not_set') {
$msg .= $this->CI->lang->line($message);
}
if ($message == 'upload_path_in_correct') {
$msg .= $this->CI->lang->line($message);
}
if ($message == 'file_extension_not_allowed') {
$msg .= sprintf($this->CI->lang->line($message), $this->file_name, $this->get_file_extension_end);
}
if ($message == 'file_size_check') {
$msg .= sprintf($this->CI->lang->line($message), $this->file_name, $this->max_size);
}
return $this->error_message[] = $msg;
}
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {
$message = "";
if (isset($this->error_message)) {
foreach($this->error_message as $msg) {
$message .= $open_tag . $msg . $close_tag;
}
}
return $message;
}
public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
}
return $data;
}
}
I have tried
public function upload_data() {
$data[] = array(
'file_name' => $this->file_name,
'file_path' => FCPATH . $this->upload_path . '/'
);
return $data;
}
Controller index function
public function index(){
$data['error'] = '';
$this->load->library('multiple_upload');
$config['upload_path'] = 'uploads';
$config['allowed_types'] = array('jpg', 'png');
$config['max_size'] = 3000000;
//$config['max_size'] = 1000;
$config['overwrite'] = TRUE;
$this->multiple_upload->set_config($config);
if ($this->multiple_upload->upload() == FALSE) {
$data['error'] = $this->multiple_upload->display_error_messages('<div class="alert alert-danger">', '</div>');
$this->load->view('upload', $data);
} else {
$data = $this->multiple_upload->upload_data();
echo $data['file_name'];
}
}
<?php
class Multiple_upload {
private $filenames;
public function __construct($config = array()) {
$this->CI =& get_instance();
$this->files = $this->clean($_FILES);
$this->filenames = array();
empty($config) OR $this->set_config($config);
}
public function set_config($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
return $this;
}
public function upload($field = 'userfile') {
if (empty($this->upload_path)) {
$this->set_error('upload_path_not_set');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path)) {
$this->set_error('upload_path_in_correct');
return FALSE;
}
if (!empty($this->files[$field]['name'][0])) {
$check_error = 0;
foreach ($this->files[$field]['name'] as $key => $value) {
$this->file_name = $this->files[$field]['name'][$key];
$this->filenames[] = $this->files[$field]['name'][$key];
$this->file_temp = $this->files[$field]['tmp_name'][$key];
$this->file_size = $this->files[$field]['size'][$key];
$this->get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($this->get_file_extension));
if (!in_array($this->get_file_extension_end, $this->allowed_types)) {
$this->set_error('file_extension_not_allowed');
$check_error++;
}
if ($this->files[$field]['size'][$key] > $this->max_size) {
$this->set_error('file_size_check');
$check_error++;
}
if ( ! #copy($this->file_temp, FCPATH . $this->upload_path . '/' . $this->file_name)) {
if ( ! #move_uploaded_file($this->file_temp, FCPATH . $this->upload_path . '/' . $this->file_name)) {
$this->set_error('upload_destination_error', 'error');
$check_error++;
}
}
}
if($check_error > 0 ) {
return FALSE;
}
// This lets me get file data in another function
return $this;
}
}
public function upload_data()
{
$data = array();
foreach($this->filenames as $filename)
{
$data[] = array(
'file_name' => $filename,
'file_path' => FCPATH . $this->upload_path . '/'
);
}
return $data;
}
public function set_error($message) {
$this->CI->lang->load('upload', 'english');
$msg = "";
if ($message == 'upload_path_not_set') {
$msg .= $this->CI->lang->line($message);
}
if ($message == 'upload_path_in_correct') {
$msg .= $this->CI->lang->line($message);
}
if ($message == 'file_extension_not_allowed') {
$msg .= sprintf($this->CI->lang->line($message), $this->file_name, $this->get_file_extension_end);
}
if ($message == 'file_size_check') {
$msg .= sprintf($this->CI->lang->line($message), $this->file_name, $this->max_size);
}
return $this->error_message[] = $msg;
}
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {
$message = "";
if (isset($this->error_message)) {
foreach($this->error_message as $msg) {
$message .= $open_tag . $msg . $close_tag;
}
}
return $message;
}
public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
}
return $data;
}
}
And
public function index()
{
$data['error'] = '';
$this->load->library('multiple_upload');
$config['upload_path'] = 'uploads';
$config['allowed_types'] = array('jpg', 'png');
$config['max_size'] = 3000000;
//$config['max_size'] = 1000;
$config['overwrite'] = TRUE;
$this->multiple_upload->set_config($config);
if ($this->multiple_upload->upload() == FALSE)
{
$data['error'] = $this->multiple_upload->display_error_messages('<div class="alert alert-danger">', '</div>');
$this->load->view('upload', $data);
}
else
{
$data = $this->multiple_upload->upload_data();
foreach($data as $file)
{
echo $file['file_name']."<br>";
}
}
}
you can use array to save all files information in upload_data function.
public function upload_data() {
$data = array(
'file_name' => $this->file_name,
'file_path' => FCPATH . $this->upload_path . '/'
);
return $data;
}
to
public function upload_data() {
$data[] = array(
'file_name' => $this->file_name,
'file_path' => FCPATH . $this->upload_path . '/'
);
return $data;
}
it will return array of all the file details
In Code igniter Model I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
public function fetchdeal_products($id) {
$this->db->select('*');
$this->db->from('products');
$q = $this->db->like("name", $id);
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
//Controller
function ajaxdealcategorydata($id = NULL) {
$this->sma->checkPermissions('index');
$id = $this->input->get('id');
$subcategories = $this->pos_model->getdealcategory($id);
$scats = '';
foreach ($subcategories as $category) {
$scats .= "<button id=\"subcategory-" . $category->id . "\" type=\"button\" value='" . $category->id . "' class=\"btn-prni subcategory\" ><img src=\"assets/uploads/thumbs/" . ($category->image ? $category->image : 'no_image.png') . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded img-thumbnail' /><span>" . $category->name . "</span></button>";
}
$products = $this->ajaxdealproducts($id);
if (!($tcp = $this->pos_model->products_count($id))) {
$tcp = 0;
}
echo json_encode(array('products' => $products, 'subcategories' => $scats, 'tcp' => $tcp));
}
I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.
function ajaxdealproducts() {
$this->sma->checkPermissions('index');
if ($this->input->get('id') ) {
$id = $this->input->get('id');
} else {
$category_id = $this->pos_settings->default_category;
}
if ($this->input->get('subcategory_id')) {
$subcategory_id = $this->input->get('subcategory_id');
} else {
$subcategory_id = NULL;
}
if ($this->input->get('per_page') == 'n') {
$page = 0;
} else {
$page = $this->input->get('per_page');
}
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url() . "pos/ajaxdealproducts";
$config["total_rows"] = $subcategory_id ? $this->pos_model- >products_count($id, $subcategory_id) : $this->pos_model->products_count($id);
$config["per_page"] = $this->pos_settings->pro_limit;
$config['prev_link'] = FALSE;
$config['next_link'] = FALSE;
$config['display_pages'] = FALSE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$this->pagination->initialize($config);
$products = $this->pos_model->fetchdeal_products($id, $config["per_page"], $page);
$pro = 1;
$prods = '<div>';
foreach ($products as $product) {
$count = $product->id;
if ($count < 10) {
$count = "0" . ($count / 100) * 100;
}
if ($category_id < 10) {
$category_id = "0" . ($category_id / 100) * 100;
}
$prods .= "<button id=\"product-" . $category_id . $count . "\" type=\"button\" value='" . $product->code . "' title=\"" . $product->name . "\" class=\"btn-prni btn-" . $this->pos_settings->product_button_color . " product pos-tip\" data-container=\"body\"><img src=\"" . base_url() . "assets/uploads/thumbs/" . $product->image . "\" alt=\"" . $product->name . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded' /><span>" . character_limiter($product->name, 15) . "</span></button>";
$pro++;
}
$prods .= "</div>";
if ($this->input->get('per_page')) {
echo $prods;
} else {
return $prods;
}
}
The database LIKE function only creates a LIKE in the SQL statement. You'll still need to call the GET mothod afterwards; Like this;
$this->db->like('name', $id);
$q = $this->db->get();
You are using $this->db_like() incorrectly.
$this->db->like('title', 'match');
// Produces: WHERE title LIKE '%match%'
(from documentation)
In your case the usage would be
$this->db->like('name', 'rice')
More info
public function fetchdeal_products($id)
{
$query = $this->db->query("Select products.* From products Where (products.name Like '%$id%')");
$result = $query->result_array();
return $result;
}
you have to write it as bellow.
public function fetchdeal_products($id) {
$this->db->like("name", $id);
$q = $this->db->get('products');
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}