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?

2 Answers 2

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; } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.