Make an attendance sheet with jqGrid - php

I want to make an attendance sheet with jqGrid. I'm using PHP and Mysql
I have two tables, one called MemberInfo and one called Attendance.
From the MemberInfo I want to show in the grid the first name and the last name of the member. Then I want to have a box for every day of the week. I want that when I add some data to those fields, for the data to be saved in the Attendance table and also that if I generate the Attendance grid again, the fields that were already filled up, to show the data.
My question is:
How can I add more columns and How can I connect those columns with the Attendance table? Thanks!
EDIT:
I was able to generate new columns and to add the data to the database with cellEdit. Still having problems with generating the grid with the data from 2 tables. Thanks!
I hope this is clear! if its not please let me know! thanks!
(if there is another library for PHP that would make this easier please let me know)
EDIT:
<?php
require_once 'jqgrid/jq-config.php';
// include the jqGrid Class
require_once "jqgrid/php/jqGrid.php";
// include the driver class
require_once "jqgrid/php/jqGridPdo.php";
// Connection to the server
$conn = new PDO("mysql:host=localhost;dbname=db;","root",NULL);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = 'SELECT member_id, first_name, last_name FROM members_info WHERE member_type !=5';
// set the ouput format to json
$grid->dataType = 'json';
$grid->table ="members_info";
$grid->setPrimaryKeyId("member_id");
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('grid.php');
$grid->cacheCount = true;
// Set grid caption using the option caption
$today = date('Y-m-d');
if(isset($_POST['past_month'])){
$today = date('Y-m-d', strtotime($_POST['past_month']));
}
if(isset($_POST['next_month'])){
$today = date('Y-m-d', strtotime($_POST['next_month']));
}
$days = attendance_cal(date('F', strtotime($today)), date('Y', strtotime($today))); // Gets the days for that month and that year
sort($days); //sort the days
foreach($days as $day){
$grid->addCol(array(
"name"=>date('m-d', $day)
));
}
$grid->setGridOptions(array(
"caption"=>"This is custom Caption",
"rowNum"=>30000,
"sortname"=>"member_id",
"hoverrows"=>true,
"width"=>1000,
"height"=>1000,
"cellEdit"=> true,
"cellsubmit"=>"remote",
"cellurl"=> "cell_dump.php",
"rowList"=>array(10,20,50),
"postData"=>array("grid_recs"=>776)
));
// Change some property of the field(s)
$grid->setColProperty("member_id", array("label"=>"ID", "width"=>60, "editable"=>false));
$grid->setColProperty("first_name", array("label"=>"First Name", "width"=>120, "editable"=>false));
$grid->setColProperty("last_name", array("label"=>"Last Name", "width"=>120, "editable"=>false));
// Enjoy
$grid->navigator = false;
// and finaly bind key navigation
// This is way if no events or parameter
//$grid->callGridMethod('#grid', 'bindKeys');
//
//in case of passing events is better this way
$bindkeys =<<<KEYS
$("#grid").jqGrid('bindKeys', {"onEnter":function( rowid ) { alert("You enter a row with id:"+rowid)} } );
KEYS;
$grid->setJSCode($bindkeys);
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
?>
Let me be more specific:
My table "Members" has the fields "member_id", "first_name", "last_name"
The "Attendance" table has the fields "attendance_id", "member_id", "attendance_date" ,"attendance_value"
My Grid, I want it to look like:
| Member Id | Name | 03-15-2012 | 03-20-2012 | 03-22-2012 |
The "Member Id" column and "Name" column is being generated from the "Members" table with the SelectCommand, the other columns I'm creating them with addCol. I kinda can figure out how to add data to the database via cellEdit, but when I load the sheet, I dont know how to put the data from the database in the grid besides for the ones coming from the Members table. I hope this is clearer! thanks!

I am assuming you have never used jqGrid and you need to get started...
Please have a look at this link, it gives you demos with code for everything you need to know on how to create your grid using PHP.
http://www.trirand.net/demophp.aspx

Related

Laravel Crud Editing makes a new record

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
I want a check before it makes a new ExtraRegistration. that it only makes a new registration if the registration_id with that extra_id doesn't already exists. (Not 100% sure though).
Thanks in advance!
you make a new object of ExtraRegistration so its always make a new entry for update first get object of those id after that update
check the below link
https://laravel.com/docs/5.8/eloquent#updates
This happens because you're creating a new ExtraRegistration record:
$registrationextra = new ExtraRegistration();
If you want to update it, you need to find the related $registrationextra for your $options, and then update them (assuming you have relations set up):
$registrationextra = ExtraRegistration::where('options_id', $option->id);
$registrationextra->update([
'your_fields' => value
// etc...
]);
If you want to check if ExtraRegistration exists, and depending on that, create or update it, you can do something like this:
$registrationextra = App\ExtraRegistration::updateOrCreate(
['your_fields' => 'value'],
);
You can read more on official documentation.

