I am trying to update each contract I have for a particular job entry. My condition is for every job I post, more than one applicant can be hired. Right now, this is what my code shows.
Job ID | Job Title | Provider | Contract Status | Provider's Feedback | Note to Self
1 | First job.| Provider1| DONE | |I like his work.
1 | First job.| Provider2| DONE | |I like his work.
This is supposedly correct but just until PROVIDER column. Whenever I click job title entry (I have it linked to another page where I can update the contract), I should be able to write something about the worker - if the job was done, how his performance was (which should be view-able on his equivalent page), and my note to self about him. Problem is, whenever I click one of the contracts and make an update, all the contracts will be affected. How will I make it unique?
Here's my code so far:
public function update_job_contract($post_obj)
{
$id = $post_obj['id'];
$data = array
(
'client_feedback' => $post_obj['client_feedback'],
'client_notetoself' => $post_obj['client_notetoself'],
'contract_status' => $post_obj['contract_status'],
'client_id' => $this->auth_model->get_user_id()
);
$this->db->where('id', $id);
$this->db->update('job', $data);
}
in my controller:
public function update_job_contract()
{
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->uri->segment(3,0);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/update_job_contract', $data);
}
public function update_job_contract_submit()
{
$this->validateRole('client');
$this->load->model('job_model');
//if ( '0' == $_POST['id'] ) {
$this->job_model->update_job_contract($_POST);
//}
redirect('client/manage_job_contracts?message=Congratulations!');
}
I am looking forward to getting help! Thanks!
Related
So I'm working on this/these problem(s), and there's just a lot going on for recently using codeigniter..
So, I have these specific tables:
sect_tbl ----------- sub_tbl
sect_id ----------- sub_code
2 --------------------ST20
3 --------------------AS13
3 --------------------DA11
1 --------------------PE40
2 --------------------SE10
3 --------------------PE20
4 --------------------RT40
I made sect_id the foreign key in sub_tbl.
the display - a side menu:
link of sect_ids:
1
2
3
4
I want to make it so, that if I click the link of sect_id 3, I'll be redirected to a new page with its corresponding set of sub_code -- "AS13, DA11, PE20" as seen in the table above.
Expected outcome is something like this:
sub_code
(in sect_id-3)
| AS13 |
| DA11 |
| PE20 |
Model: (most definitely not right )
function fetch_view($data){
$query = $this->db->query("SELECT a.sub_code, a.sub_name, a.sect_id,
b.block_name, b.year_level FROM subject as a JOIN section as b
WHERE a.sect_id = b.sect_id ");
return $query->result();
}
Controller:
$data["subjects"] = $this->view_model->fetch_view($this->session-
userdata('user_id'));
view: (this lacking since I cant get around much further with foreach loop yet )
<?php
foreach ($subjects as $row) {
?>
<?php echo $row->year_level.$row->block_name; ?>
<?php
}
?>
-> I tried working my way with href to redirect me and then be able to view it dynamically but it backfired. Thanks a lot to those who could spare their time to share solutions.
If you say you want all sub codes where sect ID = 3
$sect_id = 3;
I simplified your query, but the main problem was that you had no use of "on x = y" for your join
$query = $this->db->query("
SELECT
sec.year_level,
sec.block_name,
sub.sub_code,
FROM section as sec
JOIN subject as sub
ON sec.sect_id = sub.sect_id
WHERE sec.sect_id = ?
", [ $sect_id ] );
if( $query->num_rows() > 0 )
return $query->result();
return [];
Then, because I don't have your actual table schemas, I couldn't suggest if your view is correct, but in general, something like this should work:
$this->load->helper('url');
foreach( $subjects as $row )
{
echo anchor( 'subs/show/' . $row->sub_code, $row->year_level . $row->block_name );
}
Also, as a side note, try to always follow a code style with proper indentation. It makes things that are wrong stand out and easier to fix.
I have get problem on this case, I don't know how to prevent multiple value in 3 column, I mean if the 3 column have multiple value it will not insert to database,for example I have Krs table where has data like this :
| id | nim | nip | kode_mk
| 1 | 134 | 154 | 543
and laravel will ignore this data when someone insert data like this :
nim=134,nip=154,kode_mk=543 laravel will not insert and give an
atttention
but laravel will accept if data like this :
nim=1132,nip=154,kode_mk=543 laravel will accept and save to database
nim=134,nip=1984,kode_mk=543 laravel will accept and save to database
nim=1345,nip=154,kode_mk=543 laravel will accept and save to database
I have a table like this where I want prevent the data in the black circle:
this my Krs.php :
protected $fillable = ['nim','nip','kode_mk','absen','uts','uas'];
protected $table = 'krs';
public function mahasiswas(){
return $this->belongsToMany('App\Mahasiswa','id');
}
public function dosens(){
return $this->belongsToMany('App\Dosen','id');
}
public function makuls(){
return $this->belongsToMany('App\Matakuliah','id');
}
this my KrsController.php :
public function store(KrsRequest $request)
{
$krs = new Krs([
'nim' => $request->get('nim'),
'nip' => $request->get('nip'),
'kode_mk' => $request->get('kode_mk'),
'absen' => $request->get('absen'),
'uts' => $request->get('uts'),
'uas' => $request->get('uas')
]);
if ($krs->save()) {
session()->flash('status','done_all');
session()->flash('pesan','Data berhasil disimpan');
}else{
session()->flash('status','clear');
session()->flash('pesan','Data gagal disimpan');
}
return redirect('Akademik/Krs/create');
}
I don't know how to explain it in english , sorry for my bad grammar
EDIT 1 Table Structure
In your Krs migration, set the field to require the value to be unique. For example:
$table->string('kode_mk')->unique();
I have a table like this
month | rate | admin_id
0 | 1000 | 1
I am storing data with following method:
public function store(Request $request)
{
foreach ($request->admin_id as $key => $val){
$adminbillings = AdminBilling::create([
'month' => 0,
'rate' => $request->rate[$key],
'admin_id' => $val,
]);
}
return redirect('/');
}
When I add a new row in the table using this method, I also want to replace the month of previous row (which is 0) with the timestamp of the new line. So the table will become
month | rate | admin_id
29-08-2017 | 1000 | 1
0 | 2000 | 1
I can add multiple lines from single method, but here one is store and one is update, and by default update requires two arguments. But may be I am mistaken somewhere. Can you please guide me how to achieve my target?
I can access the previous row inside the method using this line:
$ended_at = AdminBilling::where('admin_id', $val)->where('month', 0)->get();
This is not best solution but you will achieve your target.
To update previous record after insertion , you can do like this -
$res = AdminBilling::select('id')->orderBy('id','desc')->skip(1)->take(1)->first(); //to get prev. row
AdminBilling::where('id',$res->id)->update(['month',date()]); // to update month
put these lines in your store function as -
public function store(Request $request)
{
foreach ($request->admin_id as $key => $val){
$adminbillings = AdminBilling::create([
'month' => 0,
'rate' => $request->rate[$key],
'admin_id' => $val,
]);
$res = AdminBilling::select('id')->orderBy('id','desc')->skip(1)->take(1)->first();
AdminBilling::where('id',$res->id)->update(['month',date()]);
}
return redirect('/');
}
Hope this will helpful for you.
When you use create method it returns the instance of your inserted row. So you can use $adminbillings->id if you need the access to previously insterted row.
I have a Document Model which represents a Document (name, description etc). I then have a DocumentData Model which represents the data of a Document. A Document has One to Many DocumentData,
So to create a Document I have a form. Lets say that my form has a text input for Document Owner. Within my DocumentData table, the label documentOwner is set as the key and the inputted data is set as the value. So with multiple form elements, my DocumentData table might look like this
document_data
id | documentId | key | value |
----------------------------------------------
1 | 1 | clientName | Google |
----------------------------------------------
2 | 1 | projectName | Analytics |
----------------------------------------------
3 | 1 | Contact | Mr Sharp |
----------------------------------------------
4 | 1 | startDate | 29/12/2016 |
----------------------------------------------
The Document name and description for the Document table is created using hidden fields within the view of the Document.
So I can create Documents without a problem. I am having a problem with my update function though. So far I have the following
public function update(Request $request, Project $project, $id)
{
$document = $project->document()->where('id', '=', $id)->first();
$docData = $document->documentData()->get();
$input = $request->all();
foreach($docData as $orgData) {
foreach($input as $key => $value) {
if($key !== "filePath" && $key !== "documentType" && $key !== "documentTypeDesc") {
if($key == $orgData->key) {
$orgData->value = $value;
$orgData->update();
}
}
}
}
return View::make($document->name.'Doc.edit', compact('project', 'document'));
}
So firstly I get the Document I am working on and store it in $document. I then get the DocumentData for this Document and store it in $docData. $docData is a collection containing my key-value pairings for a Document.
I then loop both the $docData and the inputted data and where the keys match, I reset its updated value.
However, at the moment, it updates everything to the last inputted data field. I am not sure where else I can perform the update operation, but I only need it to update the row it is referring too, not the whole thing.
How could I go about doing this?
Thanks
I've cleaned up the code a little, and made a few implementation changes. This code should work, so let us know if it doesn't, and if not, what failed.
public function update(Request $request, Project $project, $id)
{
// you can use eager loading and find on the relationship query
$document = $project->document()->with('documentData')->find($id);
// you can just access the relationship attribute, no need for the query
$docData = $document->documentData;
// gets all input, except the keys specified
$input = $request->except(['filePath', 'documentType', 'documentTypeDesc']);
foreach($docData as $orgData) {
// check if the input has a key that matches the docdata key
if (array_key_exists($orgData->key, $input)) {
// update the value
$orgData->value = $input[$orgData->key];
// use save, not update
$orgData->save();
}
}
return View::make($document->name.'Doc.edit', compact('project', 'document'));
}
What is the best way in PHP to do variable caching? For example, let's assume I have a table, with 4 rows.
name | job
--------------------------
Justin Smith | Plumber
Jack Sparrow | Carpenter
Justin Smith | Plumber
Katie White | Doctor
Which is built like:
foreach($people as $person) {
echo $person->name;
echo get_job($person->name);
}
And the function call get_job() looks like:
function get_job($name) {
//This is pseudo code below
$row = MySQL->Query("SELECT job FROM people WHERE name = $name");
return $row->job;
}
As you can see, once we get the job of Justin Smith, further down we shouldn't and don't need to do a full MySQL query again, since we know it is Plumber.
I was thinking of doing a global variable which is a key=>value array like:
global $jobs = array("Justin Smith" => "Plumber",
"Jack Sparrow" => "Carpenter",
"Katie White" => "Doctor");
Then in the get_job() function I simply just check if the name exists in the array before querying. If not, insert the name and job into the array and return the job.
Basically, is there a better way to do this that is more elegant?
There is many possible solutions. You can store the SQL result in an array that you can use on multipe places on the page. Instead of global you should use static:
function get_job($name)
{
static $people_jobs;
if( !isset($people_jobs[$name]) || empty($people_jobs[$name]) )
{
$row = MySQL->Query("SELECT job FROM people WHERE name = $name");
$people_jobs[$name] = $row->job;
}
return $people_jobs[$name];
}
This function will do a MySQL query only once for a person, no matter how many time you call get_job($name);
this is the typical n + 1 problem where you make 1 query to get the list of "person" and then you make one query for each person to get the "job".
Depending how they are related.. maybe you could get both using one single query. For example: if the relation is a Nx1 (1 person has 1 Job, and 1 job can be used for N Persons) then your initial query should be something like:
SELECT p.name, j.job FROM Person p INNER JOIN Job j ON (p.job_id = j.id)
If the relation is a NxN, it gets tricky :P,
Hope this helps