Any idea how to get this out put with queries?
$obj = array(
"attr" => array(
"id" => 1,
"rel" => "drive"
),
"data" => "My Documents",
"children" => array(
array(
"attr" => array(
"id" => 2,
"rel" => "file"
),
"data" => "file1.doc",
"state" => ""
)
)
);
echo json_encode($obj);
i need to loop only below part of above code.
array(
"attr" => array(
"id" => 2,
"rel" => "file"
),
"data" => "file1.doc",
"state" => ""
)
any help would be great.
Thanks
$x = 0;
while($row = mssql_fetch_object($query) )
{
$json[$x] = array (
'lastname' => $row->firstname,
'firstname' => $row->lastname
);
$x++;
}
echo json_encode($json);
You might try compiling and installing the third-party JSON UDFs:
http://www.mysqludf.org/lib_mysqludf_json/index.php
Related
in my view I'm trying to add 'value' function, which shows the field if the id = 0, otherwise it doesn't show it. I'm trying to do it like so:
echo $form->field($model, 'test', [
'options' => [
'class' => $twoColumns . ' required',
'value' => function ($model) {
return $model->testvar == 0 ? 'Test' : null;
}
]
])
I've added function into 'value', but somehow it doesn't work. Could someone explain me why?
I'm getting htmlspecialchars error
Closure here is not supported and there is no reason to use it anyway.
You only need something like:
$model->interest = null;
if ($model->is_interest_variable == 0) {
$model->interest = 'Test';
}
echo $form->field($model, 'interest', [
'addon' => [
'append' => [
'content' => '%',
],
],
'options' => [
'class' => $twoColumns . ' required',
]
])->textInput([
'placeholder' => '0.0000',
'class' => 'form-control amount-4'
]);
I've successfully created a script to post to a microsoft team via the incoming hooks connector however when it posts the message the potentialActions are always hidden behind a see more tag. Is there a way to force this to always be displayed?
Code for reference:
$data = array(
'#type' => "MessageCard",
'#context' => "http://schema.org/extensions",
'themeColor' => "1B2C58",
'summary' => "BOT has closed a sale!",
'sections' => array(
0 => array(
'activityTitle' => $username . " has closed a sale!",
'facts' => array(
0 => array(
'name' => "Customer",
'value' => $accname),
1 => array(
'name' => "Product",
'value' => $interestTitle . " - " . $opTypeArray[$opType] . $extra),
2 => array(
'name' => "Sales Value",
'value' => $value . " (excl. VAT)"),
3 => array(
'name' => "Purchase order",
'value' => $poInput),
),
'markdown' => True
),
),
'potentialAction' => array(
0 => array(
'#type' => "ActionCard",
'name' => "Add a comment",
'inputs' => array(
0 => array(
'#type' => "TextInput",
'id' => "comment",
'title' => "Enter your comment",
'isMultiline' => true),
),
),
1 => array(
'#type' => "OpenUri",
'name' => "View Opportunity on ADM",
'targets' => array(
0 => array(
'os' => "default",
'uri' => "https://...." . $ops),
),
),
2 => array(
'#type' => "OpenUri",
'name' => "View Quotation",
'targets' => array(
0 => array(
'os' => "default",
'uri' => "https://....". $newUrl),
),
),
),
);
We have added the feature to not show "See More" buttons if there are actions in the message. This will be available soon to all our users.
Thanks.
In my model I use DateValidation
['date_birthday', 'date', 'format' => 'd.m.yy', 'min' => '01.01.1900', 'max' => date('d.m.yy'), 'tooSmall'=>'The date is from past. Try another','tooBig' => 'The date is from future. Try another', 'message' => 'Try to input the date'],
In view I call the widget
<?php echo $form->field($modelForm, 'date_birthday')->widget(\kartik\date\DatePicker::classname(), [
'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
'pickerButton' => false,
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'format' => 'dd.mm.yyyy',
'autoclose' => true,
'showMeridian' => true,
'startView' => 2,
'minView' => 2,
]
]) ?>
It checks for min and max dates, but show no error message. I think its because of different date formats in model and view. How to fix it?
If you submit form you will see error messages. According to this issue https://github.com/yiisoft/yii2/issues/7745 yii2 have not client-side validations of date
You can enable ajax validation. Add in create and update action before if statement
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
And add use yii\widgets\ActiveForm; on the top of controller class. In your _form.php file enable ajax for whole form
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
]); ?>
, or for the field only
<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
...
Also, you can add plugin options for limit date with startDate and endDate (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate)
<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
'pickerButton' => false,
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'format' => 'dd.mm.yyyy',
'autoclose' => true,
'showMeridian' => true,
'startView' => 2,
'minView' => 2,
'startDate' => '01.01.1900',
'endDate' => date('d.m.Y'),
]
]) ?>
I have below JSON input -
{"links":{"self":"/some/path"},"data":
[{"type":"some_service","id":"foo","attributes":
{"created":true,"active":true,"suspended":false}},
{"type":"some_service","id":"dummy","attributes":{"created":false}}]}
I am using below code -
use strict;
use warnings;
use JSON::XS;
use Data::Dumper;
my $result = decode_json($input);
print Dumper($result) . "\n";
But i am getting below output -
$VAR1 = {
'data' => [
{
'attributes' => {
'active' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' ),
'created' => $VAR1->{'data'}[0]{'attributes'}{'active'},
'suspended' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' )
},
'id' => 'foo',
'type' => 'some_service'
},
{
'id' => 'dummy',
'attributes' => {
'created' => $VAR1->{'data'}[0]{'attributes'}{'suspended'}
},
'type' => 'some_service'
}
],
'links' => {
'self' => '/some/path'
}
};
Looks like the value in 'created' is $VAR1->{'data'}[0]{'attributes'}{'active'} which does not seems to be accurate and same happens at other places too.
Am i missing somewhere in code or the JSON input has some error?
Kindly provide your suggestions.
The JSON decoder is just "mapping/pointing" the values to previous values that have already been parsed. You can see your first created points to
$VAR1->{'data'}[0]{'attributes'}{'active'},
, the value of which is true, just like active should be. You are looking at the Data::Dumper representation of the hash array.
If you were to retrieve an element from the Perl variable, you would find that it matches up with your original input:
print $result->{"data"}[0]->{"attributes"}->{"created"}; # prints 1
To print the Data::Dumper output without this occurring, simply set this flag in your script:
$Data::Dumper::Deepcopy = 1;
Why do you think it's inaccurate? If we look at the JSON, active and created both have the same value: true. Maybe you'll find it clearer to dump the structure as follows:
use JSON::XS qw( decode_json );
use Data::Dumper qw( );
my $data = decode_json(<<'__EOI__');
{"links":{"self":"/some/path"},"data [{"type":"some_service","id":"foo","attributes": {"created":true,"active":true,"suspended":false}}, {"type":"some_service","id":"dummy","attributes":{"created":false}}]}
__EOI__
print(Data::Dumper->Dump(
[ JSON::XS::true, JSON::XS::false, $data ],
[qw( true false data )],
));
Output:
$true = bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' );
$false = bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' );
$data = {
'data' => [
{
'attributes' => {
'active' => $true,
'created' => $true,
'suspended' => $false
},
'id' => 'foo',
'type' => 'some_service'
},
{
'attributes' => {
'created' => $false
},
'id' => 'dummy',
'type' => 'some_service'
}
],
'links' => {
'self' => '/some/path'
}
};
I need to set initValueText for Select2, which is in loop, like gridview or Tabularform. but I don't know how to set right value for each.
<?= TabularForm::widget([
'dataProvider' => $dataProvider,
'form' => $form,
'actionColumn' => false,
'checkboxColumn' => false,
'attributeDefaults' => [
'type' => TabularForm::INPUT_RAW,
],
'attributes' => [
'test' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => Select2::className(),
'options' => [
'name' => 'test',
'options' => [
'class' => 'test-to-select',
],
'pluginOptions' => [
'allowClear' => true,
'minimumResultsForSearch' => 'Infinity',
'ajax' => [
'url' => Url::to(['/test/get-list']),
'dataType' => 'json',
'data' => new JsExpression('function(term,page) {
return {term : term.term};
}'),
'results' => new JsExpression('function(data,page) {
return {results:data.results};
}'),
'cache' => true
]
],
'initValueText' => 'Selected Text' /// how can I set this in gridview or Tabularform?
],
],
]
]) ?>
Of course this is not working,
'initValueText' => function($model){
retur $model->textValue;
}
Any help would be appreciated.
Into tabularform if you want dynamic initValueText, you can use options closure in this manner:
'test' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => Select2::className(),
'options' => function($model, $key, $index, $widget) {
$initValueText = empty($model['textValue']) ? '' : $model['textValue'];
return [
'name' => 'test',
'options' => [
'class' => 'test-to-select',
],
'initValueText' => $initValueText,
'pluginOptions' => [
'allowClear' => true,
'minimumResultsForSearch' => 'Infinity',
'ajax' => [
'url' => Url::to(['/test/get-list']),
'dataType' => 'json',
'data' => new JsExpression('function(term,page) {
return {term : term.term};
}'),
'results' => new JsExpression('function(data,page) {
return {results:data.results};
}'),
'cache' => true
]
],
];
}
],
If for example attribute for city, so try this..
$cityDesc = empty($model->city) ? '' : City::findOne($model->city)->description;
'initValueText' => $cityDesc, // set the initial display text
For init value first assign value to $model attribute, if you should not assign so this attribute can take value.
Set the data parameter with the array including the option you want to show. For example for cities:
'options' => [
'data' => \yii\helpers\ArrayHelper::map(\app\models\City::find()->orderBy('id')->asArray()->all(), 'id', 'name'),
]
try to put inside the options instead ouside
'widgetClass' => Select2::className(),
'options' => [
'initValueText' => 'Selected Text'
Don't use isset it return error if more the one filter are use there.
[
'attribute' => 'ad_partner_id',
'value' => function ($model, $key, $index, $widget) {
return $model->partner->name;
},
'filter' => Select2::widget([
'model' => $searchModel,
'initValueText' => !empty($searchModel->ad_partner_id) ? $searchModel->partner->name : "",
'attribute' => 'ad_partner_id',
'options' => ['placeholder' => Yii::t('app', 'Search Partner ...')],
'pluginOptions' => ['allowClear' => true, 'autocomplete' => true,
'ajax' => ['url' => Url::base() . '/partner/get-partners',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
]),
],