CakePHP how to add data from another field of a related model

This is one of my first applications out of tutorials so I don't know how to express my issue well.
Well I have these 2 tables:
User ( id, code )
Hours ( id, user_id, created)
I want to know how I can add an entry to the Hours table using the user_code.
I tried to grab the data of the User table with the code value and then findBy and pass for the patchEntity but it did not work.
I don't have a whole lot of information to work with, but I'll give it a go.
I want to know how I can add an entry to the Hours table using the
user_code
You mention using patchEntity, so that's updating information that's already there. Assuming user_code is the 'code' column you're talking about there, first find the user by his code:
$users_tbl = TableRegistry::get('Users');
// find the user
$user = $users_tbl->findByCode($user_code)->first();
if ($user) {
// replace '$this->request->data() with whatever patch data you wanted
$users_tbl->patchEntity($user, $this->request->data(), [
'associated' => ['Hours']
]
if ($users_tbl->save($user)) {
// success!
} else {
// error!
}
} else {
// error!
}
It will also depend on how you have the data you passed in (where my '$this->request->data() is, or whatever your array might be) - it needs to match the right column names and be in the correct format listed here.
However, this is updating the data. Just adding the data, you can load the hours table and add a new entry with the user_id acquired from the user search:
$hours_tbl = TableRegistry::get('Hours');
$hours = $hours_tbl->newEntity([
'user_id' => $user->id // $user populated from same method earlier
]);
/* assumed 'id' was autoincrementing and 'created' was populated
through Timestamp behavior */
if ($hours_tbl->save($hours)) {
// yay!
} else {
// boo
}

Date field's rows are not shown in the grocery crud table

I am new to Grocery CRUD.
I am impressed by how much time saver this library is and i want to thank all great developers who worked on this project.
i have a small problem with showing the date field in the table. when i press the edit button , it's shown in the edit and view pages.
but it doesn't appear in the table.
Even if i create the record myself from the add record button, it's saved successfully but not shown in the table.
i have checked many things like the default format of the date in the library.
$config['grocery_crud_date_format'] = 'sql-date';
I tried different web browsers
this is my table and how i update the date and save it to the DB:
$datestring = "%Y-%m-%d";
$time = time();
$data = array(
'Attendence_date_daily' => mdate($datestring, $time),
'Check_in_time' => null,
'Check_out_time' => null,
'Attendence_status' => null,
'Employee_comment' =>null,
'Deducted_today' => 0,
'user_id' => $row->id
);
this is how i created the table
public function edit_daily_record()
{
$crud = new grocery_CRUD();
$crud->columns('daily_record_id','Attendance_date_daily','Check_in_time','Check_out_time','Attendence_status','Employee_comment','Deducted_Today','user_id');
$crud->set_table('daily_attendence_record');
$crud->display_as('Attendance_date_daily','Date')
->display_as('user_id','Employee');
$crud->set_subject('daily record');
$crud->set_relation('user_id','users','username');
$output = $crud->render();
$this->_example_output($output);
}
where 'Attendance_date_daily' is of type date in mysql DB . All fields are shown correctly except this date
'daily_record_id' is auto increment PK
'user_id' is a FK
can you please help me with this problem?
image 1
image 2
I solved this with the help of Mr. Paul Savostin
one of the Advanced members in Grocery CRUD Advance members.
the issue is a simple typo.
the name of the filed in the database is different than the controller by one letter.
this shows how can a letter change the whole code!

how to use Native sessions and MySQL to join information

I have never used sessions before and I am trying to figure out the best way to handle this. I basically am trying to do:
1 step selecting a service
2 step selecting a time
3 step review and book
I can get it to work with no problems using mysql. What I would usually do is save the information into the database after each step and by the time I get to the review part I would have all the information saved and was OK.
However I don't think this is the best way to approach this and might cause problems down the road (what if they stopped at step 2 blah blah)
I decided to try the Laravel 4 sessions and it was super easy to save the session and move on to the next step. However, when I get to the final step I need to join mysql tables to fully show the information about their booking. Can I use the session information to join the information? Can I use the Sessions Database to save this information? Or use different tables?
My controller that POST after reviewing the information:
public function getReview() {
//sets id of user
$user = User::find(Auth::user()->id);
//gets the time and date that they booked from #getSchedule
$scheduler = Session::get('schedule');
//formats time to put in db
$date = strtotime($scheduler['date']);
//same thing as the line above
$dateFormat = date('Y-m-d',$date);
//model to save the schedule
$schedule = new Schedule();
$schedule->userID = $user->id;
$schedule->date = $dateFormat;
$schedule->block = $scheduler['timeslot'];
$schedule->status = "1";
$schedule->save();
//gets the services the user picked from #getServices
$service = Session::get('service');
//saves the services as individual rows in db table
foreach($service as $services)
{
if(!empty($services)) {
$service = new Service();
$service->userID = $user->id;
$service->services = $services;
$service->save();
}
}
return Redirect::to('dashboard');
}
This is the GET review page (where I am having the issues with all the JOINS)
public function showReview() {
$user = User::find(Auth::user()->id);
//show the information and confirm that they want all this crap...if they do..save it and return them to their dashboard
$time = DB::table('bk_schedule')
->leftJoin('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
->where('bk_schedule.id', Auth::user()->id)->first();
$date = strtotime($time->date);
$dateFormat = date('D: F d, Y',$date);
$service = Session::get('service');
$serviceSummary = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->get();
$total = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->sum('pr_service.price');
return View::make('book.review', array('pageTitle' => 'Booking Appointment Review and Finalize', 'service' => $service, 'date' => $dateFormat,
'time' => $time, 'email' => $user->email, 'serviceSummary' => $serviceSummary, 'total' => $total));
}
Is it possible to save the information at the GET and delete it if they don't submit to POST? Could I maybe use my session data to and use the MySQL queries I have?
You don't understand what the session is, with you approach users will not be able to fill several forms (open in several tabs) simultaneously.
So, general way to do this is:
First page shows just HTML code with some fields
User selects them and POST data back to server
Server validates data and open ANOTHER HTML page with new fields AND adds several "hidden" field with values selected in step 1 (of course server can present page1 with error messages)
Users posts this form, server can open THIRD form where new visible fields and ALL previous fields are stored in hidden inputs
Finally user posts form to last page, where you have all data from all previous pages.
Just to notice: another approach is to store "temporary" data in session, for this you will need to obtain some generated ID on 2nd step and pass it through pages as described before

set column names my own in jqgrid

I would like to add my own column names in jqgrid and also i want to prevent the column names that is automatically added by jqgrid according to sql query.
I am using this code to do that, but its also getting the name of columns which i have not declare in the method $grid->setColModel(null, null, $mylabels);
Can anyone please tell me what code i should to write for removing extra added column in jqgrid.
require_once '/var/www/html/zbajtmp/public/jqgrid/jq-config.php';
// include the jqGrid Class
require_once "/var/www/html/zbajtmp/public/jqgrid/php/jqGrid.php";
// include the driver class
require_once "/var/www/html/zbajtmp/public/jqgrid/php/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
//$grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders';
$grid->SelectCommand = 'SELECT * FROM clinic';
// set the ouput format to json
$grid->dataType = 'json';
// Let the grid create the model
//$grid->setColModel();
$mylabels = array(
"clinic_name"=>"Clinic ame",
"clinic_address"=>"Address",
"HomePhone"=>"Home Phone",
"WorkPhone"=>"Work Phone",
"Email_Id"=>"Email",
);
// Let the grid create the model with the desired labels
$grid->setColModel(null, null, $mylabels);
// Set the url from where we obtain the data
//$grid->setUrl('/var/www/html/zbajtmp/application/views/scripts/clinic/grid.php');
$grid->setUrl('http://sunlinux/zbajtmp/application/views/scripts/clinic/grid.php');
// Set grid caption using the option caption
$grid->setGridOptions(array(
"caption"=>"This is my custom Caption...",
"rowNum"=>50,
"sortname"=>"id",
"hoverrows"=>true,
"rowList"=>array(20,50,100,1000),
"width"=>"100%",
"height"=>350,
"footerrow"=>true,
"rownumbers"=>true,
"multiselect"=>true,
"altRows"=>true,
"altclass"=>'clsalt',
"loadtext"=>"<div class='loadingbox'>Please wait. Loading...</div>",
));
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
Thanks a lot.
You need to explicitly select the columns you want - SELECT * wont work
So you need to change :
$grid->SelectCommand = 'SELECT * FROM clinic';
to
$grid->SelectCommand = 'SELECT clinic_name,clinic_address,HomePhone,WorkPhone,Email_Id FROM clinic';

Categories