| | |
combobox
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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[]"
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)
<?php // echo "Connected MySql"; $res1 = mysql_query("SELECT gp FROM gp order by gp") or die("Invalid query: " . mysql_query()); //echo ".mysql_query()"; $row=mysql_fetch_array($res1,MYSQL_ASSOC); $va=array() $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'; }else{ $selected = ''; } // print the option echo ' <option value="'.$va[$x].'"'.$selected.'>'.$va[$x].'</option>'; }echo '</select>'; ?>
Last edited by peter_budo; Nov 12th, 2008 at 5:25 am. Reason: Correcting code tags, please use [code], DO NOT USE <code>
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
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)
if ($result[key] == $_POST[whatever] { echo '<option value="'.$result[key].'" selected="selected">Whatever text here</option>'; } else{ echo '<option value="'.$result[key].'">Whatever text here</option>'; }
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
I miss my mind the most...."
Mark Twain
![]() |
Similar Threads
- Filling a ComboBox.... (Java)
- ComboBox Events (VB.NET)
- Issue related to combobox.... (VB.NET)
- need help with ComboBox (C++, Dialog) (C++)
- Hiding dataitems in a combobox (VB.NET)
- How to use combobox function (C++)
Other Threads in the PHP Forum
- Previous Thread: How install APC(Alternative PHP Cache) in window
- Next Thread: problem to display all datas from database
| Thread Tools | Search this Thread |
Tag cloud for PHP
# .htaccess 5.2.10 access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display dissertation download dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link login loop mail menu mlm mod_rewrite multiple mysql mysqlquery oop open parse paypal pdf persist php problem query radio random recursion regex remote script search server sessions sms soap sockets source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube





