I want a dropdown widget in yii2 with dynamic items. How is it possible?
After doing this i face a error that option is required.
What's wrong in my code?
$items = [];
foreach ($all_label as $each_label) {
$items[$each_label->id_label]['id_label'] = $each_label->id_label;
$items[$each_label->id_label]['label'] = $each_label->label_name;
}
print_r($items);
O:P-
Array
(
[5] => Array
(
[id_label] => 5
[label] => Label1
)
[6] => Array
(
[id_label] => 6
[label] => Label2
)
[9] => Array
(
[id_label] => 9
[label] => Label3
)
)
echo Dropdown::widget([
'items' => [
$items
],
]);
Ok, after checking the spec, it seems the problem is that you use "id_label" which is not a valid option for the widget.
You need to leave this out:
$items = [];
foreach ($all_label as $each_label) {
$items[$each_label->id_label]['label'] = $each_label->label_name;
}
If this does not generate the values as you need them, try to adapt to this:
$items = [];
foreach ($all_label as $each_label) {
$items[$each_label->id_label]['label'] = $each_label->label_name;
$items[$each_label->id_label]['options']['value'] = $each_label->id_label_;
}
http://www.yiiframework.com/doc-2.0/yii-bootstrap-dropdown.html#$items-detail
Related
I want to retrieve some data from 'tbl_karyawan' and input into 'tbl_absen' which is if the NIP exist from 'tbl_karyawan' then parshing some data into 'tbl_absen'. I was create the code, and the data is going well. but i have something trouble with
i want the data input in Nip_kyn be like 'KIP001' not [{"Nip_kyn":"KIP001"}].
this is my Model
public function presensi($data)
{
$isExist = DB::table('tbl_karyawan')
->where('Nip_kyn', $data)->exists();
if ($isExist) {
$namakyn = DB::table('tbl_karyawan')->where($data)->get('Nama_kyn');
$nippppp = DB::table('tbl_karyawan')->where($data)->select('Nip_kyn')->get($data);
$values = array('Nip_kyn' => $nippppp, 'Nama_kyn' => $namakyn, 'Jam_msk' => now(), 'Log_date' => today());
DB::table('tbl_absen')->insert($values);
} else {
echo 'data not available';
}
}
this is my controller
public function get()
{
$day = [
'time_in' => $this->AbsenModel->timeIN(),
'time_out' => $this->AbsenModel->timeOut(),
'break' => $this->AbsenModel->break(),
// absen here
's' => $this->AbsenModel->absensi(),
];
$data = [
'Nip_kyn' => Request()->Nip_kyn,
];
$this->AbsenModel->presensi($data);
return view('v_absen', $data, $day);
}
Yup, I finally got it, the problem is on my model.
$nama_karyawan = DB::table('tbl_karyawan')->where($data)->value('Nama_kyn');
$nipkyn = DB::table('tbl_karyawan')->where($data)->value('Nip_kyn');
I just change 'get' to 'value'.
i have a array data like this below. i want to save some of it's data not all into mysql table.
when i dd($request->all) data in my controller function it looks like this.
array:1 [
"{"cart":" => array:1 [
"{"product":{"id":1,"category":1,"product_name":"Brinjal 500g","product_image":"1585409454.jpeg","product_price":232,"unit":"500g","product_description":null,"stock":"In Stock","product_status":1,"show":1,"delievery_date":null,"vendor":null,"quantity":18,"item_type":"feature_product","created_at":"2020-03-28T15:30:54.000000Z","updated_at":"2020-03-28T15:30:54.000000Z","deleted_at":null,"categories":{"id":1,"category_name":"Fresh Fruits","category_image":null,"category_description":null,"category_status":1,"show":0,"created_at":"2020-03-28T15:30:12.000000Z","updated_at":"2020-03-28T15:30:12.000000Z","deleted_at":null}},"quantity":1}" => null
]
]
i want to add id and total quantity shows last object into a table. table rows looks like this.
$table->increments('id');
$table->integer('order_id');
$table->integer('product_id')->unsigned();
$table->integer('total_quantity');
$table->timestamps();
$table->softDeletes();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
how can i write a store function using this data in laravel
result data looks like this
Array
(
[{"cart":] => Array
(
[{"product":{"id":1,"category":1,"product_name":"Brinjal 500g","product_image":"1585409454.jpeg","product_price":232,"unit":"500g","product_description":null,"stock":"In Stock","product_status":1,"show":1,"delievery_date":null,"vendor":null,"quantity":18,"item_type":"feature_product","created_at":"2020-03-28T15:30:54.000000Z","updated_at":"2020-03-28T15:30:54.000000Z","deleted_at":null,"categories":{"id":1,"category_name":"Fresh Fruits","category_image":null,"category_description":null,"category_status":1,"show":0,"created_at":"2020-03-28T15:30:12.000000Z","updated_at":"2020-03-28T15:30:12.000000Z","deleted_at":null}},"quantity":1}] =>
)
)
this is my latest update
$data = [];
foreach ($request->all() as $key => $value) {
$data[$key]['product_id'] = $value['product']['id'];
$data[$key]['quantity'] = $value['quantity'];
dd($value);
}
Loop through your data coming and make array as below i have shown.
Try this one by creating the array of the data and insert array to the database as bulk data:
// this will decode your json data that you're getting in the post data
$postdata = json_decode($request->all(), true);
$data = [];
foreach ($postdata as $key => $value) {
$data[$key]['product_id'] = $value['product']['id'];
$data[$key]['quantity'] = $value['quantity'];
}
// $data now will have data as following structure
$data = [[
'product_id' => 2,
'total_quantity' => 7
],
[
'product_id' => 2,
'total_quantity' => 7
]];
$order = Order::create($data);
Using Laravel5.1 ...
I'm trying to convert this JSON:
"[{"John Doe":"john.gmail.com"},{"Frank Smith":"frank#frank.com"},{"Jie Brent":"jie#gmail.com"},{"Jeffrey Manney":"jeff17#gmail.com"}]"
To this:
"[{"name":"John Doe", "email":"john.gmail.com"},{"name":"Frank Smith", "email":"frank#frank.com"},{"name":"Jie Brent", "email":"jie#gmail.com"},{"name":"Jeffrey Manney", "email":"jeff17#gmail.com"}]"
This is my code:
$users_storage = [];
foreach($rcf_and_rcfm_users as $key => $user){
$users_storage[][$key] = $user;
}
$users = json_encode($users_storage);
dd($users);
The $rcf_and_rcfm_users variable is a collection of users from the database.
If I understand it correctly.
$users_storage = [];
foreach($rcf_and_rcfm_users as $name => $email){
$users_storage[] = [
'name' => $name,
'email' => $email,
];
}
$users = json_encode($users_storage);
dd($users);
I think this is what you're trying to accomplish.
I am struggling to get the "merchant" -> "id" from the Groupon API below, while I don't have any problems to return the discountPercent.
<?php
$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['deals'] as $results) {
$discountPercent = $results['options'][0]['discountPercent'];
$merchantId = $results['merchant'][0]->id;
echo $discountPercent.'<br>';
echo $merchantId;
}
?>
If someone could point me to the right direction.
Many Thanks,
Ok here is the answer:
$merchantId = $results['merchant']['id'];
>>>This may be the Reason.....
>>>Explanation for:: $results['options'][0]['discountPercent']
In $json array you can see
[options] => Array
(
[0] => Array
(
.........
[discountPercent] => 54
.........
)
)
Here, the hierechy is
option->0(key)->discountPercent
that's why you need to use index '0';
>>>Explanation for::: $results['merchant'][0]->id
in json array you can see
[merchant] => Array
(
[id] => global-cuisine-restaurant
[uuid] => 183dd76b-a1a6-40cf-93c7-33e00f379451
[name] => Global Cuisine Restaurant
[websiteUrl] => http://www.globalcuisine.ie/
[twitterUrl] =>
[facebookUrl] =>
[ratings] =>
)
Here the hirerchy is:::
merchant->id (notice '0' is not present as any subarray index)
that's why should use
$merchantId = $results['merchant']['id'];
finally use can use code::
<?php
$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['deals'] as $key=>$results) {
$discountPercent = $results['options'][0]['discountPercent'];
$discountPercent[] = $results['options'][0]['discountPercent'];//to get all in one;
$merchantId = $results['merchant']['id'];
$merchantId[] = $results['merchant']['id'];//to get all in one
echo $discountPercent.'<br>';
echo $merchantId;
}
?>
I'm on this trouble since a week from now. I've read a lot of topics here on stackoverflow and on various blogs that I googled but I still need help from you on solving this issue. For many of you this is a dumb question, so I apologize since now for my not so big knowledge of php/javascript languages but I'm in a learning phase and I'm sure you will support me during this formation path.
My main problem is to convert a json_encode output array into something that I can use into Flot that works in javascript.
My current PHP code is the following:
$csv_is_a = str_getcsv(file_get_contents('csv/IS_A.csv'));
$csv_is_q = str_getcsv(file_get_contents('csv/IS_Q.csv'));
for ($i=1, $k=12; $i<12, $k<22; $i++, $k++) {
$JsonArray[] = array( $csv_is_a[$i] => $csv_is_a[$k]);
}
echo '<pre>';
print_r( $JsonArray );
echo '</pre>';
The output of this code is:
Array (
[0] => Array
(
[2005-09] => 13931
)
[1] => Array
(
[2006-09] => 19315
)
[2] => Array
(
[2007-09] => 24006
)
[3] => Array
(
[2008-09] => 32479
)
[4] => Array
(
[2009-09] => 42905
)
[5] => Array
(
[2010-09] => 65225
)
[6] => Array
(
[2011-09] => 108249
)
[7] => Array
(
[2012-09] => 156508
)
[8] => Array
(
[2013-09] => 170910
)
[9] => Array
(
[2014-09] => 182795
)
)
Now I apply the json_encode:
$csv_enc = json_encode( $JsonArray );
The output of this is:
[{"2005-09":"13931"},{"2006-09":"19315"},{"2007-09":"24006"},{"2008-09":"32479"},{"2009-09":"42905"},{"2010-09":"65225"},{"2011-09":"108249"},{"2012-09":"156508"},{"2013-09":"170910"},{"2014-09":"182795"}]
But what I want is this:
[[2005-09, 13931],[2006-09, 19315],[2007-09, 24006],[2008-09, 32479],[2009-09, 42905],[2010-09, 65225],[2011-09, 108249],[2012-09, 156508],[2013-09, 170910],[2014-09, 182795]]
In the javascript section of the code I tried to use the JSON.parse() in this way:
<script type="text/javascript">
var data = JSON.parse(<?php echo $csv_enc ;?>);
$(document).ready(function() {
$.plot($("#placeholder"), [data]);
});
</script>
but it doesn't work. If I substitute the "JSON.parse()" with a bidimensional object, the script works without problems. So, the trouble should be just in that part of the code. What's the issue in your opinions?
Thanks for your support.
UPDATE:
I've used the code suggested by #callback, but it doesn't work. Here it is:
<script type="text/javascript">
var theArray = <?php echo $csv_enc; ?>, multiArray = [];
for(var i = 0 ; i< theArray.length ; i++){
var obj = theArray[i];
for( k in obj) {
multiArray[i] = [k, parseInt(obj[k])];
}
}
console.log(multiArray)
$(document).ready(function() {
$.plot($("#placeholder"), [multiArray]);
});
</script>
The console.log doesn't print anything...
Considering that theArray is your input array (the array of objects) and multiArray is the array of arrays that you need, you can do something like this:
var theArray = [{"2005-09":"13931"},{"2006-09":"19315"},{"2007-09":"24006"},{"2008-09":"32479"},{"2009-09":"42905"},{"2010-09":"65225"},{"2011-09":"108249"},{"2012-09":"156508"},{"2013-09":"170910"},{"2014-09":"182795"}], multiArray = [];
for(var i = 0 ; i< theArray.length ; i++){
var obj = theArray[i];
for( k in obj) {
multiArray[i] = [k, parseInt(obj[k])];
}
}
console.log(multiArray)
This will print your array as follows (in the console):
[["2005-09", 13931],["2006-09", 19315],["2007-09", 24006],["2008-09", 32479], ...]
http://jsfiddle.net/1L344srv/
I dont know if Flot cares about the quotes in the first values of each array in your multiArray but since it is a string anyways, it should be fine.