combobox

Reply

Join Date: Oct 2007
Posts: 43
Reputation: rohitrohitrohit is an unknown quantity at this point 
Solved Threads: 3
rohitrohitrohit's Avatar
rohitrohitrohit rohitrohitrohit is offline Offline
Light Poster

combobox

 
0
  #1
Nov 11th, 2008
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[]"
  1. <?php
  2. // echo "Connected MySql";
  3. $res1 = mysql_query("SELECT gp FROM gp order by gp") or die("Invalid query: " . mysql_query());
  4. //echo ".mysql_query()";
  5. $row=mysql_fetch_array($res1,MYSQL_ASSOC);
  6. $va=array()
  7. $va[] =$row['gp'];/?????????????????????????????????????????????
  8. echo '
  9. <select name="gp" id="gp">';
  10. for($x = 0; $x < count($va); $x++)
  11. {
  12. // write "selected" if the value matches the one posted
  13. if($va[$x] == $_POST['gp'])
  14. {
  15. $selected = 'selected';
  16. }else{
  17. $selected = '';
  18. }
  19. // print the option
  20. echo '
  21. <option value="'.$va[$x].'"'.$selected.'>'.$va[$x].'</option>';
  22. }echo '</select>';
  23. ?>
Last edited by peter_budo; Nov 12th, 2008 at 5:25 am. Reason: Correcting code tags, please use [code], DO NOT USE <code>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: combobox

 
0
  #2
Nov 11th, 2008
Dude,
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.
http://www.daniweb.com/forums/thread156511.html

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
  1. if ($result[key] == $_POST[whatever]
  2. {
  3. echo '<option value="'.$result[key].'" selected="selected">Whatever text here</option>';
  4. }
  5. else{
  6. echo '<option value="'.$result[key].'">Whatever text here</option>';
  7. }
Last edited by Rhyan; Nov 11th, 2008 at 7:41 am.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC