Disable items in dropdown which are previously selected using codeigniter - codeigniter

I have created a dropdown which contains Job Positions. I want to disable the dropdown item which user has previously applied. Here to reduce my code i have created options_selected static to get selected job profile lists.
Here $job_positions contains all job profiles and $options_selected contains all the items which he previously selected from job_positions. Now he can't select these options again these should be disabled.
$job_positions =array('0' => 'Select', '1' => 'IT Staff', '2' => 'Customer Care', '3' => 'Sales', '4' => 'Doctor');
$options_selected =array('1' => 'IT Staff','2' => 'Doctor');
$opt_array=array();
// extract the job position
for ($i=0; $i < count($job_positions); $i++) {
$disabled = '';
// extract the options_selected and compare with the job position and if match overwrite the variable disabled
for ($x=1; $x <= count($options_selected); $x++) {
if ($options_selected[$x]==$job_positions[$i]) {
$disabled = 'disabled';
}
}
$opt_array[]= '<option '.$disabled.' value="'.$job_positions[$i].'">'.$job_positions[$i].'</option>';
}
echo form_dropdown('category', $opt_array);

You can use the array_diff() function which compare the values of two arrays and returns the differences.
$job_positions =array('0' => 'Select', '1' => 'IT Staff', '2' => 'Customer Care', '3' => 'Sales', '4' => 'Doctor');
$options_selected =array('1' => 'IT Staff','2' => 'Doctor');
$position = array_diff($job_positions,$options_selected);
echo form_dropdown('category', $position);

Related

response()->json() in Laravel re-sorts array by key

Let's say you have following data:
$array = ['23' => 'item', '32' => 'another item', '1' => 'More items'];
Using
return response()->json($array);
Will decode data element following:
{
1 : 'More Items'
23 : 'Item'
32 : 'Another Item'
}
To prevent array resorting put it into object's variable as such:
$array = ['23' => 'item', '32' => 'another item', '1' => 'More items'];
$returnData = ['unsorted' => $array];
return response()->json($returnData);
This becomes a problem for orderBy() queries. You can alternatively, add orderBy()->values();

convert select DB::table to array on laravel

i want to change this code
$data = array(
'O' => 'Orange',
'Y' => 'Yellow',
'G' => 'Green',
'B' => 'Blue',
'I' => 'Indigo',
'V' => 'Violet',
);
with this code
$d = DB::table('sps')
->select(array('sps.namasp'))
->where('namasp','like',$term)
->get();
Here is my full code on route
Route::get('getdata', function()
{
$term = Input::get('term');
$data = array(
'SPION DEPAN' => 'Spion Depan',
'SPION TENGAH' => 'Spion Tengah',
'O' => 'Orange',
'Y' => 'Yellow',
'G' => 'Green',
'B' => 'Blue',
'I' => 'Indigo',
'V' => 'Violet',
);
$return_array = array();
foreach ($data as $k => $v) {
if (strpos($v, $term) !== FALSE) {
$return_array[] = array('value' => $v, 'id' =>$k);
}
}
return Response::json($return_array);
});
basicly i try to find code for autocomplete on my blade. and i stack here.
if you have any references for search autocomplete on laravel 5.1, give me the example or link. thanks before :)
You can get array using lists method
$d = DB::table('sps')
->select(array('sps.namasp'))
->where('namasp','like',$term)
->lists("<< value >>","<< key >>");
This query is return an array.
Note:-
<< value >> replace to your table field that make value of array.
<< key >> replace to your table filed that make key of array

Update multiple Rows in Codeigniter

