Database Forum
Web Database Register Members List Calendar FAQ

 
Go Back   Database Forum > Database Management > PHP & MySQL
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Question Populating drop down list from table columns
07-21-2009, 08:26 PM #1  

justintoo1
Junior Member

 
Status: Offline
Posts: 2
Join Date: Jul 2009

Hello,

I would like to populate a drop down list with values that are columns in a table.

I know a way to populate the rows from a table but not values from column names.

For example -- I have a table that contains the columns 'avail_mon' through 'avail_sun'. I would like to populate that list with the columns 'avail_mon' through 'avail_sun'. Is there a way to do that ?

Thanks for the help - Justin
Reply With Quote

Default
12-19-2009, 11:54 AM #2  

sarg
Member

 
Status: Offline
Posts: 59
Join Date: May 2005

HTML Code:
//Populate dropdown box with MySQL table fields

<? 
$connection = mysql_connect("localhost","user","pass"); 
$fields = mysql_list_fields("dbname", "table", $connection); 
$columns = mysql_num_fields($fields); 
echo "<form action=page_to_post_to.php method=POST><select name=Field>"; 
for ($i = 0; $i < $columns; $i++) { 
echo "<option value=$i>"; 
echo mysql_field_name($fields, $i); 
} 
echo "</select></form>"; 
?>
Edit these variables with your own
line 1> "localhost","user","pass"
line 2> "dbname", "table", $connection
line 4> Your page_to_post_to.php php file for processing the form data.

Cheers
Reply With Quote

Default
12-20-2009, 07:37 PM #3  

justintoo1
Junior Member

 
Status: Offline
Posts: 2
Join Date: Jul 2009

Hello, thanks for the reply!

I have come up with this so far...

It is a page with question - What avail? Then is drop down list with availmon-->availsun
next question -- What area? Then drop down with areas in a restaurant.

Then is a submit button in which I would like to create a query from the selected values from each of the drop down lists.

Code:
<?php

/* Requires the header and the header
   requires config.php */

require("header.php");


if($_POST['submit']) {
	$db  = mysql_connect($dbhost, $dbuser, $dbpassword);
	       mysql_select_db($dbdatabase, $db);
}
echo mysql_error();
?>


<br/>
<br/>
<div id="main">
<h3>Employee Search</h3>



<?

echo 	"<br>";

$fieldavail = mysql_list_fields("sch", "avail", $db);
$colavail = mysql_num_fields($fieldavail);
echo 	"<p>What day ?</p>";
echo 	"<form action='xxx.php' method='POST'><select name ='$avail'>";
	for ($i = 1; $i < $colavail; $i++) {
	echo "<option value=$i>";
	echo mysql_field_name($fieldavail, $i);
	}

echo "</select></form>";
echo "<br>";

$fieldarea = mysql_list_fields("sch", "area", $db);
$colarea = mysql_num_fields($fieldarea);
echo "<p>What area ?</p>";
echo "<form action=xxx.php method=POST><select name ='$area'>";
	for ($i = 1; $i < $colarea; $i++) {
	echo "<option value =$i>";
	echo mysql_field_name($fieldarea, $i);
	}
echo "</select></form>";
echo "<br><br>";
echo "<input type=\"submit\" name=\"submit\" value=\"Search employees\">";
?>
</select>
</form>
</div>
<!--
  <td></td>
  <td><input type="submit" name="submit" value="Search employees"></td>
-->
Now when /i click search i get nothing. I would atleast expect a notfound error since "xxx.php" does not exist!

do i need a <form> tags outside of my php?

can you help?!

-justin
Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 09:36 AM.

Powered by: vBulletin Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Database Forums

Help support our forum assistants and product development ->

go to top go to top