How to display array data in Yii2 GridView using ActiveDataprovider - php

I have an array of data very similar to this
$array = [
'FUNDSCTR' => '10000001'
'RCMMTITEM' => 'R400001'
'YEAR' => '2018'
'CONSUMA' => 5898257
'CONSUME' => 30140
'AVAIL' => 5868117 ]
and use in ArrayDataProvider
$dataProvider = new ArrayDataProvider(['allModels' => $array,]);
code in gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'label' => 'Year',
'attribute'=>'YEAR',
],
[
'label' => 'Fund',
'attribute'=>'FUNDSCTR',
],
[
'label' => 'Item',
'attribute'=>'RCMMTITEM',
],
[
'label' => 'Consumeable',
'attribute'=>'CONSUMA',
],
[
'label' => 'Consumed',
'attribute'=>'CONSUME',
],
[
'attribute'=>'Available',
'value'=> 'AVAIL',
],
]);
?>
</div>
resluts
print_r($dataProvider); ~ $arrays return
yii\data\ArrayDataProvider Object ( [key] => [allModels] => Array ( [RFUNDSCTR] => 10000001 [RCMMTITEM] => R400013 [RYEAR] => 2018 [CONSUMA] => 5898257 [CONSUME] => 30140 [AVAIL] => 5868117 )
I need to show in gridView data like this.

To work properly, an array which you pass to ArrayDataProvider should be two-dimensional, like this:
$array = [
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
],
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
],
[
'FUNDSCTR' => '10000001',
'RCMMTITEM' => 'R400001',
'YEAR' => '2018',
'CONSUMA' => 5898257,
'CONSUME' => 30140,
'AVAIL' => 5868117,
]
];

Related

Extend an assiociative Array with variable

can someone help me, how I can extend an assiociative array with a variable?
I have a loop (foreach):
foreach($this->getWarehouseListForm() as $wareHouse) {
$wareHouseList[] =
[
"title" => "wareHouse[105]",
"form" => [
"storeId[105]" => [
"type" => "inputText",
"options" => [
"name" => "TSL",
],
],
],
];
}
And I want to extend a object like this:
"sections" => [
[
"title" => 'Schuhe24Assistant.ftpServerTitle',
"description" => 'Schuhe24Assistant.ftpServerDescription',
"form" => [
"ftpServer" => [
'type' => 'text',
'defaultValue' => 'ftp.hisasp2.com',
'options' => [
'name' => 'Schuhe24Assistant.ftpServer',
'required' => true,
]
],
"ftpUser" => [
'type' => 'text',
'defaultValue' => 'username',
'options' => [
'name' => 'Schuhe24Assistant.ftpUser',
'required' => true,
]
],
"ftpPassword" => [
'type' => 'text',
'defaultValue' => 'password',
'options' => [
'name' => 'Schuhe24Assistant.ftpPassword',
#'isPassword' => true,
'required' => true,
],
],
"deleteFiles" => [
'type' => 'toggle',
'defaultValue' => true,
'options' => [
'name' => 'Schuhe24Assistant.deleteFiles',
],
],
],
],
], $wareHouseList
But this code produces nothing (no error output) and the structure check fails in this case. If I remove the variable, the structure check is OK.
Can someone help me out?
Kind regards
Henning
Push your $wareHouseList array into the $sections array in the following ways
$sections['wareHouseList'] = $wareHouseList
It should be working.

List key and value of common occurrences in value

$personas = [
'Franz' => [
'interests' => 'all',
'gender' => 'maskulin'
],
'Sissi' => [
'interests' => 'kitesurfing', 'tennis',
'gender' => 'feminin'
],
'Egon' => [
'interests' => 'kitesurfing', 'cinema';
'gender' => 'maskulin'
],
'Maria' => [
'interests' => 'cinema', 'soccer',
'gender' => 'feminin'
]
];
List the data of persons who have indicated in interests all or Kitesurfing
foreach($personas as $person => $kitesurfing) {
echo .$person.' is '.$gender.', ';
}
echo 'and all '.$personal_number.' '.$kitesurfing.' like!';
expected result
Franz is maskulin, Sissi is feminin, Egon is maskulin, and all 3 kitesurfing like!
<?php
$personas = [
'Franz' => [
'interests' => ['all'],
'gender' => 'maskulin'
],
'Sissi' => [
'interests' => ['kitesurfing', 'tennis'],
'gender' => 'feminin'
],
'Egon' => [
'interests' => ['kitesurfing', 'cinema'],
'gender' => 'maskulin'
],
'Maria' => [
'interests' => ['cinema', 'soccer'],
'gender' => 'feminin'
]
];
$interests = ['all', 'kitesurfing'];
$filtered = array_filter($personas, function($v) use ($interests) {
return array_intersect($interests, $v['interests']);
});
var_export($filtered);
Output:
array (
'Franz' =>
array (
'interests' =>
array (
0 => 'all',
),
'gender' => 'maskulin',
),
'Sissi' =>
array (
'interests' =>
array (
0 => 'kitesurfing',
1 => 'tennis',
),
'gender' => 'feminin',
),
'Egon' =>
array (
'interests' =>
array (
0 => 'kitesurfing',
1 => 'cinema',
),
'gender' => 'maskulin',
),
)

