I am creating a theme select option for an App. I want the user to choose from a select option which theme they want, light or dark.
I am storing theme names in the database like so:
I am then calling all themes in the Model and returning the array with this code:
public function getThemes() {
$query = $this->db->query('SELECT * FROM theme');
return $query->result_array();
}
I am using my Constructor to grab the data and Render it to my settings view like so: (I am sending a few things at the same time in $this->data that's why it is in an array but I cut that code out)
$this->data = [
'themes' => $this->model_setting->getThemes()
];
$this->render('backend/standart/administrator/setting/setting_general', $this->data);
In my HTML View I have the following HTML which successfully displays the correct 2 themes available:
<div class="col-sm-12">
<label>Select Your Desired Theme</label><br>
<select class="form-control" name="site_color_theme" id="site_color_theme">
<?php foreach ($themes as $theme): ?>
<option value="<?= $theme['theme']; ?>" ><?= $theme['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
When I save the settings form I am updating my options table in the database with the theme name. The Html Body Class calls on this options table and finds the chosen theme and uses this to render a specific CSS Stylesheet.
The problem I am having is when I save my settings, the Select Option does not display the saved theme name to show that this is the active theme chosen.
How can I get the Select Option to display the chosen theme when the user visits the settings page to select another theme?
You should have active_theme column in the theme table
active_theme set to 1 when form dropdown site_color_theme is submitted (the rest theme records set to 0)
You should use Codeigniter Form Helper to do that : https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_dropdown
// $themes = Theme list array [id => theme-name, id => theme-name]
// $active_theme = Record of "active_theme" with value equal to "1"
// $extra = Custom HTML attributes e.g. class="something" onclick="function(js)"
echo form_dropdown('site_color_theme', $themes, $active_theme, $extra);
Related
I am building a education platform within wordpress, but i am having difficulties getting the categories only to show the user assigned categories in the filter.
I am using ACF Pro to assign my custom categories to the users.
And here i get the terms assigned to the user.
$uid = get_current_user_id();
$departments = get_terms( array('departments'), 'user_'.$uid );
In this line i am looping the categories:
foreach ($departments as $value) {
if(!in_array($value->term_id, $departments)){
echo '<input id="switch-demo'. $value->term_id .'" type="checkbox" data-filter="'. $value->term_id .'" class="filter-check '. $value->term_id .'" value="'. $value->term_id .'" name="departments[]" >';
echo '<label for="switch-demo'. $value->term_id .'" class="filter '.$value->slug.'">'. $value->name .'</label>';
// echo $value->name;
}
}
For some reason it is showing all categories that are currently in my post type "Courses".
But i want to make sure that y category field is only showing categories that are assigned to the user.
Please help.
get_terms doesn't take any user_.$uid parameter. Only thing you should pass to get_terms should be the arguments array.
For ACF, to get a field value you will need to use the_field or get_field functions.
$uid = get_current_user_id();
$departments = get_field( 'departments', 'user_'.$uid );
More details on how to get values from user using ACF
My Problem I can't get my dynamic checkboxes to save properly in from my edit.blade, they only work if the values are 1, if an unchecked checkbox is submitted via hidden field it will overwrite the next set of checkbox checked values.
My Code
I have a crud resource that takes orders, the form in the create.blade itself has a bunch of dynamic fields that add a new product to the order via 'add-new' button that clones the product fields.
Part of that form is a bunch of days checkboxes that work fine and are stored correcly.
Where I'm Getting Stuck
I've made an edit.blade to be used to correct any mistakes that would be made while creating an order.
To call back the section that refers to the dates checkboxes I've used the following blade syntax (I know its different from create, mainly due to me trying to fix the problem)
#foreach($orders as $orderkey => $order)
#foreach($days as $day)
{{ Form::hidden($day.'[]', 0, array('id'=> $day.'_hidden_'.$orderkey, 'class' => 'is-checkradio')) }}
{{ Form::checkbox($day.'[]', 1, $order->{$day}, array('id'=> $day.'_'.$orderkey, 'class' => 'is-checkradio')) }}
<label for="<?php echo $day.'_'.$orderkey; ?>"><?php echo $day; ?></label>
#endforeach
#endforeach
OrderController - Update
I've had to use the following in my controller to get the fields to update however whenever a checkbox is left unchecked it will overwrite the next checked value.
$customer = Customer::find($id);
foreach($customer->orders as $key => $order){
$Monday[] = $request->Monday[$key];
};
$updates = array(
'Monday' => $Monday,
);
foreach($updates['orders'] as $k => $update){
$update_order->Monday = $updates['Monday'][$k];
$update_order->save();
};
This part:
foreach($customer->orders as $key => $order){
$Monday[] = $request->Monday[$key];
};
Is creating an array where if the existing data is true and the new data is true, save it. Otherwise delete it. That means any checkboxes that have been deselected will be captured, but any newly checked checkboxes won't be. Is this your intention?
Why not just do:
$updates = array(
'Monday' => $request->Monday,
);
I may not have fully understood your question so please comment and/or amend the Q if you need to clarify further,
Found out how to fix it from Unchecked checkbox returning null value
I needed to add my $orderkey to the name []
So in the end this worked:
#foreach($days as $day)
{{ Form::hidden($day.'['.$orderkey.']', null, array('id'=> $day.'_hidden_'.$orderkey, 'class' => 'is-checkradio')) }}
{{ Form::checkbox($day.'['.$orderkey.']', 1, $order->{$day}, array('id'=> $day.'_'.$orderkey, 'class' => 'is-checkradio')) }}
<label for="<?php echo $day.'_'.$orderkey; ?>"><?php echo $day; ?></label>
#endforeach
In the form there is such group of checkboxes
$form->field($model, 'ingredients')->checkboxList(
ArrayHelper::map($ingredients, 'id', 'name')
)
In html it looks like
<input name="Dish[ingredients][]" value="1" type="checkbox">
<input name="Dish[ingredients][]" value="2" type="checkbox">
How I can get an array of checkboxes values in the actionCreate method of controller?
I trying do it like this
Yii::$app->request->post('Dish[ingredients]', [])
but I get an empty array.
Addition:
Ingredients property is not present in generated model Dish, I'm had added it later by the hand. Dish and Ingredients have a many to many relationship.
How to add ingredients to theDish model correctly?
Now if I do
$model = new Dish();
$model->load(Yii::$app->request->post());
var_dump($model->ingredients);
$model->ingredients is empty array.
Create the ingredients attribute in the Dish model:
public class Dish {
public $ingredients;
...
}
Load all the post data to your model and then access the ingredients array:
$model = new Dish();
$model->load(Yii::$app->request->post());
var_dump($model->ingredients);
I am trying to pass a variable from the index page to an article view. Basically the width of a div needs to change based on whether there are sidebars.
index.php:
if ($this->countModules('position-1')&$this->countModules('position-3')){
$content_margin = 'contentCenter';
}elseif ($this->countModules('position-1')&!$this->countModules('position-3')){
$content_margin = 'contentRight';
}elseif (!$this->countModules('position-1')&$this->countModules('position-3')){
$content_margin = 'contentLeft';
}else{
$content_margin = '';
}
How do I then access the $content_margin variable within the component?
<jdoc:include type="component" class="<?php echo $content_margin; ?>" />
I would rather try something like this:
<div class="<?php echo $content_margin; ?>">
<jdoc:include type="component" />
</div>
You do not need to pass this variable into your component, just give your CSS classes different widths.
If you want to count the number of modules in your component you could look at this link
Which Submit Button was Clicked in CakePHP?
** what is solution for 2 buttons on same form with only 1 action in cakephp? **
i have following code,1 form contain 2 buttons print & ship,if i click
print button,page is redirected on printorder page but problem is , if i click ship
button it is not working & page is redirected on printorder.
==========================================================================
<?php // in index.ctp file
echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->submit('Ship',array('name'=>'user','value'=>'Ship','id'=>'1'));
echo $this->Form->submit('Print',array('name'=>'user','value'=>'Print','id'=>'2'));
echo $this->Form->end();
?>
<?php // in UserController
public function orders()
{
if($this->params->data['form']['user'] = "Print" )
{
$this->redirect(array('action'=>'printorder'));
}
if($this->params->data['form']['user'] = "Ship")
{
$this->redirect(array('action'=>'shiporder'));
}
}
?>
It's because you have created one form and you are submitting 2 different values on the "USER" form.
so it will redirected because action of the form is common.
To avoid it.. Using of 2 different forms are the best way.
Another way is to use javascript but I suggest to use 2 different forms.
Typical solution to two submit buttons situation is to create a submit button (default action) and a regular button to handle another action. Than you can use the JavaScript to implement the behaviour of the second button, e.g. to set a value of a hidden field which contains the actual 'action' string:
echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->hidden('submit_action', array(id='submit_action')); ?>
echo $this->Form->submit('Ship',array('value'=>'Ship','id'=>'ship_button'));
echo $this->Form->button('Print',array('value'=>'Print','id'=>'print_button'));
echo $this->Form->end();
And the js:
<script>
var form = <get your form here>
var print_button = document.getElementById('print_button');
print_button.onclick = function() {
// Set value of print button to the #submit_action hidden field
document.getElementById('submit_action').value = print_button.value;
// Submit the form
form.submit();
}
</script>
I followed the top voted (not selected solution) from Two submit buttons in one form, which suggested applying different values to each submit button, and checking those on submit.
However, CakePHP didn't easily play with this technique, as despite setting 'value' key in the $options array of $this->Form->submit, no value was set on the generated element. So, I followed the suggestions from here, and used the 'name' value key.
view.ctp
$this->Form->submit('Submit 1', array('name' => 'action1'));
$this->Form->submit('Submit 2', array('name' => 'anotherAction'));
controller.php
if (array_key_exists('action1', $this->request->data)) {
/** setup any data you want to retain after redirect
* in Session, Model, or add to redirect URL below
*/
// redirect to another action
$this->redirect(array('action' => 'myOtherAction'));
} else if (array_key_exists('anotherAction', $this->request->data)) {
// do something with anotherAction submit
}