List, Menu, Drop-down list, combo box --- Values from DB

A

Anonymous

Guest
Guys, Its me again, I know How to have a list or menu, with option values from a table.

<select name="contact" id="select3">
<option></option>
<?php

$query = "SELECT * FROM contact";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC))
{
echo "<option value=\"{$row1['name']}\"> {$row1['name']} </option>\n";
}

?>
</select>

I am making an update record form, how can i display the option values with a specific value selected?

For example.

Subject: Math
Teacher: Ms. Smith

I want to edit the teacher and replace it with another person.
How can I display the record in a form using a list or menu dropdown
that has Ms. Smith as the default selected value?

Sorry if i cant explain it clearly.

Thanks.
 
In HTML, to make a SELECT item selected, you use the selected attribute:

Code:
<select name="contact">
   <option value="Me">Me</option>
   <option value="You" selected="selected">You</option>
</select>

In the above, "You" will be the default selected value. In a loop context, such as your database query, you have to have a condition where the "selected" attribute will be assigned. I'd do it like this:
PHP:
<?php
$selected = 'You'; // the value you want to use

$query = "SELECT * FROM contact";
$result = @mysql_query ($query);
while ($row = mysql_fetch_assoc($result)) {
   echo '<option value="' . $row['name'] . '"' . 
        $row['name'] == $selected ? 'selected="selected"' : '' .
        '>' . $row['name'] . "</option>\n";
}
?>
 
can i have the selected value, varry from whatever value is in the database?
 
I don't like a ternary expression in the middle of an echo :)
Code:
<?php 
$selected = 'You'; // the value you want to use 

$query = "SELECT * FROM contact"; 
$result = @mysql_query ($query); 
while ($row = mysql_fetch_assoc($result)) { 
$selected_text = ($row['name'] == $selected) ? ' selected="selected"' : '';
   echo '<option value="' . $row['name'] . '"' . $selected_text . '>' . $row ['name'] . "</option>\n"; 
} 
?>

planketa: You can define $selected to whatever you want it to be.
 
Redcircle said:
I don't like a ternary expression in the middle of an echo :)

I suppose it's not the best choice, especially since I'm the one always preaching about code legibility.

planketa, the selected value can come from wherever you want.
 
You really helped me guys! Thanks again.

What is the function of You, is it a text, how can I make it a selected value from a database

Heres the example table

Teachers Table
Id Name
1 Ms. Smith
2 Mr. Black
3 Mrs. Jackson

Heres the main table

Course Table
Id Subject Teacher Time
1 Math Ms. Smith 8-11
2 Science Mr. Black 9-10

Then I want to retrieve the record number 1 from the course table and edit it.

I want to display the value of the name field in a drop down list this will come from tha table teacher. And I want to have the data in the teacher field from the course table selected or is the default value in the drop down list.


I'm so sorry if my question is so stupid, I'm just a newbie. I wish i was like you guys.

Thanks
 
I don´t understand your question!!
Isn´t what the code above does!?
If you want do to have some values(from DB) as default in your <option>:
PHP:
<?php 
$selected = 'Ms. Smith';

$query = "SELECT * FROM teachers"; 
$result = @mysql_query ($query); 
echo '<select name="teachers">';
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['name'] . '"' . 
// Here, if found 'Ms. Smith', will be set as default in the drop down list!
       $row['name'] == $selected ? 'selected="selected"' : '' . '>' . $row['name'] . "</option>\n"; 
} 
echo '</select>';
?>
 
Ok, I think I should have a variable value not a constant value.

What the code does is that it checks if the value is = Ms. Smith

$selected = 'Ms.Smith'

This equation has a definite value, what if I have deleted Ms. Smith from the table.?

Can it be like this...

$selected = $row['teacher'];

and place the value Ms. Smith at the top of the selection.

I dont know how to explain it more, sorry if i'm so stupid...
 
Got you! Your right, but if you want to select a specific value(person) from the table it has to exists, right!? So, the easiest way is making $selected = 'Ms.Smith' !

Tell me if is this what you want:
You want to call a page with a form where you´ll update some data. And in your drop down list you want to select what you want to update... i´m i right!?
 
Back
Top