I have this relation CompanyhasMany Branch
And using $this->Company->find('all') output this:
(int) 1 => array(
'Company' => array(
'id' => '4',
'nome' => 'Somov',
'diretores' => 'Marcelo, Carl'
),
'Branch' => array(
(int) 0 => array(
'id' => '3',
'nome' => 'Serra',
'rua' => 'Rua teste 2 exttttt',
'numero' => '22',
'estado' => 'ES',
'cidade' => 'Etc',
'cep' => '',
'responsavel' => '',
'company_id' => '4',
'cnpj' => ''
)
)
),
(int) 2 => array(
'Company' => array(
'id' => '5',
'nome' => 'Soimpex',
'diretores' => ''
),
'Branch' => array()
)
)
I want to transform this in a json like this to use with Highchart:
[{
name: NAME OF COMPANY (nome),
data: NUMBER OF BRANCHS
}, {
name: NAME OF COMPANY (nome),
data: NUMBER OF BRANCHS
}]
How I do this convertion? Thanks
This will return a json object with only one result.
If we use previous example, can be done like this:
$arr = $this->Company->find('all'); // fetch the array
$arr1 = array();
foreach ($arr as $value) {
$tmp = array();
$tmp['name'] = $value['Company']['nome'];
$tmp['data'] = count($value['Branch']);
$arr1[] = $tmp;
}
return json_encode($arr1);
<?php
$sql=mysql_query("select * from Posts limit 20");
$response = array();
$posts = array();
while($row=mysql_fetch_array($sql))
{
$title=$row['title'];
$url=$row['url'];
$posts[] = array('title'=> $title, 'url'=> $url);
}
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
This will generate a file called results.json file where your php file is stored on your online server, taking url and title variables from your MySQL db, you can change the variable names to what you want to fetch.
The whole idea is about like this
$arr=$this->Company->find('all'); // fetch the array
$arr1=array();
foreach ($arr as $value) {
$arr1['name']=$value['Company']['nome'];
//some more manual transform to desire format
}
return json_encode($arr1);
Related
I am trying to store array data but its giving me null value, I dont know what happend. Bellow the responses and my trial code:
Reponse Data:
Array
(
[0] => Array
(
[name] => facebook
)
[1] => Array
(
[url] => facebook
)
[2] => Array
(
[icon] => facebook
)
[3] => Array
(
[name] => linkedin
)
[4] => Array
(
[url] => linkedin
)
[5] => Array
(
[icon] => linkedin
)
)
Controller:
for ($i = 1; $i < count($request->fields); $i++) {
$socialitem[] = [
'name' => $request->fields['name'][$i],
'url' => $request->fields['url'][$i],
'icon' => $request->fields['icon'][$i],
'teams_id' => $team->id,
];
}
TeamsSocial_link::insert($socialitem);
My table:
$table->bigIncrements('id');
$table->string('name');
$table->string('url');
$table->string('icon')->nullable();
$table->bigInteger('teams_id')->unsigned();
I have try above code to store array data but its not working, I also research interet and try that way but still getting error.
it seems to me like you should do something like this
for ($i = 0; $i < count($request->fields); $i++) {
$socialitem = [
'name' => $request->fields[$i]['name'],
'url' => $request->fields[$i]['url'],
'icon' => $request->fields[$i]['icon'],
'teams_id' => $team->id,
];
TeamsSocial_link::insert($socialitem);
}
if you need only one item, no need for the loop
$socialitem = [
'name' => $request->fields[0]['name'],
'url' => $request->fields[1]['url'],
'icon' => $request->fields[2]['icon'],
'teams_id' => $team->id,
];
if you want to have multiple items
for ($i = 0; $i < count($request->fields) / 3; $i = $i+3) {
$socialitem = [
'name' => $request->fields[$i]['name'],
'url' => $request->fields[$i+1]['url'],
'icon' => $request->fields[$i+2]['icon'],
'teams_id' => $team->id,
];
TeamsSocial_link::insert($socialitem);
}
I need to insert an array in a table column with other non array inputs, But each time it's inserting a string "Array" ,
My codes are :
$post_data['product_category'] = "Goods";
$post_data['product_profile'] = "physical-goods";
for ($i = 1; $i < count($request->package_type_id); $i++) {
$answers[] = [
$post_data['package_type_id'] =$request->package_type_id,
];
}
$update_product = DB::table('orders')
->where('transaction_id', $post_data['tran_id'])
->updateOrInsert([
'name' => $post_data['cus_name'],
'email' => $post_data['cus_email'],
'phone' => $post_data['cus_phone'],
'amount' => $post_data['total_amount'],
'status' => 'Pending',
'address' => $post_data['cus_add1'],
'transaction_id' => $post_data['tran_id'],
'currency' => $post_data['currency'],
'package_type_id' => implode($answers,',')
]);
You have gone a long way, I think you just want to make a string from the $request->package_type_id which I assume is an array.
$post_data['product_category'] = "Goods";
$post_data['product_profile'] = "physical-goods";
$post_data['package_type_id'] = implode(',', $request->package_type_id);
$update_product = DB::table('orders')
->where('transaction_id', $post_data['tran_id'])
->updateOrInsert([
// ...
'package_type_id' => $post_data['package_type_id']
]);
Also note that the syntax for implode is implode(glue, array) not the other way around.
i'm already can insert data into database using below query. But i want to shortening code for my a few column name because my it just like a continues number. i already explode mark by m1=A, m2=B, m3=C, m4=A and continue..
Below is my controller:
$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '10';
for($i=0; $i<$totalquestion; $i++):
$no = $i+1;
$m = substr($mark, 0, $no);
endfor;
$data[] = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
'm1' => $m[0],
'm2' => $m[1],
'm3' => $m[2],
'm4' => $m[3],
'm5' => $m[4],
'm6' => $m[5],
'm7' => $m[6],
'm8' => $m[7],
'm9' => $m[8],
'm10' => $m[9],
'm11' => $m[10],
'm12' => $m[11],
'm13' => $m[12],
'm14' => $m[13],
'm15' => $m[14],
'm16' => $m[15],
'm17' => $m[16],
'm18' => $m[17],
'm19' => $m[18],
'm20' => $m[19]
);
$this->excel_import_model->insert($data);
This is my result:
My question is how to re-code for my data[] array so i dont need to type manually m1, m2, m3 till m20. Sorry for my bad english
$mark = 'ABCADDBBAACBCDDABBCA';
$totalquestion = '20';
$data = array();
$data = array(
'TotalQuestion' => $totalquestion,
'Mark' => $mark,
);
//Split String into array
$list = str_split($mark);
$mlist = array();
//Loop through splited string
for($i=0; $i<$totalquestion; $i++):
$mlist['m'.$i] = $list[$i];
endfor;
//Merge here or you an even use push array in for loop itslef
$data = array_merge($data,$mlist);
How can I insert array data to mysql using code igniter ??
I tried example from CI documentation like this
$data = array(
'id_kls' => 'id_kls',
'fk__id_kls' => 'fk__id_kls',
'id_reg_pd' => 'id_reg_pd',
'nm_pd' => 'nm_pd',
'asal_data' => 'asal_data',
'nilai_angka' => 'nilai_angka',
'nilai_huruf' => 'nilai_huruf',
'nilai_indeks' => 'nilai_indeks',
);
$this->db->insert('master_nilai', $data);
// Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
But not working ..
I have array data like this
Array
(
[error_code] => 0
[error_desc] =>
[result] => Array
(
[0] => Array
(
[id_kls] => f77294f7-2a5a-4876-860b-824d227d5b19
[fk__id_kls] => 02
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
[nm_pd] => SUYATNO
[asal_data] => 9
[nilai_angka] =>
[nilai_huruf] => B
[nilai_indeks] => 3.00
)
Make sure you've enable config/autoload.php/$autoload['libraries'] = array('database');
and config/database.php/your hostname,username,dbname!
I have multiple array to insert into database but i don't fix the field name because can select format table data and insert into database but can check field name with $id_template.
This my format table(example)
So i want to know how can i get data from multiple array to insert into database
This my code in controller
$column = $this->m_rate_template->get_column($id_template);
$colum_detail = implode(",", $column);
$column_cut = explode(",", $colum_detail); //example data get format is Array ( [0] => min [1] => max)
foreach ($column_cut as $key => $val){
$a = $this->input->post($column_cut[$key]);
foreach ($a as $key1 => $val1){
echo $val1;
$child_data = array(
'id' => $this->m_rate_template->generate_id_in_template($template_name),
'id_rate' => $id_rate,
$column_cut[$key] => $val1
);
$this->m_rate_template->insert_rate($child_data, $template_name);
}
}
My data it show like this
Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 )
Array ( [id] => bc3e60157f [id_rate] => 7f881e02bb [min] => 2 )
Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 )
Array ( [id] => ee135ecd8a [id_rate] => 7f881e02bb [max] => 2 )
actually, data should be like this
Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 [max] => 2)
Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 [max] => 2)
Update
$array = array(
[0] => array(
'min' => '2500',
'max' => '5000'
),
[1] => array(
'min' => '5001',
'max' => '7000'
)
)
You can use batch insert to insert multiple
$this->db->insert_batch();
first parameter is table name and second is array of arrays(records)
if you have want to insert multiple record in table then you can also use codeigniter inbuilt insert_batch function without make query in loop.
so i thing your execution will be fast.
you have want to array in below format.
$array = array(
[0] => array(
'column 1' => 'value 1',
'column 2' => 'value 1'
),
[1] => array(
'column 1' => 'value 2',
'column 2' => 'value 2'
)
)
$this->db->insert_batch('tbl_name',$array)
so please make your code and generate your array as above in loop and simply pass your array in insert_batch function.