Codeigniter insert horizontal lines in form_dropdown() - html

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...

Related

acf/save_post not triggered until manual update

I'm using acf_form to create a post like this:
<?php acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'new_post' => array(
'post_type' => 'books',
'post_status' => 'publish'
),
'fields' => array('field_5fb2a66de25ba', 'field_5fb009504f235',),
'submit_value' => 'Create Book',
'html_submit_button' => '<input type="submit" value="%s" />',
'updated_message' => ("Book submitted",)
Everything works except my acf/save_post function does not trigger until I manually edit the post and update it. Here's what I'm doing in acf/save_post:
$key_word = get_field('field_5fb2a66de25ba', $post_id);
update_post_meta( $post_id, 'rank_math_focus_keyword', $key_word );
This works, but not until I manually edit and update the page.
I'm not the only one with this question. If I get an answer, I'll find the others and share the good news.
Cheers, Richard
Eureka! All I had to do was change the priority on my action:
add_action('acf/save_post', 'my_acf_save_post', 25);

CakePHP 3 quoting function names defined in 'fields' configuration of my find() call

I am querying a database table conveniently named order and because of that I had to set 'quoteIdentifiers' => true in my app.php configuration. However, when I'm putting function names into the fields configuration of my find() call, CakePHP quotes them too.
$orders->find('all', array(
'fields' => array(
'DATE(Orders.date_added)',
'COUNT(*)',
),
'conditions' => array(
'Orders.order_status_id <>' => 0,
),
'group' => array(
'DATE(Orders.date_added)',
),
));
The query above ends up calling
SELECT <...>, `DATE(Orders.date_added)` FROM `order` <...>
which obviously throws an error.
I googled a lot, tried this:
$orders = $orders->find('all', array(
'conditions' => array(
'Orders.order_status_id <>' => 0,
),
'group' => array(
'DATE(Orders.date_added)',
),
))->select(function($exp) {
return $exp->count('*');
});
and that didn't work either, throwing me some array_combine error.
Is there any way for me to un-quote those function names, while keeping the rest of the query quoted automatically? Here's what I'm trying to accomplish:
SELECT <...>, DATE(Orders.date_added) FROM `order` <...>
Please help.
You should use function expressions, they will not be quoted, except for arguments that are explicitly being defined as identifiers:
$query = $orders->find();
$query
->select([
'date' => $query->func()->DATE([
'Orders.date_added' => 'identifier'
]),
'count' => $query->func()->count('*')
])
->where([
'Orders.order_status_id <>' => 0
])
->group([
$query->func()->DATE([
'Orders.date_added' => 'identifier'
])
]);
I'd generally suggest that you use expressions instead of passing raw SQL snippets wherever possible, it makes generating SQL more flexible, and more cross-schema compatible.
See also
Cookbook > Database Access & ORM > Query Builder > Using SQL Functions

ZF2 - inputFilter -> set value to null

In my ZF2 app I've got a form with several optional fields. If user decide to leave that field blank then it should set it as NULL in db, however - it doesn't work.
Example field:
$this->add(array(
'name' => 'productId',
'type' => 'select',
'options' => array(
'value_options' => array(
'global' => 'Global option', // this should be null
'mobile' => 'Another option'
)
),
));
And filter:
$inputFilter->add(array(
'name' => 'productId',
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
// 'messages' => array(
// \Zend\Validator\Callback::INVALID_VALUE => $value,
// ),
'callback' => function ($value) {
if($value === 'global')
{
$value = null;
//return true;
// echo '<pre>' . var_export($value, true) . '</pre>';
// die();
}
}
),
),
),
));
This piece of code works when "mobile" option is selected. However, with 'global' option it doesn't work.
As you can see, I've done some basic debugging to make sure that value is overrided on callback and it indeed returns NULL. Regardless of that validator says:
array (
'productId' =>
array (
'callbackValue' => 'The input is not valid',
),
)
So I tried to return true on callback which resulted in below error:
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (app.codes, CONSTRAINT fk_products_id FOREIGN KEY (productId) REFERENCES products (id)))
How am I supposed to pass null param to my db? If I cut this field completly from form then it is ignored and everything works.
Found solution, it's pretty simple actually.
I can set desired select options to some specific value and then use ToNull filter to convert it into null -
https://framework.zend.com/manual/2.4/en/modules/zend.filter.null.html
Code for $inputFilter:
$inputFilter->add(array(
'name' => 'productId',
'required' => false,
'filters' => array(
array('name' => 'ToNull', 'options' => array('type' => \Zend\Filter\ToNull::TYPE_STRING)),
),
));

