nashsaint Posted May 26, 2008 Share Posted May 26, 2008 Hi, I wanted to populate a drop down menu from sql table but my code doesn't work.. help please. Here's my Code: <select name="select">'; // default is 0, no entry will be selected. db_createlist($linkID,0,"select pull_id, pc from job_log_pull","Please select one ..."); </select> And here's the function: function db_createlist($linkID,$default,$query,$blank) { if($blank) { print("<option select value=\"0\">$blank</option>"); } $resultID = pg_exec($linkID,$query); $num = pg_numrows($resultID); for ($i=0;$i<$num;$i++) { $row = pg_fetch_row($resultID,$i); if($row[0]==$default)$dtext = "selected"; else $dtext = ""; print("<option $dtext value=\"$row[0]\">$row[1]</option>"); } } Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/ Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 what "doesn't work?" Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/#findComment-550194 Share on other sites More sharing options...
nashsaint Posted May 26, 2008 Author Share Posted May 26, 2008 the dropdown only displays "Please select one ..." and doesn't populate the entries from sql query. Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/#findComment-550198 Share on other sites More sharing options...
unidox Posted May 26, 2008 Share Posted May 26, 2008 use a while loop. $q = mysql_query() or die(mysql_error()); echo "<option value=\"blank\">Please select.</option>"; while ($r = mysql_fetch_array($q)) { echo "<option value=\"" . $r['id'] . "\">" . $r['name'] . "</option>"; } Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/#findComment-550204 Share on other sites More sharing options...
BlueSkyIS Posted May 26, 2008 Share Posted May 26, 2008 yes, you're using some non-standard functions that we'd need to see, e.g., pg_exec or change to standard mysql_ functions. Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/#findComment-550205 Share on other sites More sharing options...
nashsaint Posted May 26, 2008 Author Share Posted May 26, 2008 I tried unidox code but I couldn't get it to work. I tried another approach and it seems to be working but now I don't know how to capture the user's selection from the select, since this form is under another Form statement. Any help how to extract the user selection? Thanks. $pcquery = "SELECT pull_id, pc FROM job_log_pull"; $pcresult = @mysql_query($pcquery); echo '<form action=blah method=Post> <select name=category>'; // get resultset as object while ($pcrow = mysql_fetch_object($pcresult)) { echo "<option value=" . $pcrow->pull_id . ">" . $pcrow->pc . "</option><br>"; } echo '</select> </form>'; mysql_free_result($pcresult); Link to comment https://forums.phpfreaks.com/topic/107311-drop-down-from-sql-table/#findComment-550233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.