Invalid argument supplied for foreach() when i try to print value

This is my array
[
'field_test1' => [
'field_test2' => [ 'value' => 'Yes' ,'action' => 'visible']
],
'field_test3' => [
'field_test4' => [ 'value' => '2' ,'action' => 'visible']
]
'body' => [
'field_test2' => [ 'value' => 'No', 'action' => 'visible']
'field_test4' => [ 'value' => '1', 'action' => 'visible']
]
]
When i try to loop through each element i am getting error like invalid argument passed for foreach;
My code is
foreach ($myArray as $key => $value) {
echo $key;
}
what should i do??
You forgot the comma after your second array within your array. Before the one with the 'body' key. Try using a decent IDE like PhpStorm, it will highlight the errors in your syntax for you, making the search for common errors easy.
$myArray =
[
'field_test1' => [
'field_test2' => [ 'value' => 'Yes' ,'action' => 'visible']
],
'field_test3' => [
'field_test4' => [ 'value' => '2' ,'action' => 'visible']
],
'body' => [
'field_test2' => [ 'value' => 'No', 'action' => 'visible']
'field_test4' => [ 'value' => '1', 'action' => 'visible']
],
];

How do you define a Cassandra CollectionMap nested in a UDT with duoshuo's PHP client library?

I have a CollectionSet<UDT> where UDT contains a CollectionMap<int,boolean>. I have not been able to find any documentation or example of how to define this when creating a new Cassandra\Type\CollectionSet for inserting into the table. There is a great example with a CollectionList (found here) which is like this:
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean,
// CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
[
'id' => 1,
'name' => 'string',
'active' => true,
'friends' => ['string1', 'string2', 'string3'],
'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
],[
'id' => 2,
'name' => 'string',
'active' => false,
'friends' => ['string4', 'string5', 'string6'],
'drinks' => []
]
], [
[
'type' => Cassandra\Type\Base::UDT,
'definition' => [
'id' => Cassandra\Type\Base::INT,
'name' => Cassandra\Type\Base::VARCHAR,
'active' => Cassandra\Type\Base::BOOLEAN,
'friends' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => Cassandra\Type\Base::VARCHAR
],
'drinks' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => [
'type' => Cassandra\Type\Base::UDT,
'typeMap' => [
'qty' => Cassandra\Type\Base::INT,
'brand' => Cassandra\Type\Base::VARCHAR
]
]
]
]
]
]);
I've tried using the above example with several variations to accommodate the CollectionMap but nothing is working. My last attempt was this
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'value' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
which gives the error Caught exception: Since v0.7, collection types should have \"definition\" directive. I've also tried using 'definition' instead of 'value'. I'm running out of ideas, any help would be greatly appreciated.
Use "definition" instead of "value". I tried this before but apparently I was doing something else wrong because this worked.
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'definition' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])

RBAC in YII2: user->can() in PHPManager

I am trying to configure rbac with phpmanager in my project using the Yii2 advance app version. But \Yii::$app->user->can is not returning the expected.
I wrote the RbacController and executed sucessfully yii rbac/init
That updated common/components/items.php as shown
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => 'create a X',],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX => [ 'type' => 2, 'description' => 'update a X', ],
];
In SignupForm::signup, I added it:
$auth = Yii::$app->authManager;
$roleObj = $auth->getRole('user'); // this role is defined by the RBAC Controller's init action
$auth->assign($roleObj, $user->getId());
assignments.php
return [ 2 => [ 'user', ], ];
I think that 2 corresponds to the user id.
rules.php
return [];
common/main.php
...
'components' => [
...
'authManager' => [
'class' => 'yii\rbac\PhpManager',
'defaultRoles' => ['user','admin'],
'itemFile' => '#common/components/rbac/items.php',
'assignmentFile' => '#common/components/rbac/assignments.php',
'ruleFile' => '#common/components/rbac/rules.php'
],
],
...
When I got that role permissions, it prints:
Array ( [createX] => yii\rbac\Permission Object ( [type] => 2 [name] => createX [description] => create a X [ruleName] => [data] => [createdAt] => 1438601819 [updatedAt] => 1438601819 ) )
So I'm expecting that user doesn't have updating permission, but in the method XController::update
echo \Yii::$app->user->can('updateX');
// returns 1, just the same than \Yii::$app->user->can('createX') returning
Please some help
First items file should be:
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => [ 'create a X', ], ],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX' => [ 'type' => 2, 'description' => ['update a X', ], ],
];
You can verify if the code is fine with var_dump:
var_dump(\Yii::$app->authManager);

Categories