943,811 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 605
  • PHP RSS
Nov 11th, 2008
0

combobox

Expand Post »
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[]"
PHP Syntax (Toggle Plain Text)
  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>
Similar Threads
Reputation Points: 15
Solved Threads: 5
Junior Poster
rohitrohitrohit is offline Offline
120 posts
since Oct 2007
Nov 11th, 2008
0

Re: combobox

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
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How install APC(Alternative PHP Cache) in window
Next Thread in PHP Forum Timeline: problem to display all datas from database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC