0

Hello Friends how to get data from database in Drop-down list?

This is my code but i can't get data in Drop-down list,

This is my php code.

public function System_parameter_dropdown_value() {
   $sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE     PARAMETER_KEY='ROLE_TYPE'";
   $rs=  mysql_query($sql) or die(mysql_error());
   $option_list="<option value=0>Select Role Type</option>";
   while($data=  mysql_fetch_assoc($rs)) {
      $option_list.="<option value='$data[PARAMETER_KEY]'>
      $data[PARAMETER_VALUE]</option>";
   }
   return $option_list;
}

This is my html option list, When i run the code it's show nothing in Dropdown list

<label class="control-label" for="roleid"  style="padding-right: 80px;">ROLE TYPE</label>
   <select id="" name="roleid">
      <?php echo $option_list['PARAMETER_KEY']['PARAMETER_VALUE'];?>
   </select>

I am Stuck on this code , please Guide me How to Resolve it.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

4 Answers4

1

Try some thing like this:

$arr = array(1 => 'MP', 2 => 'UP');

echo '<select>';
foreach($arr as $key => $val)
{
    echo '<option value="'.$key.'">'.$val.'</option>';
}
echo '</select>';

It will generate html like:

<select>
  <option value="1">MP</option>
  <option value="2">UP</option>
</select>

In your case it is like:

<select id="" name="roleid">
  <?php echo System_parameter_dropdown_value();?>
</select>
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

that's because $option_list in not array but string:

<?php $option_list = $your_object->System_parameter_dropdown_value(); ?>
<label class="control-label" for="roleid"  style="padding-right: 80px;">ROLE TYPE</label>
   <select id="" name="roleid">
      <?php echo $option_list;?>
   </select>

Method should be changed too

public function System_parameter_dropdown_value()
    {
        $sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE     PARAMETER_KEY='ROLE_TYPE'";
        $rs=  mysql_query($sql) or die(mysql_error());
        $option_list="<option value=0>Select Role Type</option>";
        while($data=  mysql_fetch_assoc($rs))
        {
           $option_list.=sprintf("<option value='%s'>%s</option>", $data['PARAMETER_KEY'], $data['PARAMETER_VALUE']);
        }
        return $option_list;
    }
Piotr Pasich
  • 2,639
  • 2
  • 12
  • 14
1

If you are writing the function System_parameter_dropdown_value inside a class, you have to create an object of the class and then call the function. Like this

$classObj = new className();

echo $classObj->System_parameter_dropdown_value();

class className {
    public function System_parameter_dropdown_value() {
       $sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE     PARAMETER_KEY='ROLE_TYPE'";
       $rs=  mysql_query($sql) or die(mysql_error());
       $option_list="<option value=0>Select Role Type</option>";
       while($data=  mysql_fetch_assoc($rs)) {
          $option_list.="<option value='$data[PARAMETER_KEY]'>
          $data[PARAMETER_VALUE]</option>";
       }
       return $option_list;
    }
}

I think this might help you.

1

I hope this one helps, it's complete example with the db connection.

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT record FROM table";
$result = mysql_query($sql);

echo "<select name='tagName'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['tagName'] ."'>" . $row['tagName'] .</option>";
}
echo "</select>";
Majid Ali Khan
  • 701
  • 8
  • 13