Jump to content

populating a listbox with field names


cljones81

Recommended Posts

  :) Hi Everyone:

 

I hope someone, can help me with this problem as its got me baffled:

 

What I am trying to create is a MySQL database administration page where the user will select a database from a combo box and the next combo box will be automatically populated with the field ids for that database e.g. Username and Password and not the associated data that each column contains.

 

The user will then enter the search criteria based on the selected item e.g. Username = Paul. This information will be stored into a variable contained within an SQL query e.g. $result = "select * from $dbname where $fieldid = $searchid"; which will be called as part of a PHP script when the submit button is clicked.

 

All of this I can do I just cannot think of how to get the combo box to be automatically populated with the field ids and not the associate contents ???

 

Any help would be greatly appreciated.

 

 

Link to comment
Share on other sites

Thank you for the reply, further too this, I have managed to echo out the field names of the selected database using this code:

 

$result = mysql_list_fields($database, $dbtable);
  $number_fields = mysql_num_fields ($result);

  if (!$result){
     echo "not result";
     return false;
  }
  echo "Table '$dbname' in database '$database'"; 
for ($index=0; $index < $number_fields; ++$index) {
   echo  mysql_field_name ($result, $index);
}

I have tried many ways of getting this information into a listbox for further selection by the user and none of them worked so please help me.

 

Once again thank you

Link to comment
Share on other sites

:) Hi I've been playing around with my code and I came up with this:

 

echo "Table '$dbname' in database '$database'"; 
for ($index=0; $index < $number_fields; ++$index) {
   $fields = mysql_field_name ($result, $index);
}
echo "<body text='#0033FF'>
<form name='form1' method='post' action=''>
  <label>
  <select name='select'>
    <option value='$fields'>$fields</option>
    <option value='$fields'>$fields</option>
    <option value='$fields'>$fields</option>
  </select>
  </label>
</form>";

 

Which will return the last element in the array and print it out as options, I tried this believing that it would print out the first element in the array $field[0], but instead it only printed out the first character, in the last element of the array.

 

My apologises, this may seem like a rather simple question, but my programming is a tad rusty but is there anyway, I can access and print out the first element and so on in the array.

 

Thank you

 

Link to comment
Share on other sites

You need to echo the <option></option> HTML tags within the for loop:

<?php

echo "Table '$dbname' in database '$database'";

echo "<body text=\"#0033FF\">
<form name=\"form1\" method=\"post\" action=\"\">
  <label>
    <select name=\"select\">\n";

for ($index=0; $index < $number_fields; $index++)
{
   $field = mysql_field_name ($result, $index);

   echo "      <option value=\"$field\">$field</option>\n"

}
echo '  </select>
  </label>
</form>';

?>

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.