Using from another table for radioList's select options

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']);?>

Appending one array to another array in the same loop

This is for a custom Wordpress page but I think the basic array principles should apply. I've not worked with complex arrays before so am a little lost, trial and error hasn't worked yet.
I have a database of Posts, each post has meta_key's of 'shop' and 'expired'.
'expired' is a date (YYYY-MM-DD) which is used to tell the visitor when a Post's content expires and this key is what I'm trying to work with.
If a Post's 'expired' date is before today, they are shown 'Offer expired'
If the 'expired' date is in the future, they are shown 'Offer expires in X days' (this script isn't shown below, not necessary)
Posts are listed in order of their 'expired' date, ASC. The problem is that when a post expires I'd like that post to show at the end rather than stay on top.
Example of what I currently see:
Post 1 | Expired 3 days ago
Post 2 | Expired 1 day ago
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
And what I'd like to see (note Post X order):
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
Post 2 | Expired 1 day ago
Post 1 | Expired 3 days ago
This is my array code where I've attempted to merge the two
$postid = get_the_ID();
$meta1 = get_post_meta($postid, 'shop', true);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = date('Y-m-d', time());
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'shop',
'value' => $meta1
)
),
'paged' => $paged,
'posts_per_page' => '5',
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$args2 = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'shop',
'value' => $meta1
)
),
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '<',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$final = $args + $args2;
$query = new WP_Query( $final );
while ( $query->have_posts() ) : $query->the_post(); ?>
HTML FOR DISPLAYING POST
endwhile;
At the moment it doesn't seem to take any notice of "$args2" and only displays $args
I'm sure my idea is on the right lines, needing to create two arrays and join them with the "+" rather than array_merge() but that's where I can't get any further.
Can someone kindly shed some light please? Thanks!
Now the solution you are trying to achieve is actually impossible if i understood your requirement properly. Let me explain why this is not achievable.
In your two arrays $args and $args2 most of the values are same leaving two odds , i am picking only one to just illustrate :
//it's in args
'meta_compare' => '>'
//it's in args2
'meta_compare' => '<'
Now what happens when you are trying to merge this two using array_merge($args , $args2):
'meta_compare' => '<'
That means it is taking 'meta_compare' from the later array which is $args2 here. This is the behavior of array_merge function defined in the doc:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one
Now if you are using + array union operator $args+$args2 :
'meta_compare' => '>'
That means it is taking 'meta_compare' from the first array which is $args here. This is the behavior of + array union operator defined in the doc :
The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.
Why it is happening because in the same level keys have to be unique . So in your situation they are in the same level :
$args = array ('key' => 'value1')
$args2= array ('key' => 'value2')
So only and only one value can exist here as in the merged array 'key' can point only one.
If this was the scenario :
$args [0] = array ('key' => 'value1' )
$args2 [1]= array ('key' => 'value2' )
Then both of the value stored by key will be resrved. This will be the resulting array :
array(2) {
[0]=>
array(1) {
["key"]=>
string(6) "value1"
}
[1]=>
array(1) {
["key"]=>
string(6) "value2"
}
}
Though i am not a geek of WP but as far as i understood from WP_query you can't pass args like this (I am not sure). So that leaves only one way to achieve this. You can pass these two args separately and merge the result as the result most probably (I am not sure) will be a 2D array you can merge them easily.
Hope that helps.
Happy coding :)
You can't just add 2 arrays together using the args+args2 syntax. PHP has no way of knowing what you mean by that. If you want the result to be an array containing both of these arrays, you could do this:
$final = array($args, $args2)
Otherwise I'm not sure how you want these two arrays combined. If you clarify how they need to be combined we might be able to help more.