Jump to content

[SOLVED] Populating a drop-down


rawky1976

Recommended Posts

Hi all; hope you're well!!!! I found a script that displays the field names from a table in a drop-down but I want to show the values in that field.

 

I've tried to amend that script but think I've got the logic of the functions incorrect.

 

Thanks for any help you might be able to offer

 

<?php
$connection = mysql_connect("localhost","xxx","xxx");
$select_db = mysql_select_db("dbname", $connection);
//$fields = mysql_list_fields("dbname", "tablename", $connection);
//$columns = mysql_num_fields($fields);
$query = "select field from dbname.table ORDER BY field";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
$columns = mysql_fetch_array($result);
echo "System Name: <select name=Field>";
for ($i = 0; $i < $columns[$num_rows]; $i++) {
echo "<option value=$i>";
//echo mysql_field_name($fields, $i);
}
echo "</select>";
?>

Link to comment
https://forums.phpfreaks.com/topic/90059-solved-populating-a-drop-down/
Share on other sites

This should get you on track.  This assumes the value is the first field and the name is the 2nd.

 

echo "System Name: <select name=Field>";

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

 

echo "<option value={$row[0]}>";

echo $row[1];

echo "</option>";

}

echo "</select>";

 

Thanks, here it is: -

 

<?php

$connection = mysql_connect("localhost","user","pass");

$select_db = mysql_select_db("dbname", $connection);

//$fields = mysql_list_fields("dbname", "tablename", $connection);

//$columns = mysql_num_fields($fields);

$query = "select * from dbname.tablename ORDER BY fieldname";

$result = mysql_query($query) or die(mysql_error());

$num_rows = mysql_num_rows($result);

$columns = mysql_fetch_array($result);

echo "System Name: <select name=Field>";

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

 

echo "<option value={$row[4]}>";

echo $row[0];

echo "</option>";

}

echo "</select>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.