I'm trying to insert array data using insert_batch in active record as shown in below: Any ideas to prepare the array to insert using insert_batch or any other way?
$detailBill=array(
'TraineeID'=>$inputall['ID'],
'wDate'=>$inputall['wDate'],
'ACH'=>$inputall['Hour'],
'CRA'=>$inputall['Retention'],
'Amount'=>$inputall['PaybleAmt'],
'forMonth'=>$inputall['monthid']
);
$this->db->insert_batch("tbl_submit_coursefee", $detailBill);
Output:
Array
(
[TraineeID]=> Array
(
[0]=>3001
[1]=>3002
[2]=>3003
[3]=>3004
[4]=>3005
[5]=>3006
[6]=>3007
[7]=>3008
[8]=>3009
[9]=>3010
[10]=>3011
[11]=>3012
[12]=>3013
)
[wDate]=> Array
(
[0]=>
[1]=>
[2]=>
[3]=>
[4]=>
[5]=>
[6]=>
[7]=>
[8]=>
[9]=>
[10]=>
[11]=>
[12]=>
)
more field here....
)
I am assuming you want to reorganize arrays. Try this:
$a = array(
'TraineeID' => array(
3001,
3002
),
'wDate' => array(
'123',
'234'
),
'Hour' => array(
12,
13
)
);
$keys = array_keys($a);//counting outer array
if(count($keys[0]) > 0)//checking if inner array has values at all
{
$new_a = [];//initializing expecting array
foreach($a[$keys[0]] as $k => $v)
{
for($i = 0; $i < count($keys); $i++)
{
$new_a[$k][$keys[$i]] = $a[$keys[$i]][$k];
}
}
}
//echo '<pre>', var_dump($new_a);
Related
[file_1] => Array
(
[name] => Array
(
[file] => EmailAccounts-Testing-3-customers-120email-accounts.xlsx
)
[type] => Array
(
[file] => application/octet-stream
)
[tmp_name] => Array
(
[file] => D:\wamp\tmp\phpDB76.tmp
)
[error] => Array
(
[file] => 0
)
[size] => Array
(
[file] => 21505
)
)
Let's do this with Recursive:
public function array_print(){
$myArray = array(
'type'=>'Array outside 1',
'name'=>'Array outside 2',
array(
'file_name'=>'second array',
array(
'file_ext'=>'third array',
array('text'=>'Fourth array inside thrid array')
)
)
);
Print "<pre>";
$output=array();
$result=$this->recursive($myArray);
print_r($result);
//Output value has key and value. I used ## as separator
foreach ($result as $value) {
$data = explode('##',$value);
$output[$data[0]] = $data[1];
}
print_r($output);
}
function recursive($array){
global $temp_data;
if(!empty($array)){
foreach($array as $key => $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
return $this->recursive($value);
} else{
$temp_data[]= $key.'##'.$value;
}
}
}
return $temp_data;
}
Using this we can loop through multidimensional array and store it in Global array. $myArray is your array.
I'm new to this...help me please
$data is object
stdClass Object ( [menu_id] => 38 [menu_code] => M062 [menu_name] => BAP (RICE) [price] => Rp 6.364 [total] => 1
and $fields is array, this is not all..
Array ( [0] => Array ( [code] => menu_id [title] => ID [width] => 5 ) [1] => Array ( [code] => menu_code [title] => Kode [width] => 8 ) [2] =>
and this is my function :
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}
i know the bad line is $this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
thanks all
Change your function as below and check if its work or not:
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
$fields = array_filter($fields);
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}
How can I add more than one element to my keyed array?
array_add($myArray, 'key', 'a');
array_add($myArray, 'key-2', 'b');
Is there a better way?
There is no need for any other custom function because in PHP there is a built-in function for this and it's array_merge and you can use it like this:
$myArray = array('one' => 'TheOne', 'two' => 'TheTwo');
$array = array_merge($myArray, array('three' => 'TheThree', 'four' => 'TheFour'));
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
You can also use this:
$myArray1 = array('one' => 'TheOne', 'two' => 'TheTwo');
$myArray2 = array('three' => 'TheThree', 'four' => 'TheFour');;
$array = $myArray1 + $myArray2;
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
I prefer:
$myArray['key'] = 'a';
$myArray['key-2'] = 'b';
But this is not really better, because you're not adding more than one in a single command.
And if you really need to add multiple, you can always create a helper:
function array_add_multiple($array, $items)
{
foreach($items as $key => $value)
{
$array = array_add($items, $key, $value);
}
return $array;
}
And use it as
$array = array_add_multiple($array, ['key' => 'a', 'key-2' => 'b']);
or, if you're not using PHP 5.4:
$array = array_add_multiple($array, array('key' => 'a', 'key-2' => 'b'));
My custom "on the fly" method:
function add_to_array($key_value)
{
$arr = [];
foreach ($key_value as $key => $value) {
$arr[] = [$key=>$value];
}
return $arr;
}
dd(add_to_array(["hello"=>"from this array","and"=>"one more time","what"=>"do you think?"]));
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)));
I am using Codeigniter to parse an uploaded csv file (which is a multi-dimensional array) into a database. I have tried everything to parse the comma values correctly, but the "id" column in mysql comes up short, as it reads "text", and not "text,text,text". Help!?
*For reference:*
print_r($data['csvData']);
Array ( [0] => Array ( [category,id] => text1,"text,text,text" )
[1] => Array ( [category,id] => text2,"text,text,text" )
)
foreach($data['csvData'] as $row) {
foreach ($row as $item) {
$item=explode(",", $item);
$results_array = array(
'category' => $item[0],
'id' => $item[1]
);
$this->db->set($results_array);
$this->db->insert('table', $results_array);
}
}
My uneducated guess:
$item=explode(",", $item); is exploding $item which is text1,"text,text,text", right? So it sees 4 commas, and explodes them. Therefore $item[0] will be "text1, $item[1] will be "text" $item[2] will be "text" and $item[3] will be "text".
You can try to set your delimiter in the csv as something other than a comma, and explode that.
Or you can concatenate the other items before inserting them into the db:
$item = explode(",", $item);
$id_insert = $item[1].$item[2].$item[3];
//if you need to retain the commas in the id:
//$id_insert = $item[1].','.$item[2].','.$item[3];
$results_array = array(
'category' => $item[0],
'id' => $id_insert,
);
$this->db->set($results_array);
$this->db->insert('table', $results_array);