0

I am trying to get a values from the drop down displayed from table .

i have tried in diff ways but i don't know where i have gone wrong.

Can any one help me on this..

Below is my code.

<tr class="form-field" id="appid">
<div>
<th valign="top" scope="row" >
<label for="country"><?php _e('country', 'custom_table_example')?></label>
</th>
<td>
<select id="country" name="country" class="code" >;
<option value="">select country</option>
<?php 
global $wpdb;
$coun_name = $wpdb->get_col("select country_name FROM countries") ;
//print_r($coun_name);
foreach($coun_name as $a)
    {
echo '<option value="'. strtolower($a) .'" />' . "$a </option>";

    }
  ?>
</td>
</div>
</tr>

The above code is displaying values into drop down.

now the problem is i need to get the selected values.

echo '<option value="'. strtolower($a) .'"<?php echo $item['country']==".$a."?'selected="selected"':'' ?> />' . "$a </option>";

$item is the variable where i am storing all data.

country =name attribute.

1 Answer 1

1

Have you set $item from $_POST? Are you using POST as your form action? Do something like this:

<form name='countryTest' method='POST' action='<?/*where your action is going to*/?>'>
    <select name='country'>
    <?foreach($coun_name as $c){
        ?><option value='<?echo$c;?>'<?if($_POST['country']==$c)echo' selected="selected"';?><?
    }?>
    </select>
</form>

OR! If you want to be really cool (yea ok not so cool but nerdy)! Make a function or class function to do all this for you!

class formHelper{
    public function select_form($name,$options=array([0]=>'Please select'),$selected=array(),$multiple=false){//name of select, options, selected options, multiple select
        if(!is_array($selected))$selected=array($selected);
        $sel='<select name="'.(($multiple===true)?$name.'[]':$name).'"';
        if($multiple===true)$sel.=' multiple';
        $sel.='>';
        foreach($options as $value=>$shown){
            $sel.='<option value="'.$value.'" '.((in_array($value,$selected))?'selected="selected"':'').'>'.$shown.'</option>';
        }
        return$sel.='</select>';
    }
}

Now to use it just do this

$coun_name=array(merge(array('Please select a country'),$coun_name));
formHelper::select_form('country',$coun_name,$_POST['country']);

EDIT

your error is you've set your value to lower but when you're comparing it's not lowered. See strtolower. What you want to do is compare both as lower as $item will be lower. I'd recommend using an integer when comparing like this:

array(
    [1]=>'England',
    [2]=>'Wales',
    [3]=>'Scotland'
);

So that your values will be

<option value='1'>England</option>
<option value='2'>Wales</option>
<option value='3'>Scotland</option>

But ye your issue is $item['country']==$a. Needs to be $item['country']==strtolower($a). And remove the string quotes with the full stops. "england" does not equal ".England.". The reason it's "england" already is because you've already set the string to lower. Unless $item is not $_POST['county']'

Sign up to request clarification or add additional context in comments.

10 Comments

Where is $item being set? And do you have an integer on the table for the country?
$item is a variable to store all data and passing $item values into db
check what $item is and $a. Like var_dump($a);var_dump($item);
this is how i set the value in to $ item <option value="Approved" <?php echo $item['status']=="Approved"?'selected="selected"':'' ?>>Approved</option>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.