2

Hi my dropdown getting value from below code:

public function getFloorsHTML() {
     $jinput = JFactory::getApplication()->input;
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $myflrs = $jinput->get ('fvalue');
     $query->select($db->quoteName(array('id', 'f_name')));
     $query->from($db->quoteName('#__pk_flrs'));
     $query->where($db->quoteName('bldg')." = ".$db->quote($myflrs));
     $db->setQuery($query);
     $results = $db->loadObjectList();
     foreach ($results as $result) 
     {
       echo '<option value="' . $result->id . '" > ' . $result->floor_name.'</option>'; 
      }
    exit; 
}

Code is working fine but how can i have "Please Select" as default value?

1

Here is the answer:

echo '<option value="">Please Select</option>';
foreach ($results as $result) 
{
   echo '<option value="' . $result->id . '" > ' . $result->floor_name.'</option>'; 
}
1

Just to address some code issues here, instead of using an exit statement in your function you should return the values to the function call and run your loop there.

For example:

In your default.php file:
<?php $floors = $model->getFloorsHTML();?>
<select name="floors">
    <option value="">Please Select One</option>
    <?php foreach($floors as $floor){?>
        <option value="<?php echo $floor->id;?>"><?php echo $floor->floor_name;?></option>
    <?php }?>
</select>

In your model:
public function getFloorsHTML()
{
     $jinput = JFactory::getApplication()->input;
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $myflrs = $jinput->get('fvalue','','int');
     $query->select("id, f_name");
     $query->from("#__pk_flrs");
     $query->where("bldg = ".$db->quote($myflrs));
     $db->setQuery($query);
     $results = $db->loadObjectList();
     return $results;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.