CI Event Calendar not showing current month data until clicking next/prev - codeigniter

With this code below CI calendar working fine to view data from database, when click next/prev. but for first time when calendar open with default current month and doesn't show any data from database, cause in ulr after the controller name there is no year, month parameter. I really can't find that what I missed actually.
Controller:
class my_calendar extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('logindatamodel');
$this->load->model('my_calModel');
}
public function showcal($year=null, $month=null){
$user_data['userinfo'] = $this->logindatamodel->login_data();
$data['calendar']=$this->my_calModel->generate($year,$month);
$this->load->view('common/header', $user_data);
$this->load->view('common/menu', $user_data);
$this->load->view('my_calendar',$data);
$this->load->view('common/footer');
}
}
Model:
class my_calModel extends CI_Model{
var $conf;
function __construct() {
parent::__construct();
$this->conf = array(
'start_day' => 'saturday',
'show_next_prev' => TRUE,
'next_prev_url' => base_url().'my_calendar/showcal'
);
}
public function get_calendar_data($year, $month){
$query= $this->db->select('scheduleDate, event_data')
->from('tbl_calendar')
->like('scheduleDate',"$year-$month",'after')
->get();
$cal_data=array();
foreach($query->result() as $row){
$index=ltrim(substr($row->scheduleDate,8,2),'0');
$cal_data[$index]=$row->event_data;
}
return $cal_data;
}
public function generate($year, $month){
$this->load->library('calendar', $this->conf);
$cal_data=$this->get_calendar_data($year, $month);
return $this->calendar->generate($year,$month,$cal_data);
}
}

This is a bit old, the original poster may have gotten it fixed without coming back to tell the forum. But for others who are stuck, here is how I fixed a similar problem. Your controller should look like so:
public function showcal($year=null, $month=null){
if (!$year) {
$year = date('Y');
}
if (!$month) {
$month = date('m');
}
$user_data['userinfo'] = $this->logindatamodel->login_data();
$data['calendar']=$this->my_calModel->generate($year,$month);
$this->load->view('common/header', $user_data);
$this->load->view('common/menu', $user_data);
$this->load->view('my_calendar',$data);
$this->load->view('common/footer');
}
Take note of the initialisation for month and year. The reason is because, since the month and year passed as arguments in the function are set to null, if the month and year aren't entered in the url, the browser assumes there is no date. But by initialising the month and year, the current month and year will always be passed to the controller and model, then the right data would be fetched.
Enough of the rambling. Please original poster should check and accept.
Yours truly.

Related

how to Automatically calculate expiry date in backpack laravel?

