Jump to content

How to make options in a select box be values from a table.


Beast27

Recommended Posts

I have set up a form page with a select box of colleges to select. I want the "options" in the select box to be values taken from a field called "name" in a table called "colleges" and they should be ordered alphabetically. I also want the default selected option to be "none."

 

I have attached a picture to describe what i want. Please be detailed with the code. I am fairly new to php and mysql. Thank you.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Please be detailed with the code

 

Programming help forums don't write the code for your assignments. We help you with the code you are writing. Exactly what problem, error, specific question, or point did you get stuck at when you attempted to do this?

Link to comment
Share on other sites

I'll try my best to help you - but I'm not going to spoon feed you.

 

First thing I need to know is that are the tables in your SQL (college) already populated? By that, I mean do they already have data in them? Am I correct in thinking that you simply want to have them being placed from your SQL onto a nice organised table in which you can view on your browser?

 

:)

Link to comment
Share on other sites

I'll try my best to help you - but I'm not going to spoon feed you.

 

First thing I need to know is that are the tables in your SQL (college) already populated? By that, I mean do they already have data in them? Am I correct in thinking that you simply want to have them being placed from your SQL onto a nice organised table in which you can view on your browser?

 

:)

 

The tables in my SQL (college) are already populated. I have manually inserted those values. I want all the "name" fields from SQL (college) to appear as OPTIONS for a select box(alphabetically). The concept of the page as a whole is like this. There will be a list box titled "college" and the user will be able to select one of the colleges from this list box. Thats all there is to it. :)

 

I am guessing I need to call a query such as: SELECT name FROM college ORDER BY name ASC

 

Then i would have to put those values as options in my listbox. This is the code for my listbox

 

<select name="college">

<option>college 1<.option>

<option>college 2<.option>

<option>college 3<.option>

<option>college 4<.option>

etc...all the way till the LAST college in my SQL (college) table.

 

Im sorry for the confusion but if you have any more questions i will clarify them.

Link to comment
Share on other sites

<?

...

mysql cnx code

...

 

$sql="SELECT id, thing FROM table";

$result=mysql_query($sql);

 

$options="";

 

while ($row=mysql_fetch_array($result)) {

 

    $id=$row["id"];

    $thing=$row["thing"];

    $options.="<OPTION VALUE=\"$id\">".$thing;

}

?>

...

html code

...

 

<SELECT NAME=thing>

<OPTION VALUE=0>Choose

<?=$options?>

</SELECT>

...

Link to comment
Share on other sites

if my last post was confusing this may help

<?php
		$q = mysql_query("SELECT name FROM college ORDER BY name ASC");

        echo "<select name='college'>";

        while ($row = mysql_fetch_assoc($q)) {

          echo '<option value="'.$row['college'].'">'.$row['college'].'</option>';

        }

        echo "</select>";
	?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.