rawky1976 Posted February 8, 2008 Share Posted February 8, 2008 Hi all; hope you're well!!!! I found a script that displays the field names from a table in a drop-down but I want to show the values in that field. I've tried to amend that script but think I've got the logic of the functions incorrect. Thanks for any help you might be able to offer <?php $connection = mysql_connect("localhost","xxx","xxx"); $select_db = mysql_select_db("dbname", $connection); //$fields = mysql_list_fields("dbname", "tablename", $connection); //$columns = mysql_num_fields($fields); $query = "select field from dbname.table ORDER BY field"; $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); $columns = mysql_fetch_array($result); echo "System Name: <select name=Field>"; for ($i = 0; $i < $columns[$num_rows]; $i++) { echo "<option value=$i>"; //echo mysql_field_name($fields, $i); } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/ Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 This should get you on track. This assumes the value is the first field and the name is the 2nd. echo "System Name: <select name=Field>"; while ($row= mysql_fetch_array($result)) { echo "<option value={$row[0]}>"; echo $row[1]; echo "</option>"; } echo "</select>"; Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461767 Share on other sites More sharing options...
rawky1976 Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks, I'm now getting undefined output on echo $row[1]; Is it because I'm only selecting one field and not *? Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461773 Share on other sites More sharing options...
rawky1976 Posted February 8, 2008 Author Share Posted February 8, 2008 OK, I figured that part out, but I'm only getting one row from the table, I need all of them please? Thank you, Mark Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461780 Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 Yes, select what you want displayed. If just field, and you only want the field as the value and text, change $row[1] to $row[0] Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461782 Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 Repost the new code OK, I figured that part out, but I'm only getting one row from the table, I need all of them please? Thank you, Mark Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461783 Share on other sites More sharing options...
rawky1976 Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks, here it is: - <?php $connection = mysql_connect("localhost","user","pass"); $select_db = mysql_select_db("dbname", $connection); //$fields = mysql_list_fields("dbname", "tablename", $connection); //$columns = mysql_num_fields($fields); $query = "select * from dbname.tablename ORDER BY fieldname"; $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); $columns = mysql_fetch_array($result); echo "System Name: <select name=Field>"; while ($row= mysql_fetch_array($result)) { echo "<option value={$row[4]}>"; echo $row[0]; echo "</option>"; } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461787 Share on other sites More sharing options...
rawky1976 Posted February 8, 2008 Author Share Posted February 8, 2008 Fixed it, there were two variables assigned the the fetch_array. Commented the top one out!!! Thank you, Mark Link to comment https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/#findComment-461797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.