Code Lover
Code Lover

Reputation: 8348

Return all row in while loop for Dropdown select box option

I am trying to return all row as a select box Option but it is returning only first row

while($parent_cat = mysql_fetch_array( $result )) 

{
   return '<option value="'.$parent_cat['categoryid'].'">'.$parent_cat['title'].'</option>';
}

How can I return all row?

Upvotes: 1

Views: 2284

Answers (2)

Scott Mcleod
Scott Mcleod

Reputation: 1

I managed to pull this off this way.

        function openDB()
        {
            global $conn, $username,$host,$password,$db;
            $host = "localhost";
            $username ="username";
            $password= "password";
            $db = "databasename";

            $conn = mysql_connect($host, $username,$password) or die(mysql_error());
                            mysql_select_db($db,$conn) or die(mysql_error());
        }  
function closeDB()
{
    global $conn;
    mysql_close($conn);
}
        openDB();
        $query3 = "select * from table_name";

        $result2 = mysql_query($query3,$conn)
                or die ("Error in query: $query.". mysql_error());

                // if a result
        if(mysql_num_rows($result) > 0)
        {
            //turn it into an object
            $row = mysql_fetch_object($result);
        if(mysql_num_rows($result2) > 0)
        {
            //turn it into an object
                    $row2 = mysql_fetch_object($result2);   
        ?>

                    <? 
                    //creates a options box for the list of items in the dropdownmenu
                    echo"<select>";
                    while($row2=mysql_fetch_array($result2))
                    {
                        echo "<option>".$row2['feild_title']."</option>";
                    }
                    echo"</select>";
        closeDB();
        ?>

Upvotes: 0

GGio
GGio

Reputation: 7653

You can expand on below function by passing some attributes as parameters and building the dynamic drop down as you wish.

function showDropDown() {

   $html = '<select>';

   $i = 0;
   while (your loop condition) {
      $html .= '<option value="">Hello World</option>';
      $i++;
   }

   $html .= '</select>';

   //in case your loop fails return empty instead of drop down without options.
   return $i > 0 ? $html : '';

}

echo showDropDown();

The way i use above function is:

  function buildDropDown(array $array, $attributes = array()) {
      if (! empty($array)) {
         $html = '<select ';
         foreach($attributes as $attr => $val) {
             $html .= $attr . '="' . $val . '" ';
         }

         foreach($array as $key => $value) {
             $html .= '<option value="'.$key.'">'.$value.'</option>';
         }

         $html .= '</select>';

         return $html;
      }
      return '';
  }

  $testArr = array(1 => 'A', 2 => 'B', 3 => 'C'); 
  $attrs = array(
            'id' => 'hello', 
            'name' => 'hello', 
            'style' => 'background-color: blue'
           )
  echo buildDropDown($testArr, $attrs);

above generates:

 <select id="hello" name="hello" style="background-color:blue">
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
 </select>

Upvotes: 2

Related Questions