By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

populating dropdown list with column names?

P: 16
I keep getting problems with this. What i want to do is populate my dropdown list with the names of the columns in one of my database tables. Whatever i try to do i keep somehow filling it in with like 5 Array's or 5 Object's. Code is below and any help is greatly appreciated.

$query33 = "SHOW COLUMNS FROM $January";
$result33 = mysql_query($query33);

<select name='os1'>
<?php
if($result33){
while($row = mysql_fetch_array($result33)){
echo "<option value='$row'>".$row."</option>";
}
}
?>
</select>
Dec 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: 41
I keep getting problems with this. What i want to do is populate my dropdown list with the names of the columns in one of my database tables. Whatever i try to do i keep somehow filling it in with like 5 Array's or 5 Object's. Code is below and any help is greatly appreciated.

$query33 = "SHOW COLUMNS FROM $January";
$result33 = mysql_query($query33);

<select name='os1'>
<?php
if($result33){
while($row = mysql_fetch_array($result33)){
echo "<option value='$row'>".$row."</option>";
}
}
?>
</select>
the $row that you extracted from the mysql result has a lot more information than you realize. It not only contains the column title, but also what kind of row it is, if its a PRIMARY KEY, or UNIQUE, a bunch of stuff. You need to change1 line to your code to make this work out the way you want it to.

[PHP]
echo "<option value=\"{$row['Field']}\">".$row['Field']."</option>";
[/PHP]

You may want to check out what the $row array includes as well so you know what you are working with.

[PHP]
$result = $mysql_query($query);
while ($row = mysql_fetch_array($result)) {
print_r($row);
}

[/PHP]

This will give you a good idea of what is inside the array so you know how to properly reference it next time

hope this helps
Dec 10 '07 #2

P: 16
That worked perfectly. Thank you. One more thing though that should be easily solved, yet I am still have difficulty with....

I want to take this a step further and display certain columns when put under a specified query... a random example would be easiest to see what i mean...

January
Day Ticket Payment Guest
1 yes no yes
2 no yes yes

If i wanted the dropdown list to show only Payment when it is 'no' on Day 1.
(The dropdown list would only have 'Payment' in it based only from the Day 1 row).
Dec 10 '07 #3

P: 41
That worked perfectly. Thank you. One more thing though that should be easily solved, yet I am still have difficulty with....

I want to take this a step further and display certain columns when put under a specified query... a random example would be easiest to see what i mean...

January
Day Ticket Payment Guest
1 yes no yes
2 no yes yes

If i wanted the dropdown list to show only Payment when it is 'no' on Day 1.
(The dropdown list would only have 'Payment' in it based only from the Day 1 row).
You basically have to find out this information by sending a query to the database

SELECT payment FROM table WHERE payment = 'no' AND day = 1

then you test how many rows you got in your mysql result with the mysql_num_rows function. If it is equal to 1 then you have a match and you know that you show display it. If it is 0 then you shouldn't display it.

something like that
Dec 10 '07 #4

P: 16
it should be one row no matter what if there are unique Days (such as Day 1).
I'm just lookin to see if it's possible to display not all the column names at once, but rather specificy (maybe not even from a query--i dont really know) when a column has a certain value within a certain row and to select that certain column. maybe i shouldn't show all the columns at first and maybe there is another way to displaying column names with specifications? hope i'm not confusing too much.
Dec 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.