Codeigniter calendar how to get multiple events per day - codeigniter

I am currently working on codeIgniter 3 calendar library where I can add new rows of information to database on each day of the month
If a day has 2 rows that are inserted I should be able to see the two calendar events on that day.
2015-12-25 has to rows as shown in image.
I would like to be able to show instead of just one event but to show what ever is on that day.
Question: How could I get the get_calendar_data function to be able to get rows that are on that day of calendar. Currently on 2015-12-25 it only displays one row.
Get Data Function
public function get_calendar_data($year, $month) {
$cell_data = array();
$query = $this->db->get('calendar');
if ($query->num_rows() > 0) {
$row = $query->row();
$this->db->select('*');
$this->db->from('calendar');
$this->db->where('year', $year);
$this->db->where('month', $month);
$this->db->where('day', $row->day);
$query = $this->db->get();
foreach ($query->result() as $result) {
$cell_data[$result->day] = $result->data;
}
return $cell_data;
}
}
Controller
<?php
class Calendar extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('dashboard/model_calendar');
$this->load->library('calendar');
}
public function index() {
if ($this->uri->segment(3) == FALSE) {
$year = date('Y');
} else {
$year = $this->uri->segment(3);
}
$data['year'] = $year;
if ($this->uri->segment(4) == FALSE) {
$month = date('m');
} else {
$month = $this->uri->segment(4);
}
$data['month'] = $month;
$prefs = array(
'start_day' => 'monday',
'show_next_prev' => true,
'day_type' => 'long',
'next_prev_url' => base_url('dashboard/calendar')
);
$prefs['template'] = '
{table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered calendar">{/table_open}
{heading_row_start}<tr>{/heading_row_start}
{heading_previous_cell}<th><i class="fa fa-chevron-left fa-2x "></i></th>{/heading_previous_cell}
{heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell}
{heading_next_cell}<th class="text-right"><i class="fa fa-chevron-right fa-2x"></i></th>{/heading_next_cell}
{heading_row_end}</tr>{/heading_row_end}
{week_row_start}<tr>{/week_row_start}
{week_day_cell}<td>{week_day}</td>{/week_day_cell}
{week_row_end}</tr>{/week_row_end}
{cal_row_start}<tr class="days">{/cal_row_start}
{cal_cell_start}<td class="day">{/cal_cell_start}
{cal_cell_content}
<div class="day_number">{day}</div>
<div class="content" style="margin-top: 10px;">{content}</div>
{/cal_cell_content}
{cal_cell_content_today}
<div class="day_number highlight">{day}</div>
<div class="content" style="margin-top: 10px;">{content}</div>
{/cal_cell_content_today}
{cal_cell_no_content}
<div class="day_number">{day}</div>
{/cal_cell_no_content}
{cal_cell_no_content_today}
<div class="day_number highlight">{day}</div>
{/cal_cell_no_content_today}
{cal_cell_blank} {/cal_cell_blank}
{cal_cell_end}</td>{/cal_cell_end}
{cal_row_end}</tr>{/cal_row_end}
{table_close}</table></div>{/table_close}
';
$this->calendar->initialize($prefs);
$this->delete_calendar_event();
if ($this->input->post('day')) {
//if ($this->check_calendar_event($year, $month, $this->input->post('day'))) {
// $this->update_calendar_event($year, $month);
//} else {
$this->add_calendar_event($year, $month);
//}
}
$data = $this->get_calendar_data($year, $month);
$data['calendar'] = $this->calendar->generate($year, $month, $data);
if ($this->uri->segment(3) == TRUE) {
$data['view_more'] = site_url('report/events/'. $year .'/'. $month);
} else {
$data['view_more'] = site_url('report/events');
}
$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y')));
//echo $date;
// echo '<br/>';
//echo $this->test();
$this->load->view('dashboard/calender_view', $data);
}
public function test() {
$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y')));
$this->db->where('date <', $date);
$query = $this->db->get('calendar');
return $query->num_rows();
}
public function add_calendar_event($year, $month) {
$date = $year .'-'. $month .'-'. $this->input->post('day');
$calendar = array(
'year' => $year,
'month' => $month,
'day' => $this->input->post('day', TRUE),
'date' => $date,
'data' => $this->input->post('event_data')
);
$this->db->insert('calendar', $calendar);
}
public function update_calendar_event($year, $month) {
$date = $year .'-'. $month .'-'. $this->input->post('day');
$calendar = array(
'year' => $year,
'month' => $month,
'day' => $this->input->post('day', TRUE),
'data' => $this->input->post('event_data')
);
$this->db->where('date', $date);
$this->db->update('calendar', $calendar);
}
public function delete_calendar_event() {
$this->db->where("date <", date('Y-m-d'));
$this->db->or_where('data', '');
$this->db->delete('calendar');
}
public function get_calendar_data($year, $month) {
$cell_data = array();
$query = $this->db->get('calendar');
if ($query->num_rows() > 0) {
$row = $query->row();
$this->db->select('*');
$this->db->from('calendar');
$this->db->where('year', $year);
$this->db->where('month', $month);
$this->db->where('day', $row->day);
$query = $this->db->get();
foreach ($query->result() as $result) {
$cell_data[$result->day] = $result->data;
}
return $cell_data;
}
}
public function check_calendar_event($year, $month, $day) {
$date = $year .'-'. $month .'-'. $day;
$this->db->select('year, month, day');
$this->db->from('calendar');
$this->db->where('date', $date);
$results = $this->db->count_all_results();
return $results;
}
public function check_calendar_event_date() {
}
}

