Laravel - How to get id for other function in controller - laravel

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);

Related

yajra laravel datatables - order by custom employee working hours sum Column ['total_hrs']

**yajra laravel datatables
order by custom employee working hours sum Column ['total_hrs']
After server side order by**
return Datatables::eloquent($model)
->addColumn('username', function (User $data) {
return 'getImageUrlAttribute($data->id).'" alt="user_id_'.$data->id.'" class="profile-user-img-small img-circle"> '. $data->name;
})
->addColumn('branch', function (User $data) {
$branches='';
foreach($data->branches as $branch){
$branches .= '<span class="selection_choice">'.$branch->company->name.' - '.$branch->name.'</span>';
}
return $branches;
})
->addColumn('search_username', function (User $data) {
return 'user_id_'.$data->id;
})
->addColumn('total_hrs', function (User $data) {
$totalDuration = 0;
foreach ($data->attendance_creator as $attendance_creator) {
if($attendance_creator->punch_out!=null){
$startTime = Carbon::parse($attendance_creator->attendance_at);
$endTime = Carbon::parse($attendance_creator->punch_out->attendance_at);
$mins = $endTime->diffInMinutes($startTime, true)/60;
$totalDuration = $totalDuration + $mins;
}
}
return number_format($totalDuration,2,".","");
})
->orderColumn('total_hrs', function ($query, $order) {
$query->orderBy('total_hrs', $order);
})
->rawColumns(['total_hrs', 'username', 'branch', 'search_username']) ->make(true);
}

how to show image in laravel yajra datatables?

I have this error GEThttp://localhost:8000/report/[object%20Object]
I want to display user image in reports table.
my controller code:
public function index(Request $request)
{
if ($request->ajax()) {
$data = Report::with('user','presidents', 'years', 'quarters', 'points')->select('reports.*');
return Datatables::of($data)
// ->addIndexColumn()
->addColumn('created_at', function ($data) {
return jdate($data->created_at)->format('Y-m-d');
})
->addColumn('image', function ($report) {
$url=asset("$report->user->image['thumb']");
return '<img src='.$url.' border="0" width="40" class="img-rounded" align="center" />';
})
->addColumn('action', 'action', 'image')
->make(true);
}
return view('reports.index');
}
js code:
{ data: 'user.image', name: 'user.image',
render: function( data, type, full, meta ) {
return "<img src=\"/report/" + data + "\" height=\"50\"/>";
}
},
I changed my controller and js code: it is work
public function index(Request $request)
{
if ($request->ajax()) {
$data = Report::with('user','presidents', 'years', 'quarters', 'points')->select('reports.*');
return Datatables::of($data)
// ->addIndexColumn()
->addColumn('created_at', function ($data) {
return jdate($data->created_at)->format('Y-m-d');
})
->addColumn('user.image', function ($data) {
return ' '.$data->user->image['thumb'].' ';
})
->addColumn('action', 'action')
->make(true);
}
return view('reports.index');
}
js code:
{ data: 'user.image', name: 'user.image',
render: function( data, type, full, meta ) {
return "<img src=\"" + data + "\" height=\"50\"/>";
}
},

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); ?>" />
...

Laravel Nova ignore a field if empty using ->fillUsing()

I have 3 fields "name", "email" and "url". These 3 fields are casted to a json in 1 column in my database.
Now if you fill in just the url I want to save only {url: "value"} in the database. If you fill in email and name I want to save only {name: "john", email: "john#gmail.com"} in the database.
This is how I try do to it:
Text::make('To Name', 'toName')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toName;
}
),
Text::make('To Email', 'toEmail')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toEmail;
}
),
Text::make('To Url', 'toUrl')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toUrl;
}
),
But I keep getting this error:
General error: 1364 Field 'to' doesn't have a default value
Am I returning something wrong?
Check this code, use Laravel mutator and some changes in your fillUsing. Note toName, toEmail, toUrl are virtual attributes, and to_json contain json values of these columns in model!
// database\migrations\2019_08_28_045853_create_infos_table.php
...
Schema::create('infos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('label',100);
$table->json('to_json');
});
// app\Nova\Info.php
...
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Label'),
Text::make('To Name', 'toName')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toName)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['name'] = $request->toName;
$model['to_json'] = json_encode($json);
}
}
),
Text::make('To Email', 'toEmail')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toEmail)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['email'] = $request->toEmail;
$model['to_json'] = json_encode($json);
}
}
),
Text::make('To Url', 'toUrl')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toUrl)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['url'] = $request->toUrl;
$model['to_json'] = json_encode($json);
}
}
),
];
}
// app\Info.php
...
class Info extends Model
{
public function gettoNameAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['name'])){
return $result['name'];
}else{
return "";
}
}
public function gettoEmailAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['email'])){
return $result['email'];
}else{
return "";
}
}
public function gettoUrlAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['url'])){
return $result['url'];
}else{
return "";
}
}
}

Codeigniter calendar how to get multiple events per day

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>';
}
}

Resources