Welcome Guest, Not a member yet? Register   Sign In
How to use template parser to preselect drop down box?
#1

[eluser]Khoa[/eluser]
Hi,

I have a view that contains a form, inside that form I have a dropdown box that contains the list of countries as follows:

Code:
// inside the form.php controller

$data['countries'] = "SOME QUERY TO GET THE LIST OF COUNTRIES IN RESULT_ARRAY";
$this->parser->parse('form', $data);

// inside the form.php view

<select>
   {countries}
      <option value="{id"}>{name}</option>
   {/countries}
</select>

How can I make Australia, which has the country id of 13 in this case, to be selected by default?

I tried the followings but they don't work:

Code:
// This does not work
<select>
   {countries}
      <option value="{id"} &lt;?php if($id == 13) echo "selected"; ?&gt;>{name}</option>
   {/countries}
</select>

// nor does this
<select>
   {countries}
      <option value="{id"} &lt;?php if("{id}" == 13) echo "selected"; ?&gt;>{name}</option>
   {/countries}
</select>

Any idea how to do this?

Thanks
#2

[eluser]Khoa[/eluser]
I think I found a solution straight after I post :-)

But the solution that I have requires the use of ordinary PHP as follows:

Code:
<select type="text" name="countryId" id="countryId" class="text">
   &lt;?php
      foreach ($countries as $country)
         {
            echo '<option value="'.$country['id'].'"'.(($country['id'] == $countryId)?" selected":"").'>'.$country['name'].'</option>'."\n";
         }
   ?&gt;
</select>

It works but the code does not look as clean as using template parser. Does anyone know how to achieve this using the parser lib?

I just realize that whatever "if" tags that we put inside the {countries}...{/countries} seem to be ignored. Is that right?

Khoa




Theme © iAndrew 2016 - Forum software by © MyBB