Solved!
After a long time searching the web and trying out new things I found that this code function array_key_exists below lets me get multiple events for each day.
public function get_events($year, $month) {
$calendar = array();
$this->db->where('year', $year);
$this->db->where('month', $month);
$query = $this->db->get('calendar');
$results = $query->result_array();
foreach ($results as $event) {
if (array_key_exists($event['day'], $calendar)) {
$calendar[$event['day']] = $calendar[$event['day']] .'<ul class="list-unstyled"><li>'. anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>';
} else {
$calendar[$event['day']] = '<ul class="list-unstyled"><li>' . anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>';
}
}
return $calendar;
}
Ash shown here in image working:
Controller
<?php
class Calendar extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('dashboard/model_calendar');
$this->load->library('calendar');
}
public function index() {
if ($this->uri->segment(3) == FALSE) {
$year = date('Y');
} else {
$year = $this->uri->segment(3);
}
$data['year'] = $year;
if ($this->uri->segment(4) == FALSE) {
$month = date('m');
} else {
$month = $this->uri->segment(4);
}
$data['month'] = $month;
$prefs = array(
'start_day' => 'monday',
'show_next_prev' => true,
'day_type' => 'long',
'next_prev_url' => base_url('dashboard/calendar')
);
$prefs['template'] = '
{table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered calendar">{/table_open}
{heading_row_start}<tr>{/heading_row_start}
{heading_previous_cell}<th><i class="fa fa-chevron-left fa-2x "></i></th>{/heading_previous_cell}
{heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell}
{heading_next_cell}<th class="text-right"><i class="fa fa-chevron-right fa-2x"></i></th>{/heading_next_cell}
{heading_row_end}</tr>{/heading_row_end}
{week_row_start}<tr>{/week_row_start}
{week_day_cell}<td>{week_day}</td>{/week_day_cell}
{week_row_end}</tr>{/week_row_end}
{cal_row_start}<tr class="days">{/cal_row_start}
{cal_cell_start}<td class="day">{/cal_cell_start}
{cal_cell_content}
<div class="day_number">{day}</div>
<div class="content" style="margin-top: 10px;">{content}</div>
{/cal_cell_content}
{cal_cell_content_today}
<div class="day_number highlight">{day}</div>
<div class="content" style="margin-top: 10px;">{content}</div>
{/cal_cell_content_today}
{cal_cell_no_content}
<div class="day_number">{day}</div>
{/cal_cell_no_content}
{cal_cell_no_content_today}
<div class="day_number highlight">{day}</div>
{/cal_cell_no_content_today}
{cal_cell_blank} {/cal_cell_blank}
{cal_cell_end}</td>{/cal_cell_end}
{cal_row_end}</tr>{/cal_row_end}
{table_close}</table></div>{/table_close}
';
$this->calendar->initialize($prefs);
$this->model_calendar->delete_calendar_event($year, $month);
if ($this->input->post('day')) {
if ($this->model_calendar->check_calendar_event($year, $month, $this->input->post('day'))) {
$this->model_calendar->update_calendar_event($year, $month);
} else {
$this->vadd_calendar_event($year, $month);
}
}
$events = $this->model_calendar->get_events($year, $month);
$data['calendar'] = $this->calendar->generate($year, $month, $events);
if ($this->uri->segment(3) == TRUE) {
$data['view_more'] = site_url('report/events/'. $year .'/'. $month);
} else {
$data['view_more'] = site_url('report/events');
}
$this->load->view('dashboard/calender_view', $data);
}
}
Model
<?php
class Model_calendar extends CI_Model {
public function test() {
$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 3, date('Y')));
$this->db->where('date <', $date);
$query = $this->db->get('calendar');
return $query->num_rows();
}
public function add_calendar_event($year, $month) {
$date = $year .'-'. $month .'-'. $this->input->post('day');
$calendar = array(
'year' => $year,
'month' => $month,
'day' => $this->input->post('day', TRUE),
'date' => $date,
'data' => $this->input->post('event_data')
);
$this->db->insert('calendar', $calendar);
}
public function update_calendar_event($year, $month) {
$date = $year .'-'. $month .'-'. $this->input->post('day');
$calendar = array(
'year' => $year,
'month' => $month,
'day' => $this->input->post('day', TRUE),
'data' => $this->input->post('event_data')
);
$this->db->where('date', $date);
$this->db->update('calendar', $calendar);
}
public function delete_calendar_event() {
$this->db->where("date <", date('Y-m-d'));
$this->db->or_where('data', '');
$this->db->delete('calendar');
}
public function get_events($year, $month) {
$calendar = array();
$this->db->where('year', $year);
$this->db->where('month', $month);
$query = $this->db->get('calendar');
$results = $query->result_array();
foreach ($results as $event) {
if (array_key_exists( $event['day'], $calendar ) ) {
$calendar[$event['day']] = $calendar[$event['day']] .'<ul class="list-unstyled"><li>'. anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>';
} else {
$calendar[$event['day']] = '<ul class="list-unstyled"><li>' . anchor(base_url() .'report/events/'. $year .'/'. $month .'/'. $event['calendar_id'], $event['data']) . '</li></ul>';
}
}
return $calendar;
}
public function check_calendar_event($year, $month, $day) {
$date = $year .'-'. $month .'-'. $day;
$this->db->select('year, month, day');
$this->db->from('calendar');
$this->db->where('date', $date);
$results = $this->db->count_all_results();
return $results;
}
public function check_calendar_event_date() {
}
}

