Using from another table for radioList's select options - yii2

I want to use another table data for select options of radioList.
for getting better I mean, I want something like bellow code:
$form->field($model_add, 'link_type')->radioList([
'another table ID 1' => 'another table title 1',
'another table ID 2' => 'another table title 2',
]);
Can I use foreach inside it, for options?
If my question is not clear, ask me what you want to know more.

You can use ArrayHelper for this. Assuming $array looks like this:
[
['id' => 1, 'title' => 'aaa'],
['id' => 2, 'title' => 'bbb'],
// ...
]
You can map it to
[
1 => 'aaa',
2 => 'bbb',
// ...
]
like this:
$form->field($model_add, 'link_type')->radioList(
\yii\helpers\ArrayHelper::map($array, 'id', 'title')
);

It also same like Bizley answer,little clear
<?= $form->field($model, 'fieldName')->radioList(
ArrayHelper::map(TableName::find()->all(), 'id', 'name'),
['prompt' => 'Please Select']);?>

Related

Display previous post instead of the latest post

On wordpress I have a littl box that displays the latest post from a category.
The code looks like this:
<?php $args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates'
If I wanted to create a second box that displays not the latest post but the one before how should I approach it?
Thanks.
you can use get_posts() function like this:
$posts = get_posts(array(
'posts_per_page' => 2,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates'
));
and you can access the second post with $posts[1].
Thank you. Eventually I got what I wanted with the following:
<?php $args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'order_by' => 'date',
'category_name' => 'updates',
'limit' => '1',
'offset' => '1'
For the following boxes it becomes:
'offset' => '2' and 'offset' => '3'

Yii2 grid sorting is not correct with ArrayDataProvider

I'm using array data provider for grid view widget since the data source is an API response. It's working fine, but when I try to sort the 'Name' column, it's sorting incorrectly with the lowercase first letters. Please check the table grid screenshot. The API response which I'm receiving is correct with the current sorting.
1. Table Grid
2. Model
$provider = new ArrayDataProvider([
'allModels' => #$response->data,
'pagination' => false,
'sort' => [
'attributes' => ['id', 'name', 'shortDescription'],
],
]);
3. View
<?= GridView::widget([
' dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'shortDescription'
],
'options' => ['class' => 'content'],
'tableOptions' => ['class' => 'table table-striped table-hover'],
'summary' => Yii::t('app', 'Showing').' {begin}-{end} of '.$total.' '.Yii::t('app', 'items')
]);
?>
Please guide me to fix this. Thanks!

Yii2 update with multiple conditions

$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId', $this->user,
'product_id', $this->productId,
'name', $el['old'],
'created', $el['date'],
'category', $el['cat'],
]
);
I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.
You have a syntax error. Try following:
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat'],
]
);
Try This updateAll query :
MYTable::updateAll([ // field to be updated in first array
'name' => $el['new'],
'data' => $el['data']
],
[ // conditions in second array
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat']
]);

how to write mysql AND/OR in cakephp find() method [duplicate]

This question already has answers here:
cakephp OR condition
(4 answers)
Closed 6 years ago.
I wanted to find out a way to write the following code into cakephp find() method but the didn't find related resource on the cakebook.
my code is
SELECT * FROM Customers
WHERE
(Country='Germany' AND City='München')
OR (Country='Germany' AND CustomerName='München');
please share a way to write this accordingly in find() method. Thanks
You can do this using the OR key when using where:
$query = $this->Customers
->find()
->where([
'OR' => [
[
'City' => 'München',
'Country' => 'Germany'
],
[
'Country' => 'Germany',
'CustomerName' => 'München'
]
]
]);
This could be simplified to:
$query = $this->Customers
->find()
->where([
'Country' => 'Germany',
'OR' => [
['City' => 'München'],
['CustomerName' => 'München']
]
]);
See http://book.cakephp.org/3.0/en/orm/query-builder.html#advanced-conditions, I find using the andWhere and orWhere functions in combination so just stick to where!
Try
$query = $this->Customers->find(
'all',
[
'conditions' => [
'Country' => 'Germany',
'OR' => [
[
'City' => 'München',
],
[
'CustomerName' => 'München'
]
]
]
]
);
In the cookbook it says to wrap the or conditions in arrays if they are pertaining to the same field http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
If you want to paginate results you can try this:
public function yourFunctionName() {
$this->paginate['conditions']['and'][] = ['Country' => 'Germany'];
$this->paginate['conditions']['and']['or'][] = ['City' => 'München'];
$this->paginate['conditions']['and']['or'][] = ['CustomerName' => 'München'];
$customers = $this->paginate($this->Customers);
$this->set(compact('customers'));
$this->set('_serialize', ['customers']);
}

Codeigniter insert horizontal lines in form_dropdown()

I need to add horizontal lines in my drop down list. I have researched and found this way:
<select>
<option>First</option>
<option disabled>──────────</option>
<option>Second</option>
<option>Third</option>
</select>
The question is that I use Codeigniter form_dropdown() and cannot insert lines in my code. Could you please help me to insert horizontal lines in the code below.
$options = array(
'' => 'Select Size',
'' => '-----------', //does not work
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'' => '-----------', // does not work
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
echo form_dropdown('shirts', $options, 'set_value('shirts')');
Check your syntax. I think you are mixing single and double quotes there when you are ehco-ing out the actual form element. Also, your last item in your options array does not need the trailing ,
Otherwise, your code looks "good".
php
$options = array(
'' => 'Select Size',
'-----------',
'small' => 'Small',
'medium' => 'Medium',
'-----------',
'large' => 'Large',
'xlarge' => 'Extra Large'
);
echo form_dropdown('shirts', $options, $this->input->post('shirts'));
EDIT
To create your dropdown to use opt groups: "If the array passed as $options is a multidimensional array, form_dropdown() will produce an with the array key as the label."
$options = array(
'' => 'Select Size',
'Children' => array(
'small' => 'Small',
'medium' => 'Medium'
),
'Adults' => array(
'large' => 'Large',
'xlarge' => 'Extra Large'
)
);
echo form_dropdown( 'shirts', $options, $this->input->post( 'shirts'));
What I have found though, is that your optgroup label(s) need to be unique. "Children"/"Adults" otherwise it will only render the last group. So, you could run into a case where you need to have your data be 'child large' instead of just 'large'.
If you want to use disabled options while using form_dropdown, you might have to extend the form helper library and build your own. Otherwise, you could just use plain old' HTML syntax. Then you could just add the disabled="disabled" right on the option(s).
Hope this helps...