i have a field which name is month what i want is when user set the month it automatic calculate with created at month and then upload it to database.
PS - sorry i am new at laravel
Please see Edit:
my store method looks like
<?php
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need
form validation
use App\Http\Requests\ClientsRequest as StoreRequest;
use App\Http\Requests\ClientsRequest as UpdateRequest;
use Carbon\Carbon;
class ClientsCrudController extends CrudController
{
public function setup()
{
$this->crud->setModel('App\Models\Clients');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/clients');
$this->crud->setEntityNameStrings('clients', 'clients');
$this->crud->setFromDb();
}
public function store(StoreRequest $request)
{
// your additional operations before save here
$start_day = Carbon::parse($request->created_at);
$expiry_day = $start_day->addMonths($request->month);
$request->input($expiry_day);
$redirect_location = parent::storeCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
public function update(UpdateRequest $request)
{
// your additional operations before save here
$redirect_location = parent::updateCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
}
Assuming that you mean months ammount ( i.e for a monthly payment )
You can do it like this in your store method i.e:
$start_day = Carbon::parse($request->created_at); //get a carbon instance with created_at as date
$expiry_day = $start_day->addMonths($request->user_selected_months); //add X months to created_at date
public function store(StoreRequest $request) {
date_default_timezone_set('asia/calcutta');
$month = $request->month;
$expiry_date = Carbon::now()->addMonths($month);
$request['expiry_date'] = $expiry_date;
}

How can i get codeigniter session database blob value?

With Codeigniter session.
I have created the ci_session table.
In this table I save session datas in the blob datatype.
$sess_data= array(
'logged_in' => TRUE
);
$this->session->set_userdata($sess_data);
For count all online users, I need to get logged_in==1 value in blob datatype.
For example, user 1 is logged in with Chrome web browser. User2 is logged in with Mozilla. In the database there are 2 sessions with the different session id.
How can i get this all logged_in==1 values in this ci_session table?
This is my model function.
Please help me the fill where place in this function?
public function count_online_users(){
$this->db->select('*');
$this->db->from('ci_session');
$this->db->where(...........);
$query = $this->db->get()->num_rows();
return $query;
}
Check this this method you have to follow
First you have to update logged_in = 1 in your users table as your defined name
after you get number of users are logged_in
// Update user in to call this function
$data = array(
'logged_in' => '1'
);
public function update_user($user_id, $data)
{
$this->db->where('id',$user_id);
$this->db->update('users',$data);
}
// After you get logged in user in total value
public function count_online_users()
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('logged_in','1');
$query = $this->db->get()->num_rows();
return $query;
}
You can use javascript and asynchron request for update field. When you close the window, the handler of window.onunload is called
var unloadHandler = function(e){
//here ajax request to close session
};
window.unload = unloadHandler;
To solve the problem of redirection, php side you can use a counter
class someController extends Controller {
public function methodWithRedirection(){
$this->session->set_userdata('isRedirection', 1);
//here you can redirect
}
}
class homeController extends Controller{
public function closeConnection(){
$this->load->library('session');
if($this->session->userdata('isRedirection')!== 1){
//destroy session
}
}
}

Cakephp 3 does not work for the default language

Sorry for my english, but I hope you will understand me.
Simplified code looks like this:
//in bootstrap.php
ini_set('intl.default_locale', 'deu');
// MainMenusTable.php
public function initialize(array $config)
{
parent::initialize($config);
...
$this->addBehavior('Translate', ['fields' => ['title']]);
...
}
//in controller - THIS WORKS!
public function add()
{
I18n::locale('eng');
$mainMenu = $this->MainMenus->newEntity();
if ($this->request->is('post')) {
$mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
$this->MainMenus->save($mainMenu)
}
$this->set(compact('mainMenu'));
}
// in controller BUT THIS DOES'T WORK:
public function add()
{
I18n::locale('deu');
$mainMenu = $this->MainMenus->newEntity();
if ($this->request->is('post')) {
$mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
$this->MainMenus->save($mainMenu)
}
$this->set(compact('mainMenu'));
}
I have the same problem when I read the record
//in controller - THIS WORKS!
I18n::locale('eng');
$query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();
// in controller BUT THIS DOES'T WORK:
I18n::locale('deu');
$query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();
For 'deu' I manually entered records.
Do you know what the problem is?
Thanks!
This is a solution to the problem from https://github.com/cakephp/cakephp/issues/8416:
The behavior assumes that you store the records in the default language. If the current locale is the same as the default language, then it will just return the records in the database instead of fetching from the translations table.
The title will not be saved in the i18n table for the default language, that is only done for other languages.

codeigniter best way to create controller for editing a db row

Im new to codeigniter and im developing my first web application with it and want to make sure im doing best practices the 1st time so i dont have to go back to make corrections down the road. with that said, here is what im doing.
I want to edit a note in the DB, then after the record has been updated redirect to a different page.
my model is coded correctly so im not worried there, but the controller looks like this (and this is probably not correct:
public function edit($id) {
$this->load->model('Notes_model');
if (isset($_POST["edit"]))
{
$data['data'] = $this->Notes_model->edit($id);
$url = "/Notes/view/" . $id;
redirect($url);
}
$data['notes'] = $this->Notes_model->viewNotes($id);
$this->load->view('templates/header');
$this->load->view('notes/edit', $data);
$this->load->view('templates/footer');
}
hopefull this makes sense, basically what I'm wanting to do here is:
1.) Show the edit note page
2.) if i edited that page by hitting submit
a.) update the db
b.) redirect to a different page.
does this look pretty good or should i make some better changes?
Although your controller code is fine but one thing you have to take care that you should load model in the constructor of your controller so you don't have to include the model in each function same recommendations for the libraries, helpers this is the best practice
class myclass extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Notes_model');
$this->load->helper(form);
}
public function myfunction(){
}
}
Here is the starting tutorial with MVC standards advanced-codeigniter-techniques-and-tricks
<?php
class Home extends CI_Controller
{
function __construct() {
parent::__construct();
$this->m_auth->notLogin();
$this->load->library('form_validation');
$this->load->library('ajax_pagination');
$this->load->library('dateconverter');
$this->load->helper('template');
$this->load->helper('check');
$this->load->model('mymodels/crud_model');
$this->lang->load('personal', $this->m_auth->get_language());
$this->lang->load('global', $this->m_auth->get_language());
}
function index()
{
$this->get_recs();
}
function get_recs()
{
//get for view or first page to be showed
}
/**
* Register New User
*/
function updateRecords()
{
$this->form_validation->set_rules('ministery','<span class="req">(Ministry)</span>','trim|required');
$this->form_validation->set_rules('directorate','<span class="req">(Directorate)</span>','trim|required');
if($this->form_validation->run()==FALSE)
{
header_tpl($this->m_auth->get_language(),'a');
banner_tpl($this->m_auth->get_language(),'a');
left_tpl($this->m_auth->get_language(),'a');
$content = $this->load->view('personal/edit_personal', $this->POST,true);
content_tpl($content);
footer_tpl();
}
else
{
$form_data = array(
'ministry' => $this->input->post('ministery'),
'directorate' => $this->input->post('directorate'),
'job_province' => $this->input->post('job_province'),
'job_district' => $this->input->post('job_district'),
'first_name' => $this->input->post('fname'),
'last_name' => $this->input->post('lname')
);
if($this->crud_model->update_recs('ast_emp_property',$form_data)==TRUE)
{
$this->session->set_flashdata("msg","<span class='m_success'>".$this->lang->line('global_insert_success')."</span>");
redirect('/home/success_reg/'.$id.'','refresh');
}
else
{
$this->session->set_flashdata("msg","<span class='m_error'>".$this->lang->line('global_insert_error')."</span>");
redirect('home','refresh');
}
}
}
}
?>