this is a great answer, I was trying to modify the codeigniter calendar making MY_calendar, but this is much better.
I still didnt add the links to the events.
public function get_datos_calendario($year, $month)
{
$this->db->like('fecha', "$year-$month", 'after');
$query = $this->db->get('tareas');
$datos_calendario = array();
$results = $query->result_array();
foreach ($results as $event) {
$day = ltrim(substr($event['fecha'], 8, 2), '0');
if (array_key_exists( $day, $datos_calendario ) ) {
$datos_calendario[$day] = $datos_calendario[$day] .'<ul class="list-unstyled"><li>'. $event["descripcion"] . '</li></ul>';
} else {
$datos_calendario[$day] = '<ul class="list-unstyled"><li>' . $event["descripcion"] . '</li></ul>';
}
}

Related

How to get/view images dynamically in homepage using CodeIgniter?

image
New_model
=========
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class New_model extends CI_Model
{
private $_table = "news";
public $title;
public $date;
public $description;
public $image = "default.jpg";
public $url;
public function rules()
{
return [
['field' => 'title',
'label' => 'Title',
'rules' => 'required'],
['field' => 'date',
'label' => 'Date',],
// 'rules' => 'required'],
['field' => 'description',
'label' => 'Description',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
public function save()
{
$post = $this->input->post();
// $this->new_id = uniqid();
$this->title = $post["title"];
$this->date = $post["date"];
$this->description = $post["description"];
$this->image = $this->_uploadImage();
$this->db->insert($this->_table, $this);
// $this->url = base_url() . 'uploads/';
$post['url'] = base_url() . 'uploads/';
}
public function update()
{
$post = $this->input->post();
$this->new_id = $post["id"];
$this->title = $post["title"];
$this->date = $post["date"];
$this->description = $post["description"];
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->db->update($this->_table, $this, array('new_id' => $post['id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("new_id" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['file_name'] = $this->new_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
private function _deleteImage($id)
{
$new = $this->getById($id);
if ($new->image != "default.jpg") {
$filename = explode(".", $new->image)[0];
return array_map('unlink', glob(FCPATH."uploads/$filename.*"));
}
}
}
public function single($id)
{
$data['news'] = $this->New_model->where('new_id',$id)->order_by('new_id','desc')->get_all();
$data['news1'] = $this->New_model->limit(5)->order_by('id','desc')->get_all();
$this->current = 'news';
$this->load->view(['current' => $this->current]);
$this->load->view('news',$data);
}
Controller
==========
public function index()
{
$data["news"] = $this->New_model->getAll();
$this->load->view('index',$data);
}
public function latestnews()
{
$data['news'] = $this->New_model->where('new_id',$id)->order_by('new_id','desc')->get_all();
$this->load->view('news',$data);
}
view
====
<?php
if (isset($news) and $news) {
foreach ($news as $new) {
?>
<div class="tile-primary-content">
<img src="<?php echo $new->url . $new->image;?>" alt="image" />
</div>
<div class="tile-secondary-content">
<div class="section-title-1">
<span class="title"><?php echo $new->title;?></span>
</div>
<h2 class="heading-20"><?php echo $new->date;?></h2>
</div>
<?php
}
}
?>
I'm trying to upload an image in the dashboard with a title and date along with the image upload.
How to display that image on the homepage?
How to set the URL in the New_model?
How to set the links to display the images in the homepage?
How to set the date links?
The title field can be viewed in the homepage, but no image and dateā€¦
Try this code.
Controller: (Edited)
function index() {
$this->data["news"] = $this->new_model->getAll();
$this->load->view('index', $this->data);
}
function single_news($id) {
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
function add_news() {
$image_status = FALSE;
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
if($this->form_validation->run() == TRUE) {
$image_name = NULL;
if ($_FILES['image']['size'] > 0) {
$this->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1024';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
if( ! $this->upload->do_upload('image')){
$this->data['error'] = $this->upload->display_errors();
}
$image_name = $this->upload->file_name;
$image_status = TRUE;
}
$data = array(
'title' => $this->input->post('title'),
'date' => $this->input->post('date')?date('Y-m-d', strtotime($this->input->post('date'))):NULL,
'description' => $this->input->post('description'),
'image' => $image_name
);
}
if($this->form_validation->run() == TRUE && $image_status && $this->new_model->addNews($data)) {
$this->session->set_flashdata('message', 'News has been created successfully.');
redirect('index');
} else {
$this->data['error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error');
$this->data['page_title'] = 'Add News';
$this->load->view('add_news', $this->data);
}
}
When adding new News you don't need to add the uploaded location (Not necessary).
Model: (Edited)
function getAll() {
$q = $this->db->get('news');
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
function addNews($data) {
if($this->db->insert('news', $data)){
return true;
}
return false;
}
View: (Edited)
<?php
if ($news) {
foreach ($news as $new) {
?>
<div class="tile-primary-content">
<a href="<?php echo base_url('single_news/'.$new->id); ?>"
<img src="<?php echo site_url('uploads/'.$new->image); ?>" alt="image" />
</a>
</div>
<div class="tile-secondary-content">
<div class="section-title-1">
<span class="title"><?php echo $new->title;?></span>
</div>
<h2 class="heading-20"><?php echo !is_null($new->date)?date('d.m.Y', strtotime($new->date)):' - ';?></h2>
</div>
<?php
}
}
?>
single-news.php: (View)
...
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
...

Stop form validation submitting if error on file upload

Question: When the user has filled in the form validation correct but there is a error on image upload how is it possible to stop the form being submitted
Currently still submits data even if there is a error on my image upload.
<?php
class Users extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('countries');
$this->load->library('upload');
$this->load->model('admin/user/user_model');
$this->load->model('admin/user_group/usergroup_model');
}
public function add() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
if ($this->form_validation->run() == true) {
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {
if (!is_dir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/')) {
mkdir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/');
}
$config['upload_path'] = './uploads/users/' . $this->input->post('username') . '/';
$config['allowed_types'] = 'gif|png';
$config['max_size'] = 3000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile')) {
return true;
}
}
$this->user_model->insert($this->upload->data());
}
if ($this->upload->display_errors() != false) {
$this->error['warning'] = $this->upload->display_errors();
}
if (validation_errors() != false) {
$this->error['warning'] = validation_errors('<div class="alert alert-danger">', '</div>');
}
if (isset($this->error['warning'])) {
$data['warning_error'] = $this->error['warning'];
} else {
$data['warning_error'] = '';
}
$data['countries'] = $this->countries->get();
$data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
$data['usergroups'] = $this->usergroup_model->get_user_groups();
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('user/add_view', $data);
}
}
Model
<?php
class User_model extends CI_Model {
public function insert($upload_data = array()) {
$this->db->trans_begin();
$options = [
'cost' => 12,
];
$hash = password_hash($this->input->post('password'), PASSWORD_BCRYPT, $options);
$data = array(
'user_group_id' => $this->input->post('user_group_id'),
'username' => $this->input->post('username'),
'password' => $hash,
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'image' => ($upload_data['file_name']) ? $upload_data['file_name'] : '',
'country' => $this->input->post('country'),
'timezone' => $this->input->post('timezone'),
'status' => $this->input->post('status'),
'date_added' => date('Y-m-d')
);
$this->db->set($data);
$this->db->insert($this->db->dbprefix . 'user');
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
} else {
$this->db->trans_commit();
}
}
}
Codeigniter Version 3.1.0 & XAMPP Windows 10
Have solved it now.
<?php
class Users extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('countries');
$this->load->library('upload');
$this->load->model('admin/user/user_model');
$this->load->model('admin/user_group/usergroup_model');
}
public function add() {
$this->form_validation->set_rules('username', 'username', 'trim|required|is_unique[user.username]');
$this->form_validation->set_message('is_unique', 'Opps looks like someone has all ready got that {field}');
if ($this->form_validation->run() == true) {
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {
if ($this->upload() == true) {
$this->user_model->insert($this->upload->data());
}
} else {
$this->user_model->insert();
}
}
if (validation_errors() != '') {
$this->error['warning'] = validation_errors('<div class="alert alert-danger">', '</div>');
}
if (isset($this->error['warning'])) {
$data['warning_error'] = $this->error['warning'];
} else {
$data['warning_error'] = '';
}
$data['countries'] = $this->countries->get();
$data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
$data['usergroups'] = $this->usergroup_model->get_user_groups();
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('user/add_view', $data);
}
public function upload() {
if (!is_dir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/')) {
mkdir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/');
}
$config['upload_path'] = './uploads/users/' . $this->input->post('username') . '/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$this->error['warning'] = $this->upload->display_errors('<div class="alert alert-danger">', '</div>');
} else {
return true;
}
return !$this->error;
}
}

When echo multiple file data it only display last file info

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

Magento Issues with category dropdown

I need to display all the subcategories under root category name using . I am unable to find the root category details. Root category name is displaying as '/' How to overcome this issue?
Currently i am getting all the categories including root categories (/).
My code is as in below:
public function toOptionArray()
{
$categories = array();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter(
'path',
array('neq' => '1')
);
foreach ($categoryCollection as $category) {
var_dump($category->getData());
echo '<br />';
$category = Mage::getModel('catalog/category')->load($category->getId());
$categories[$category->getId()] = $category->getUrlPath();
}
return $categories;
}
protected function _renderOptions(Varien_Object $row)
{
$categories = $this->getColumn()->getOptions();
$html = sprintf('<select class="category_select" name="mapping[%s]">', $row->getData('reference_id'));
$html .= '<option value=""></option>';
foreach ($categories as $id => $name) {
$html .= sprintf(
'<option value="%s"%s>%s</option>',
$this->escapeHtml($id),
$id == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
}
$html .= '</select>';
return $html;
}
I need to add into the category dropdown, which is subcategories displaying under root category name.
Can anyone help me please.
Thank You.
I fixed the issue using following:
protected function _renderOptions(Varien_Object $row)
{
$categories = $this->getColumn()->getOptions();
$parentIdArray = array();
foreach ($categories as $id => $name) {
$category = Mage::getModel('catalog/category')->load($id);
$parentId = $category->getParentId();
if($parentId == 1){
$parentIdArray[] = $id;
}
}
$html = sprintf('<select class="category_select" name="mapping[%s]">', $row->getData('reference_id'));
$html .= '<option value=""></option>';
foreach ($parentIdArray as $parentId) {
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$name = $parentCategory->getName();
$html .= sprintf(
'<optgroup label="'.$name.'">
<option value="%s"%s>%s</option>
</optgroup>',
$this->escapeHtml($parentId),
$parentId == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
$categories = Mage::getModel('catalog/category')->getCategories($parentId);
$subCategories = $this->get_categories($categories);
foreach ($subCategories as $id => $name) {
$html .= sprintf(
'<option style="padding-left: 10px;" value="%s"%s>%s</option>',
$this->escapeHtml($id),
$id == $row->getData('category_id') ? ' selected="selected"' : '',
$this->escapeHtml($name)
);
}
}
$html .= '</select>';
return $html;
}
public function get_categories($categories) {
$subCategories = array();
foreach($categories as $category) {
$subCategory = Mage::getModel('catalog/category')->load($category->getId());
$subCategories[$subCategory->getId()] = $subCategory->getUrlPath();
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$childrenCategories = $this->get_categories($children);
foreach ($childrenCategories as $id => $name) {
$subCategories[$id] = $name;
}
}
}
return $subCategories;
}

Dynamic Select Dropdowns with CodeIgniter

I'm using CodeIgniter to develop a web application and wondered if anyone could help with a problem that has arose.
I have two dropdowns - Division and Teams. I'm creating a fixture so want to be able to allow the user to choose a division from the first dropdown, then within the second dropdown will only contain the teams from the selected division.
The Code I have so far:
Controller:
function create() {
$data['division_list'] = $this->add_fixture_model->get_divisions();
$data['team_list'] = $this->add_fixture_model->get_teams();
$data['referee_list'] = $this->add_fixture_model->get_referee();
// field name, error message, validation rules
$this->form_validation->set_rules('team_name', 'Team Name', 'trim|required');
$this->form_validation->set_rules('home_team', 'Home Team Name', 'trim|required');
$this->form_validation->set_rules('away_team', 'Away Team Name', 'required');
$this->form_validation->set_rules('division_name', 'Division', 'trim|required');
$this->form_validation->set_rules('referee', 'Referee', 'trim|required');
$this->form_validation->set_rules('fixture_week', 'Fixture Week', 'trim|required');
$this->form_validation->set_rules('fixture_day', 'Fixture Day', 'trim|required');
$this->form_validation->set_rules('fixture_month', 'Fixture Month', 'trim|required');
$this->form_validation->set_rules('fixture_year', 'Fixture Year', 'trim|required');
$this->form_validation->set_rules('confirm_day', 'Fixture Confirm Day', 'trim|required');
$this->form_validation->set_rules('confirm_month', 'Fixture Confirm Month', 'trim|required');
$this->form_validation->set_rules('confirm_year', 'Fixture Confirm Year', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/add_fixture', $data);
$this->load->view('includes/footer');
}
else
{
if($query = $this->add_fixture_model->add_fixture())
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/fixture_added');
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/fixture_added', $data);
$this->load->view('includes/footer');
}
}
}
Model:
class Add_fixture_model extends Model {
function add_fixture()
{
$add_new_fixture = array(
'team_name' => $this->input->post('team_name'),
'home_team' => $this->input->post('home_team'),
'away_team' => $this->input->post('away_team'),
'division' => $this->input->post('division_name'),
'ground' => $this->input->post('ground'),
'ground_postcode' => $this->input->post('ground_postcode'),
'referee' => $this->input->post('referee'),
'fixture_week' => $this->input->post('fixture_week'),
'fixture_day' => $this->input->post('fixture_day'),
'fixture_month' => $this->input->post('fixture_month'),
'fixture_year' => $this->input->post('fixture_year'),
'confirm_day' => $this->input->post('confirm_day'),
'confirm_month' => $this->input->post('confirm_month'),
'confirm_year' => $this->input->post('confirm_year')
);
$insert = $this->db->insert('fixtures', $add_new_fixture);
return $insert;
}
function get_divisions()
{
$this->db->from('divisions');
$this->db->order_by('division_name');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a division';
$return[$row['id']] = $row['division_name'];
}
}
return $return;
}
function get_teams()
{
$this->db->from('teams');
$this->db->order_by('team_name');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a team';
$return[$row['id']] = $row['team_name'];
}
}
return $return;
}
function list_teams() {
$this->db->from('teams');
$this->db->where('division_id', $this->input->post('id'));
$this->db->order_by('team_name');
$result = $this->db->get();
return $result;
}
function get_referee() {
$this->db->from('referees');
$this->db->order_by('id');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[''] = 'Select a referee';
$return[$row['id']] = $row['first_name'].' '.$row['surname'];
}
}
return $return;
}
}
View:
<?php echo form_open('add_fixture/create') ;?>
<label for="division" class="label">Division:</label><?php $js = 'id="division"'; echo form_dropdown('division', $division_list, set_value('division'), $js); ?>
JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$('select#division').change(function() {
var div = $(this).val();
if(div != '') {
$.post("/add_fixture/list_teams/",{division_id: div}, function(data){
$("select#team_name").html(data);
});
}
});
});
</script>
Any help would very much be appreciated :)

Resources