I have been looking around but I have not found an answer yet. Kindly help if you know the answer.
How do you update multiple rows in CI?
In my MySQL:
I have column names:
ID, Settings Name, Settings Value ( Settings Name is Unique )
I have the ff Data:
ID = 1, Settings Name = "Hello" , Settings Value = "True"
ID = 2, Settings Name = "World", Settings Value = "Good"
and more ...
I also have a form that gets the Settings Value but I am not sure how to update it on the DB. How to update the True for the Hello being the Settings Name and update the Good for the World.
I heard about insert_batch() but is there an update_batch()?
Are you using Active record?
Yes there is an update batch: $this->db->update_batch();
$data = array(
array(
'ID' => 1 ,
'Settings Name' => 'Hello' ,
'Settings Value' => 'True'
),
array(
'ID' => '2' ,
'Settings Name' => 'World' ,
'Settings Value' => 'Good'
)
);
$this->db->update_batch('mytable', $data, 'where_key');
From the documentation:
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
There is indeed an update_batch() method available in CodeIgniter already.
You can use it your example like so:
$data = array(
array(
'ID' => 1,
'Settings Name' => 'Hello',
'Settings Value' => 'True'
),
array(
'ID' => 2,
'Settings Name' => 'World',
'Settings Value' => 'Good'
)
);
$this->db->update_batch('tableName', $data, 'id');
So what you have is an array of arrays, the children basically hold the data for each row in the database. The first parameter for update_batch() is the name of the database table, the second is the $data variable and the third is the column you want to use in the WHEN clause.
Here is a simple php code for perform this operations.
<?php
function basic_update($uwi='') {
$this->load->database();
$data = array(
'ID' => 1,
'Settings Name' => 'Hello',
'Settings Value' => 'True'
);
$this->db->where('ID', '1');
$this->db->update('<table name>', $data);
$data1 = array(
'ID' => 2,
'Settings Name' => 'World',
'Settings Value' => 'Good'
);
$this->db->where('ID', '2');
$this->db->update('<table name>', $data1);
}
In $this->db->where('ID', '1'); ID is your table field and 1 is the value.
In array first parameter will contain the table name, the second parameter is an associative array of values

Cakephp: Validation of elements in an array

I have a form where a student to enter subjects using a drop down list. The dropdown list takes subjects from a table. I want to validate this dropdown so that a subject is only selected once by the student. The subjects are being looped. How can i do this?
My controller
for ($i = 1; $i < sizeof($this->data['ApplicantOlevelQualification']['olevel_subject_code']); $i++) {
if ($this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i] != "") {
$this->ApplicantOlevelQualification->create();
$this->ApplicantOlevelQualification->id = null;
$this->ApplicantOlevelQualification->set(array(
'applicants_detail_id' => $app_id,
'olevel_subject_code' => $this->data['ApplicantOlevelQualification']['olevel_subject_code'][$i],
'grade' => $this->data['ApplicantOlevelQualification']['grade'][$i],
'mark' => $this->data['ApplicantOlevelQualification']['mark'][$i],
'centre_number'=> $centre_number,
'candidate_number'=> $candidate_number,
'exam_body_code'=> $exam_body_code,
'year_written'=> $year_written,
)
);
My add.ctp
echo "<tr><td>" . $this->Form->label('Subject: ');
echo "</td><td>";
echo $this->Form->select("ApplicantOlevelQualification.olevel_subject_code.$s",$mySubjects);
echo "</td><td>";
Model
'olevel_subject_code' => array(
'numeric' => array(
'rule' => array('valids'),
),
),
This can be unique by adding GROUP BY on dropdown list
$subjects = $this->ApplicantOlevelQualification->OlevelSubject->find('list',array('fields'=>‌​array('code','name'),'group'=>array(code)));

How to process an array of results in CodeIgniter

Here is my model. I'm not sure I'm on the right track, but this is a start. I have a survey form on my website and all of the values are numeric in INT fields. I want to compute the average of each of 10 columns. Rather than computing each result separately, I was wondering if there was a way to use a loop to process the results. So here it is:
function survey_averages() {
$q = array(
'1' => 'q1',
'2' => 'q2',
'3' => 'q3',
'4' => 'q4',
'5' => 'q5',
'6' => 'q6',
'7' => 'q7',
'8' => 'q8',
'9' => 'q9',
'10' => 'q10'
);
for ($i=1; $i<11; $i++) {
$this->db->select_avg($q[$i]);
$query[$i] = $this->db->get('survey');
}
return $query;
}
If I'm on the right track, then how to I get back the results of the array? If I'm on the wrong track, what do I need to change? Also, if I do this in my controller:
foreach($query->result_array() as $row) {
How do I return the rows of my array? PHP is not letting me do $row->1 or $row->'1'.
try var_dump($query->result_array())
in place of
$query[$i] = $this->db->get('survey');
try
$query[$i] = $this->db->get('survey')->result_array();
then, to proccess the model's result you should do:
foreach( $query as $result )
foreach( $result as $row ){
// process $row
}

Resources