Codeigniter MVC Sample Code

Hi
I am following the getting started guide for Codeigniterr given at http://www.ibm.com/developerworks/web/library/wa-codeigniter/
I have followed the instruction to create the front view and added controller to handle form submission. Ideally, when i submit the form, it should load the model class and execute the function to put details on the database, but instead it is just printing out the code of the model in the browser.
**Code of view (Welcome.php)**
----------------
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->helper('form');
$data['title'] = "Welcome to our Site";
$data['headline'] = "Welcome!";
$data['include'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
function contactus(){
$this->load->helper('url');
$this->load->model('mcontacts','',TRUE);
$this->mcontacts->addContact();
redirect('welcome/thankyou','refresh');
}
function thankyou(){
$data['title'] = "Thank You!";
$data['headline'] = "Thanks!";
$data['include'] = 'thanks';
$this->load->vars($data);
$this->load->view('template');
}
}
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
**Code of Model**
--------------
class mcontacts extends Model{
function mcontacts(){
parent::Model();
}
}
function addContact(){
$now = date("Y-m-d H:i:s");
$data = array(
'name' => $this->input->xss_clean($this->input->post('name')),
'email' => $this->input->xss_clean($this->input->post('email')),
'notes' => $this->input->xss_clean($this->input->post('notes')),
'ipaddress' => $this->input->ip_address(),
'stamp' => $now
);
$this->db->insert('contacts', $data);
}
**OUTPUT after clicking submit**
-----------------------------
class mcontacts extends Model{ function mcontacts(){ parent::Model(); } } function addContact(){ $now = date("Y-m-d H:i:s"); $data = array( 'name' => $this->input->xss_clean($this->input->post('name')), 'email' => $this->input->xss_clean($this->input->post('email')), 'notes' => $this->input->xss_clean($this->input->post('notes')), 'ipaddress' => $this->input->ip_address(), 'stamp' => $now ); $this->db->insert('contacts', $data); }
I have tried doing these things
1. Making all PHP codes executable
2. Change ownership of files to www-data
3. make permission 777 for whole of www
But, the code of model seems to be just printed ... PLEASE HELP
Just a few minor points that might help you:
In your controller, point the index method at the method you would like to call on that page. For example:
function index()
{
$this->welcome();
}
That will help keep things clean and clear, especially if anyone else comes in to work on the code with you later. I chose welcome because that's the name of your controller class and that will keep things simple.
In your model, this:
class mcontacts extends Model{
Should be:
class Mcontacts extends Model{
Capitalize those class names! That could be giving you the trouble you describe.
See here for more info on this: http://codeigniter.com/user_guide/general/models.html
Don't use camel case in your class or method names. This isn't something that will cause your code to fail, but it's generally accepted practice. See Codeigniter's PHP Style guide for more information on this: http://codeigniter.com/user_guide/general/styleguide.html
It's difficult to see with the formatting as it is, but do have an extra curly brace after the constructor method (mcontacts()) in the model? This would cause problems! Also although the code looks generally ok, there are probably better ways to use the framework especially if you do anything more complicated than what you've shown. For example, autoloading, form validation etc. Can I suggest you have a read of the userguide? It's very thorough and clear and should help you alot. http://codeigniter.com/user_guide/index.html

Resources