[eluser]BravoAlpha[/eluser]
I've been looking, unsuccessfully, for a way to provide a default value for a select element that I am building myself:
Code:
<select name="category_id">
<?php foreach($categories->result() as $category): ?>
<option value="<?php echo $category->category_id; ?>"<?php echo $this->validation->set_select('category_id', (string)$category->category_id); ?>><?php echo $category->title; ?></option>
<?php endforeach; ?>
</select>
I came up with this replacement function:
Code:
function set_select($field = '', $value = '')
{
if ($field == '' OR $value == '')
{
return '';
}
if (isset($_POST[$field]) AND $_POST[$field] == $value)
{
return ' selected="selected"';
}
else if (isset($this->$field) AND $this->$field == $value)
{
return ' selected="selected"';
}
}
Any thoughts?