default value for dynamic select list

chrissy6930 23 May, 2014
hi all,

I should set a default value for a dynamic select list:

<?php $db=& JFactory::getDBO(); ?>
<select size="1" label_over="0" hide_label="0" class="validate['required']" title="" type="select" name="ttype">
   <option value=""><?php echo JText::_('PLEASE_SELECT'); ?></option>
   <?php $db->setQuery("SELECT * FROM #__cf_ttypes ORDER BY `name` ");
$ttypes = $db->loadObjectList();
foreach ( $ttypes AS $ttype) {
	$name = $ttype->name;
	echo "<option value='" . $ttype->cf_id. "'>" . $name . "</option>";
} ?>
</select>


the default value should be '1'. only how am I supposed to achieve that?

could somebody pls help me out?
GreyHead 24 May, 2014
Hi chrissy69230,

Pleas try:
foreach ( $ttypes AS $ttype) {
  $selected = '';
  $name = $ttype->name;
  if ( $ttype->cf_id == 1 ) {
    $selected = "selected='selected'";
  }
 echo "<option value='{$ttype->cf_id}' {$selected} >{$name}</option>";

Bob
This topic is locked and no more replies can be posted.