hi all,
i am fetching recods from mysql in a combobox.but only one record display .... i want all record display in combobox ......................i write query below and need help how to sote fetch row in array i declered "$va[]"

    // echo "Connected MySql";
  $res1 = mysql_query("SELECT gp FROM gp order by gp") or die("Invalid query: " . mysql_query());
  //echo ".mysql_query()";
   $va[] =$row['gp'];/?????????????????????????????????????????????
 echo '
	<select name="gp" id="gp">';
for($x = 0; $x < count($va); $x++)
	  // write "selected" if the value matches the one posted
	  if($va[$x] == $_POST['gp'])
		  $selected = 'selected';
		  $selected = '';
	   // print the option
	   echo '
		<option value="'.$va[$x].'"'.$selected.'>'.$va[$x].'</option>';
}echo '</select>';
Member Avatar for Rhyan

First of all you have to read all records returned from the DB. You cannot fetch the whole result table from the database using a single php function. PHP can retrieve only one record at a time, so if you want to retrieve all rows from the SQL result, you have to type this many times mysql_fetch_array(), as many rows have been returned from the sql query.
Please check my post here, explaining result retrieval.

Then, once you have all your data, you can proceed with selected / non-selected verification of values in the <select> element.
Instead of loading a variable with name $selected, in my opinion it is better that you do the following

if ($result[key] == $_POST[whatever]
 echo '<option value="'.$result[key].'" selected="selected">Whatever text here</option>';
echo '<option value="'.$result[key].'">Whatever text here</option>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.