Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I currently have this line in one of my php pages:-
<td>
<?php include("inc/movementsin.php");?>
</td>inc/movementsin.php contains:-
<?php
print"<SELECT NAME=\"movementin\">
<OPTION VALUE=\"$row[movementin]\" SELECTED>$row[movementin]
<OPTION VALUE=\"Bought\">Bought
<OPTION VALUE=\"Born\">Born
<OPTION VALUE=\"Found\">Found
<OPTION VALUE=\"Borrowed\">Borrowed
</SELECT>";
?>What I want to do now is to replace this with the appropriate code so instead of taking the values from this "hard coded" list is to take the values from a table (already set up and only has 1 field "flocknumbers") which contains the values.
So, basically it's :-
<td>
<?php include "table-flocknumbers";?>
</td>How do I code this please ?
Thanks
Mike

<?php
//Connect to mysql
$db = mysql_connect("localhost", "username", "password") or die(mysql_error());//Select the database
mysql_select_db("databasename",$db) or die(mysql_error());//Run query
$query = "SELECT * FROM table-flocknumbers";
$result = mysql_query($query) or die(mysql_error());//Determine if there were any results
if (!mysql_num_rows($result)) {echo "There were no results.";
} else { //There were results
//Create the select list
echo "<SELECT NAME=\"movementin\">\n";//Iterate through the results and create the options
while ($row = mysql_fetch_assoc($result)) {echo "<OPTION VALUE=\"$row[flocknumbers]\">$row[flocknumbers]</option>\n";
}
//Close the select list
echo "</SELECT>\n";}
?>
Michael J

Thanks both,
I'll give that a try later and let you know. I think I can strip some line out as I should already be connected to the db/host and I know there will be results.
Mike

Perfect, I've stripped it down to this. Thanks again.
<?php
include ("inc/table.php");
$query = "SELECT * FROM flock_numbers";
$result = mysql_query($query) or die(mysql_error());
echo "<SELECT NAME=\"flock_no\">\n";
while ($row = mysql_fetch_assoc($result)) {
echo "<OPTION VALUE=\"$row[flock_no]\">$row[flock_no]</option>\n";
}
echo "</SELECT>\n";
?>

Hi again, another question..
I've replaced the SELECT to look at the main table and extract DISTINCT flock_no... rather than keeping them in a separate table.
$query = "SELECT DISTINCT(flock_no) FROM flock";
That works great and gives me a list of Flock Numbers which I already have used in the main table.
My next question is... is there a way to add a new Flock Number using this/similar code?
Years ago I used MS Access (yeuch) to do something similar - there was a "Limit to List = No" option if I recall.

If I unedrstand you correctly you are wanting a select list where the user can add a value on-the-fly by typing it in to the select list. That is not possible with any HTML technology.
Well, not without a LOT of work-arounds. There was a javascript I saw one that combined a select list and a text field to give that functionality - but it cost money.
Here is a possible solution. 1) Create a text box with the name that the select list has now and change the select list name to something else. The page receiving the form will get the value from the text box. Then use an onchange event on the select list and use it to populate the text box. Then add an option to the end of the selefct list for "Other".
When the user selects any value except other, the value is populated into the text field and the text field is hidden. When the user selects "Other" the field is emptied and becomes visible.
Here's an example:
<html>
<head><script type="text/javascript">
function setFlockNo(selValue) {
document.getElementById('flock_no').value = selValue;
displayVal = (selValue!='')?'none':'';
document.getElementById('flock_no_span').style.display = displayVal;}
</script>
</head>
<body><form name="test">
Flock No:
<select name="flock_no_select" onchange="setFlockNo(this.value)">
<option value="Option A from DB">Option A from DB</option>
<option value="Option B from DB">Option B from DB</option>
<option value="Option C from DB">Option C from DB</option>
<option value="">Other</option>
</select><span style="display:none;" id="flock_no_span">
<input type="text" name="flock_no" id="flock_no">
</span>
</form></body>
</html>
Michael J

Thanks Michael J,
I thought it would be a bit awkward - if not impossible. I'll keep a copy of the code you've supplied and look at it at a later date as I'm just beginning to learn the basics of PHP/MySQL at the moment.
Thanks again for all your assistance with this.
Mike

Hi, me again (struggling as usual), I have another...
I've created this to extract data from a table and display all in a dropdown box. I want to use this to only add the selected "id" into a field named "parent_id"
So, which bit do I have to change ?
Many thanks.
<?php
$query = "SELECT id, petname, flock_no, ind_no FROM flock ORDER BY petname";
$result = mysql_query($query) or die(mysql_error());
echo "<SELECT NAME=\"id\",\"petname\",\"flock_no\",\"ind_no\">\n";
while ($row = mysql_fetch_assoc($result)) {
echo "<OPTION VALUE=\"id\",\"$row[petname]\",\"flock_no\">$row[id] $row[petname] ($row[flock_no]-$row[ind_no])</option>\n";
}echo "</SELECT>\n";
?>

First of all, you should make new posts for new issues.
As to the code above and your query, I do not understand.
If you have a select field with the IDs why do you need to populate the selected ID into another field? Also your select field and option statements above has a very malformed name. What are you trying to accomplish by having comma separated values as the name/values? If it works at all it will only recognise the first value for each.
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |