Order values in select option list - php - php

I am new to coding and have the below select option list that is returning unordered values. The code checks the database for existing values (number of bedrooms) and returns them as options in the list. I would like to order them in an ascending manner:
<select class="bedrooms" name="Bedrooms" id="Bedrooms" >
<option value="">BEDROOMS</option>
<?php if ($bed_room): ?>
<?php foreach ($bed_room as $key => $value): ?>
<option value="<?php echo $value['bed_room'] ?>"><?php echo $value['bed_room'] ?></option>
<?php endforeach; ?>
<?php endif ?>
</select>

You have to search for ($bed_room) in your files so you can see from where it is getting the values and from there you can sort it as you like
or you can sort the array itself like this:
natsort($bed_room);
<select class="bedrooms" name="Bedrooms" id="Bedrooms" >
<option value="">BEDROOMS</option>
<?php if ($bed_room): ?>
<?php natsort($bed_room);>
<?php foreach ($bed_room as $key => $value): ?>
<option value="<?php echo $value['bed_room'] ?>"><?php echo $value['bed_room'] ?></option>
<?php endforeach; ?>
<?php endif ?>
</select>

Related

error php foreach in html input type select

I have a problem, I have an array of contacts that I send from the controller to the view and when it comes to going through it with a pos loop, everything goes well, but the problem lies when I try to interact within a select type input, creating for each contact a <option> iterates but does not access the values
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact[$id] ?>"><?php echo $contact[$username] ?></option>
<?php endforeach; ?>
</select>
this does work but outside the input select -->
<?php foreach ($contacts as $contact) : ?>
<?php debug($contact); //function for print data
?>
<?php endforeach; ?>
this is what the date looks like -->
There are 3 different method to get value.
using key name with "square brackets []":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact['id'] ?>"><?php echo $contact['username'] ?></option>
<?php endforeach; ?>
</select>
using key name with "arrow ->":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact->id ?>"><?php echo $contact->username ?></option>
<?php endforeach; ?>
</select>
using key name with "dots . ":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact.id ?>"><?php echo $contact.username ?></option>
<?php endforeach; ?>
</select>
Those all methods are same but we can get value using this methods simple.

Choosing selected values from array in select option tag

I have a for-each loop that populate multiple select option tag in edit mode, I would like to select the selected option that stored in the database.
The value from the DB is inside explode like this:
$f_r = explode(",",$user['field']);
This is my options create:
<?php foreach ($fields as $field) : ?>
<option value="<?= $field['field_name'] ?>" style="direction: rtl" ><?= $field['field_name'] ?></option>
<?php endforeach; ?>
How can I select the values from the $f_r value ?
I have tried this code and I am getting the result but my list in the option tag is tippled based on the array result:
<select class="selectpicker form-control" name="field[]" id="field" multiple data-live-search="true">
<?php $f_r = explode(",",$user['field']); ?>
<!-- Array ( [0] => נדלן פרוייקטים [1] => נדלן פרטי [2] => נדלן פרטי ומסחרי )-->
<?php foreach ($fields as $field) : ?>
<?php foreach ($f_r as $f) : ?>
<option value="<?= $field['field_name'] ?>" style="direction: rtl" <?php if($f == $user['field']){echo 'selected';} ?> ><?= $field['field_name'] ? </option>
<?php endforeach; ?>
<?php endforeach; ?>
</select>
How can I make it work without the option duplication?
I guess you could use in_array. Consider the following:
<?php foreach ($fields as $field) : ?>
<option value="<?= $field['field_name'] ?>" style="direction: rtl" <?php if(in_array($field['field_name'], $f_r)) {echo 'selected';} ?> ><?= $field['field_name'] ?></option>
<?php endforeach; ?>
Sorry for syntax - not on my computer but I hope you get the idea...

how to set_value use for dropdown input using codeigniter?

