Preset currency value on select codeigniter - php

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

Related

Get data from DB to set as default in dropdown HTML/CSS/MYSQL/PHP

Hello guys I want to get data from the DB to set as default/selected in my dropdown.
I have a page named Update Project, in that page I have a dropdown component that is filled by records from my data, however I want to set the selected value from the query result.
This is what I've tried so far:
$st_ac = $conn->prepare( "SELECT p.project_code, m.type
FROM tblprojects as p
JOIN tblprojectsmaster as m
ON p.project_code = m.project_code
WHERE p.project_code = :code" );
$st_ac->execute(array(':code' => $code));
$result = $st_ac->fetch(PDO::FETCH_ASSOC);
$ptype = $result['type']; // the data to be selected in the dropdwon
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value"' . $type . '"';
if($ptype == $type)
echo 'selected="selected"';
echo '>' . $description . '</option>';
echo '<br />';
?>
<option value="<?php echo $type; ?>"><?php echo $description; ?></option>
<?php
}
?>
But I can't be able to achieve what I want. Any ideas? Your help will be truly appreciated. Thanks.
Try this
<select name="projectType">
<?php
for($i=0; $row_pt = $st_pt->fetch(); $i++){
$type = $row_pt['type'];
$description = $row_pt['description'];
echo '<option value="'.$type.'"';
if($ptype == $type)
{
echo 'selected="selected"';
}
echo '>'.$description.'</option>';
}
?>
May be you can use like this
echo '<option value="' . $type . '"'; if($ptype == $type) { echo 'selected="selected" } >'; echo $description . '</option>'; echo '<br />';

Update drop down from SQL database using PHP

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>';
}

making dropdown with optgroup using php nested array list

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>';
}
}

populate dropdown list from database when editing form MySQL

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>';
}

How to set multiple select options by default from array

I'm trying to retrieve options/values from my database in the from of an array i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary.
//data in database
$mytitle = array(
'Arbitrator',
'Attorney',
'Student',
'Other'
);
//data for multiple select
$title = array(
'Judge' ,
'Magistrate' ,
'Attorney' ,
'Arbitrator',
'Title Examiner' ,
'Law Clerk','Paralegal' ,
'Intern' ,
'Legal Assistant',
'Judicial Assistant',
'Law Librarian' ,
'Law Educator' ,
'Attorney',
'Student',
'Other'
);
echo "<select name='title[]' multiple='multiple'>";
$test = implode(',', $mytitle);
for ($i=0; $i<=14; $i++) {
if($test == $title[$i]) {
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}
else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
echo "</select>";
I think you may have a logic error. Try this as your loop:
foreach ($title as $opt) {
$sel = '';
if (in_array($opt, $mytitle)) {
$sel = ' selected="selected" ';
}
echo '<option ' . $sel . ' value="' . $opt . '">' . $opt . '</option>';
}
Use the in_array() function.
for ($i=0; $i<=14; $i++) {
if(in_array($title[$i], $mytitle)){
echo "<option selected value='$title[$i]'>$title[$i]</option>";
}else {
echo "<option value='$title[$i]'>$title[$i]</option>";
}
}
Very simple with the help of jQuery where the select has the id test
$('#test option').attr('selected', 'selected');
JSFiddle Example

Categories