I have this nested array and i want to convert it into dropdown ,but at output it is just showing me combo-box with options as (array ,array,array)
<select name="pcity" id="pcity" multiple="multiple">
<?php
$pcitylist = array('Andaman and Nicobar' => array('North and Middle Andaman',
'South Andaman', 'Nicobar'), 'Andhra Pradesh' => array('Adilabad', 'Anantapur',
'Chittoor', 'East Godavari', 'Guntur', 'Hyderabad', 'Kadapa', 'Karimnagar',
'Khammam', 'Krishna', 'Kurnool', 'Mahbubnagar', 'Medak', 'Nalgonda', 'Nellore',
'Nizamabad', 'Prakasam', 'Rangareddi', 'Srikakulam', 'Vishakhapatnam',
'Vizianagaram', 'Warangal', 'West Godavari'), 'Arunachal Pradesh' => array('Anjaw',
'Changlang', 'East Kameng', 'Lohit', 'Lower Subansiri', 'Papum Pare', 'Tirap',
'Dibang Valley', 'Upper Subansiri', 'West Kameng'));
foreach ($pcitylist as $pcitylist1) {
echo '<option value="' . $pcitylist1 . '"' . (isset($_POST['pcity']) && $_POST['pcity'] ==
$pcitylist1 ? ' selected' : '') . '>' . $pcitylist1 . '</option>';
}
?>
</select>
i want it to display like this
<select>
<optgroup>Andaman and Nicobar</optgroup>
<option>North and Middle Andaman</option>
<option>South Andaman</option>.....
</select>
and so on...
foreach ($pcitylist as $key => $pcitylist1)
{
echo '<optgroup label="'.$key.'">';
foreach ($pcitylist1 as $finalCity) {
echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>' . $finalCity . '</option>';
}
echo '</optgroup>';
}
The $key holds the optgroup label. This will work with your array.
It is an multidimensional array... use one more for loop inside ur for loop and u will get the ouput..
Try the following..
foreach ($pcitylist as $key => $pcitylist1)
{
foreach ($pcitylist1 as $finalCity) {
echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>'.$key . $finalCity . '</option>';
}
}
Related
I have stuck ;/ I have activeradiolist and works ok, but I need to create another list bud with dropdowns item's
my code with $model for activeradioList
echo Html::activeradioList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
Now i try to convert this to dropdownList like ->
echo CHtml::dropDownList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
and have htmlspecialchars() expects parameter 1 to be string, object given
1) The CHtml is class from old Yii 1.x framework. Yii2 doesn't use the C prefix for its class names.
2) You are pairing the form field with your model instance so you should use the activeDropDownList() method instead of dropDownList().
3) In your radio button options you have item callback that is used to generate the html code for radio button. The drop down list doesn't have anything like that so you should remove it from its options. You can completly omit the fourth parameter because the item callback is the only option there.
So the code for drop down list should look like this:
echo Html::activeDropDownList($add, 'type_contact', $items);
I have a select box with currency 1 and 2, currency 1 is for Euro. I want my select box to preset the value 1 which is euro.
I am using controller to get the currency information like this.
$data = array(
'sexList' => $this->localization->getTextParamValues(null, 'user', 'Sex'),
'countryList' => $this->localization->getCountryList(),
'currencyList' => $this->localization->getCurrencyList(),
'languageList' => $this->localization->getLanguageList(),
'user' => $userExist,
'userinfo' => $this->user_profile->getUserInfo(),
'Currency' => $this->localization->getCompanyCurrency()
);
$this->load->view('header', $this->history->getPreviousPageInArray());
$this->load->view('borrow_registerpage_view',$data);
The currency is pushing the value one to the data array, but in the html file it doesnt preset automatically. Dont know what is wrong.
Here is the HTML code, I need to preset the value 1 in this table so it selects 1 normally which is EURO.
<?php
echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">';
if(!($user['Currency'] > 0)){
echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>';
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>';
}
}else{
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '"';
if($currency->ID==$user['Currency']){
echo 'selected';
}
echo '>' . $currency->Name .'';
echo '</option>';
}
}
echo '</select>'
?>
You Need to change your view
$opt = array();
foreach ($currencyList as $currency){
$opt[$currency->ID] = $currency->Name;
}
echo form_dropdown('currency_select', $opt, set_value('currency_select', $user['Currency'], 'id="my_profile_select_currency" class="form-control country_style"');
Try this. It will show EURO selected only if $user['Currency'] is greater than 0
<?php
echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">';
if(!($user['Currency'] > 0)){
echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>';
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>';
}
}else{
foreach ($currencyList as $currency){
echo '<option value="' . $currency->ID. '" '.$currency->Name == 'EURO' ? 'selected="yes"' : ''.'>' .$currency->Name. '</option>';
}
}
echo '</select>'
?>
If you need in both case you may use $currency->Name == 'EURO' ? 'selected="yes"' : '' in both foreach
I know this should be simple but I just can't seem to get my head around it.
I have a list of continents in a sql database that I get back using PHP DBO and display in a drop down list. What I then want to do is get the users preferred continent from the sql database and select that one in the list. E.g if the list contains World, Africa, Europe, N America and S America but the users favorite is 'Europe' I want that one selected. $getContinent is the users preference.
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
if ($getContinent != ''){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
I would be most grateful if someone could set me straight as I have found some examples on the internet but have been unable to get them to work :)
Your code should be like this
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
//just check if the option id is equal to the chosen value
if ($getContinent != '' && $getContinent==$row['CONTINENT_ID'] ){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
Its simple, as you guessed :D
You would use something like this:
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] . '">' . $row['CONTINENT_NAME'] . '</option>';
}
I hope that helps!
--al
You can use ternary operator
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] .
($getContinent == $row['CONTINENT_NAME']) ? '" selected="selected"' : '"' . '>' .
$row['CONTINENT_NAME'] . '</option>';
}
I have this drop-down and user can select multiple options ,how can i keep selected value on form after submit button, if error comes on form
<select onclick="document.getElementById('cand_qual4').style.display='none'; " name="oca[]" id="oca" multiple="multiple">
<?php
$odrop = array('B COM','M COM','BBA','MBA','LLB','LLM','CPA','CIMA','MS FINANCE','DISA','CISA','OTHER');
foreach ($odrop as $odrop1)
{
echo '<option value="' . $odrop1 . '"' . (isset($_POST['oca']) && in_array($odrop1,$_POST['oca']) ? ' selected' : '') . '>' . $odrop1 . '</option>';
}
?>
</select>
instead of
$_POST['oca'] == $odrop1
condition as $_POST['oca'] would be an array, try
in_array($odrop1,$_POST['oca'])
TRY THIS-
echo '<option value="' . $odrop1 . '"' . (is_array($_POST['oca']) && in_array($odrop1,$_POST['oca'] ) ? ' selected' : '') . '>' . $odrop1 . '</option>';
I am pretty new to PHP and am having a question about dropdown lists. I am trying to get the list to pull from the database and populate the value when a user edits a form but it is not currently working. There are a few examples of this exact same thing on here but I can't quite get it working, it is likely a syntax error on my end...
Here is my code:
echo '<p><label>Is this project targeted toward?</label><select name="proj_targ_tow"><option value="Select...">Select...</option><option value="National Site">National Site</option><option="Local Site">Local Site</option><option value="Regional Site">Regional Site</option><option value="Other">Other</option></select></p>';
And here is the logic to populate the value from the database, the row I am trying to pull from is 'proj_targ_tow'...
$typesArray = array ( 'Select..', 'National Site', 'Local Site', 'Regional Site', 'Other' );
$selectedType = '';
echo 'as;ldfjas;lfmawoiealknfsliu2047a ' . $row['proj_targ_tow'] . '<br />';
foreach($typesArray as $value){
if($value == $row['proj_targ_tow']) {
$selectedType = 'selected="selected"';
}
echo '<option value="' . $value . '" ' . $selectedType . '>' . $value . '</option>';
}
Can any of you coding gods out there help me out?
It looks to me like the $value in your echo statement is out of scope... what happens when you do this?
$typesArray = array ( 'Select..', 'National Site', 'Local Site', 'Regional Site', 'Other' );
foreach($typesArray as $value){
$selectedType = '';
if($value == $row['proj_targ_tow'])
$selectedType = 'selected="selected"';
echo '<option value="' . $value . '" ' . $selectedType . '>' . $value . '</option>';
}