I want to used set_value for input where type=text and i have no problem with that.
I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .
My code:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
if($item->sport_name==$mySport)
{?>
<option value="<?php echo $item->sport_id; ?>" selected><?php echo $item->sport_name; ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id; ?>"><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
You Can try This:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id; ?>" <?php if($item->sport_name==$mySport)
{ echo "Selected";} ?>><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
As per the docs, you would not use set_value for a select element:
"Permits you to set the value of an input form (text input) or textarea.".
Rather, you would use set_select.
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
set_select($field[, $value = ''[, $default = FALSE]])
Parameters:
$field (string) – Field name
$value (string) – Value to check for
$default (string) – Whether the value is also a default one
Returns: ‘selected’ attribute or an empty string
Return type: string
Example:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
<option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>
your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php
foreach($getSport as $item):
$selected='';
if($item->sport_name==$mySport)
{
$selected='selected';
}
?>
<option value="<?php echo $item->sport_id; ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
The best way of doing this, while remaining readability would be as follow:
<select class="form-control" name="sport_name" id="sport_name">
<option selected>--Select Sport--</option>
<?php foreach($getSport as $item): ?>
<option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use
<?php echo $item['sport_name']; ?>

Not able to get saved value from db mysql as selected value in dropdown. I trying to update the form which user has already filled

I am trying to get data from database all values are reflecting good but saved value for dropdown is not coming selected.
<div class="col-md-4">
<select name="OfficeName" id="OfficeName" class="form-control" onchange="getText(this)" required>
<?php
while($data = dbFetchAssoc($result)){
?>
<option value=''><?php echo $data['off_name']; ?></option>
<?php
}//while
?>
</select>
</div>
This is a common issue.
Let's say you have a select statement that is populated with key value pairs from a database:
<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option value="<?= $value ?>"><?= htmlentities($label) ?></option>
<?php endforeach ?>
When the form is submitted, you'll get the value of sel in $_GET or $_POST (or $_REQUEST).
<?php
$selected = $_POST['sel'];
?>
To select the corresponding option, update the above code like so:
<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option <?php if ($selected == $value) : ?>selected<?php endif ?> value="<?= $value ?>">
<?= htmlentities($label) ?>
</option>
<?php endforeach ?>

PHP & HTML : how to create dynamic drop down list

I have a little problem.
I'm searching the internet now for quite some time to find a proper solution but I was not successful so far.
This is what I want :
First, I choose Category
Then, in second selection contain all the store result from category selection.
this is my first drop down list which is contain Category :
<select name="category" class="form-control" id="select1">
<option value="-1"> - Choose One -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select>
this is my second drop down list :
<select name="category" class="form-control" id="select2">
<option value="-1"> - Choose One -</option>
<?php
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreId(); ?>"><?php echo $store->getStoreName(); ?></option>
<?php endforeach; ?>
</select>
Anyone know how to implement dynamic drop down list for this case ?
Well first of all I'd like to ask that you separate business logic from view logic as much as possible, so I will do that in this answer.
Secondly, the 2nd dropdown box logic that you have does not contain anything that would retrieve stores for a given category, I will therefore make some assumptions and you can adjust based on that.
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$categories = $StoreCategoriesAPIAccessor->getStoreCategories();
if (!empty($_GET['category'])) {
$category_id = $_GET['category'];
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores($category_id); // Assumption, the call to getStores() accepts category_id as a parameter
} else { // Optional as you don't need to declare variables in PHP, but its better practice to do so
$category_id = null;
$stores = array();
}
?>
<select id="select_category" name="category" class="form-control" onchange="window.location='?category=' + this.value">
<option value="">- Choose One -</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category->getStoreCategoryId() ?>"><?php echo $category->getCategory() ?></option>
<?php endforeach ?>
</select>
<select id="select_store" name="store" class="form-control"<?php echo $category_id == null ? " disabled='disabled'" : "">
<option value="">- Choose One -</option>
<?php foreach ($stores as $store): ?>
<option value="<?php echo $store->getStoreId() ?>"><?php echo $store->getStoreName() ?></option>
<?php endforeach ?>
</select>

Categories