Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom php function dropdown list from mysql table
    primarykey
    data
    text
    <p>PHP noob with a challenge.</p> <p>I have a custom php function:</p> <pre><code>function country_dropDown(){ $dropDown = mysql_query("SELECT * FROM countries"); while ($record = mysql_fetch_array($dropDown)) { echo '&lt;option value="' . $record['id'] . '"&gt;' . $record['name'] . '&lt;/option&gt;'; } } </code></pre> <p>'countries' table has 2 fields = id, name.</p> <p>There are over 200 rows in this 'countries' table.</p> <p><em>I can call this function in a html form that requires user input to select from the list of available countries from the list like this.</em></p> <pre><code>&lt;select name="classID"&gt;&lt;?php country_dropDown() ?&gt;&lt;/select&gt; </code></pre> <p><strong>I have some questions I hope someone can help me with.</strong></p> <ol> <li><p>When I call this function inside a html form and look at the source code I can see all 249 countries as "select options". <strong>Is there a best practice or efficient method so it does not list all countries in the source code?</strong></p></li> <li><p>The drop down list always displays, "Afganistan" which is the first row in the 'countries' table. <strong>How can I modify my custom function so it does not show any value or display the text, "Select Country"?</strong></p></li> <li><p>I have another table called 'student' with 2 fields: studentName, countriesID. I have a html form that lets me edit a student's record. It populates the default value of the student's name into the input text field but <strong>how do I make the dropdown function 'selected' the student's 'countryID' from the row?</strong> It always shows, "Afganistan".</p></li> <li><p><strong>Is there a best practice method or more efficient way to approach this countries dropdown select rather than my custom function?</strong></p></li> </ol> <p>I'm grateful for any assistance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload