Adding an "all" option on categories - php

This may seem like a basic question but I hope somebody could help me out.
I have a created a widget for a custom post type so that I could show the post types in a dynamic sidebar. In the widget, I have also created a dropdown that gets the categories to show.
My problem is that I also want to have the option to show all the categories and not just 1 category for the dropdown. Here's the code I have so far:
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>
Should I just use a multi-select for this and how do i go about it?

this will echo out all your categories but you'll have to update the selected part to match your instance.
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
with your full code
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>

Related

How to add option fetched from the database to the end in select php

I have a categories table where I have different categories. I have also add Other in the category. When I'm getting the categories to populate the select I want the other option to appear at last. how can I do that?
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category = mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php if (isset($_POST['category']) && h($category['id']) === $_POST['category']) echo 'selected'; ?>><?php echo h($category['name']); ?></option>
<?php } ?>
</select>
If the Id of Other category is fixed, then you have to ignore it in your database query.
Let's say that the Id of that option is 1, our query will be like this:
select * from categories where Id <> 1
This query brings all categories except "other".
After that do the following:
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category =
mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php
if (isset($_POST['category']) && h($category['id']) ===
$_POST['category']) echo 'selected'; ?>><?php echo
h($category['name']); ?></option>
<?php } ?>
<option value="1">Other</option>
</select>
hope it helps

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

Option selected with codeigniter and data from DB

I use codeigniter to create a dropdownlist with customers.
If I enter selected, than the last customer in the dropdown is automatically choosen.
Is it possible to select a customer inside that foreach?
My code snippet:
<?php foreach ($customers as $c): ?>
<option value="<?php echo $c->customer_id;?>"><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Yes it is. However you have to know which customer is chosen at the moment, and then inside the loop check if the chosen customer_id is the same as the current one:
<?php
$chosenCustomer_id = 5; //of course don't hardcode it
foreach ($customers as $c):
$selected = $c->customer_id == $chosenCustomer_id ? 'selected' : '';
?>
<option value="<?php echo $c->customer_id;?>" <?php echo $selected; ?>> <?php echo ><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Just add ternary condition in <option> inside for loop where $selectedOption is your value to be selected.
<?php $selectedOption = "yourvalue";
foreach ($customers as $c): ?>
<option value="<?php echo $c->customer_id;?>" <?= ($c->customer_id == $selectedOption ? "selected" : "")><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>
Use selected attribute in Option Tag
Selected needs to be set based on condition
$selected=(condition): "selected","";
<option <?php echo $selected; ?>> Option Inner Html </option>
Compare variable with attribute in option tag
<?php $chosenCustomer_id = 5;?>
<select name="customer" required>
<?php foreach ($customers as $c){?>
<option <?=($chosenCustomer_id==$c['customer_id']?'selected="selected"':'')?> value="<?=$c['customer_id']?>"><?=$c['customer_name']?></option>
<?php }
?>
</select>

Highlighting multiple selections on a form after submitting

This select box below does remember and highlight -one- selection after submitting the form. But when i make it multiple, it doesn't highlight any of the selectionS after submitting.
Any idea about how to achieve this?
Thanks in advance.
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if ($no_way == 'all') { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if ($no_way == $option) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
$_GET['no_way'] only handles single parameters you have to use $_GET['no_way[]'] and in_array($option, $no_way)
This works for me:
<?php
$options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
$no_way = $_GET['no_way'];
?>
<select class="postform" name="no_way[]" multiple size="5">
<option <?php if (in_array("all",$no_way)) { ?>selected="selected"<?php }?> value="all">Any</option>
<?php
foreach ($options_amount as $option) {
?><option <?php if (in_array($option,$no_way)) { ?>selected="selected"<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option><?php }?>
</select>
I'm not sure if this will help or not, but is there any chance you've tried just using selected instead of selected="selected"?
<option <?php if ($no_way == $option) { ?> selected<?php }?> value="<?php echo $option; ?>"><?php echo $option; ?></option>

Change selected value of <select> after it's created (via PHP) IF there's a value on the URL

I am creating a like this:
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
<OPTION VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>
I am using the $_GET to check for a value on the URL and then just take it and with that value I have to pre-select the option in the select menu, any ideas how to do this? I'm unsure of how to do it via javascript (or even if it would be more complicated).
just use
selected="selected"
on your pre-selected option
<select name="id" id="id">
<OPTION VALUE="null">Select<OPTION>
<? foreach ($genres as $row){ ?>
$selected = $_GET['your_var'] == $row->id ? 'selected="selected"' : false;
<OPTION <? echo $selected; ?> VALUE="<? echo $row->id; ?>"><? echo utf8_encode($row->name); ?></OPTION>
<?
}
?>
</select>

Categories