-2

PHP noob with a challenge.

I have a custom php function:

function country_dropDown(){
    $dropDown = mysql_query("SELECT * FROM countries");
        while ($record = mysql_fetch_array($dropDown)) {
            echo '<option value="' . $record['id'] . '">' . $record['name'] . '</option>';
        }
}

'countries' table has 2 fields = id, name.

There are over 200 rows in this 'countries' table.

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.

<select name="classID"><?php country_dropDown() ?></select>

I have some questions I hope someone can help me with.

  1. When I call this function inside a html form and look at the source code I can see all 249 countries as "select options". Is there a best practice or efficient method so it does not list all countries in the source code?

  2. The drop down list always displays, "Afganistan" which is the first row in the 'countries' table. How can I modify my custom function so it does not show any value or display the text, "Select Country"?

  3. 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 how do I make the dropdown function 'selected' the student's 'countryID' from the row? It always shows, "Afganistan".

  4. Is there a best practice method or more efficient way to approach this countries dropdown select rather than my custom function?

I'm grateful for any assistance.

  • What have you tried yourself? 1: No (or via JavaScript but that doesn't change anything) 2: add an empty option before your while loop. 3: add a 'selectedid' or something to your function and check for it in the while loop. 4: Symfony custom form fields? – putvande Nov 21 '13 at 12:03
  • Hi @putvande thanks for replying. I tried some code that now seems so so far out from correct syntax and I tried a few things after googling for answers and then I left this task for over a week. Next time I will post what I tried when I ask the question. – Peter Reginald Nov 21 '13 at 15:43

5 Answers5

3

1) No, not really - If it isn't in the source code then it won't get rendered in the page.

2 & 3) You can just add a 'Please select one' option before your countries. You need a variable that has the current 'countId' and test against it. If it is the same as the current one in the loop, add a selected attribute to that option:

$countryID = 32; //This will be pulled from the student's record prior to this code.
echo '<option value=0>Please select one</option>';
while ($record = mysql_fetch_array($dropDown)) {
    echo '<option value="' . $record['id'] . '" '.($countryID == $record['id'] ? 'selected' : '').'>' . $record['name'] . '</option>';
}

4) Not really. Apart from maybe using a database library that isn't deprecated.

Community
  • 1
  • 1
George
  • 36,413
  • 9
  • 66
  • 103
  • Thanks you for your help oGeez. You have helped me to understand the code. I have another question. – Peter Reginald Nov 21 '13 at 15:16
  • @PeterReginald No problem, go ahead and ask another. If you have got everything you needed, please accept the answer. – George Nov 21 '13 at 15:17
  • Thanks you for your help oGeez. You have helped me to understand the code. The $countryID is selected if I write this "$countryID = 32;" as you suggested but all my custom functions exist in a separate file (general.php) which I 'include' in the head of my form input file (form-input.php). If I have already pulled the value of the student's countries.id to $countryID like this, "$countryID = $_POST['countryID'];" by running a mysql_fetch_array then how can I get this $countryID's value into the custom function in my general.php file? – Peter Reginald Nov 21 '13 at 15:36
  • To get the global variable inside your function use `global $countryID`. [Documentation](http://php.net/manual/en/language.variables.scope.php) – George Nov 21 '13 at 15:37
  • I can't get the global variable to work. Could it be because I've included my general.php file into the form-input.php file? This is what I have so far. general.php function going_dropdown(){ global $countryID; // this should equal the same as the countryID from form-input.php echo ''; while ($record = mysql_fetch_array($dropDown)) { echo ''; } form-input.php include general.php $countryID = $_POST['countryID']; – Peter Reginald Nov 21 '13 at 16:18
  • Should be fine, as long as you are declaring `$countID` BEFORE the function is called. – George Nov 21 '13 at 16:20
  • My code looks messed up... I am declaring $countryID in a different file called input-form.php which includes the file general.php which contains my function. Will this still work? – Peter Reginald Nov 21 '13 at 16:26
  • Yes, that will work. Including a page means that the scripts are put together, to be processed as one. – George Nov 21 '13 at 16:27
  • 1
    YAY! I finally figured out what I was doing wrong and now I understand! Thank you for your patience and assistance! So happy right now. – Peter Reginald Nov 22 '13 at 00:36
0

Best practice #1:

<select name="classID">
    <option value="invalid">--- select a country:</option>
    <?php country_dropDown() ?></select>

Best practice #2:

...
echo '<option ' . 
         ($record['id']==$studentsLangId?' selected="selected" ':'' ) . 
      ' value="'.  $record['id'] . '">' . $record['name'] . '</option>';
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0
  1. I don't know whether I understand what you mean. If you want to access to the full list inside a select tag, it will always be displayed in the source code.

  2. Just add a new option element at the top of your list. For example:

    <select name="classID"> <option value="0">none</option> <?php country_dropDown() ?> </select>

  3. You have to get the id of the country associate to the user and use the "selected" attribute inside the option element when both id fields matched.

  4. Definitely. You MUST use PDO as mysql_query function has been declared obsolete and don't use * to fetch all the fields, write their names instead.

Eduardo Casas
  • 578
  • 4
  • 11
  • Hi @Eduardo, thank you for helping me as well. I've been coding for a couple of weeks now and I've heard of PDO a few times. Looks like I'll have to do some more reading on this. – Peter Reginald Nov 21 '13 at 15:45
  • Why _must_ someone use PDO? There's also `mysqli`. – Nathan Jan 08 '14 at 05:33
0
  1. No
  2. For displaying "Select Country" rather than your first row in db you can do it in 3 ways:

    2.a: write echo '<option value=0>Please Select Country</option>' before your while loop  
    
    2.b: make one entry in database (which should be the first row) as **"Select Country"**  
    
    2.c: <select name="classID"><option value=0>Please Select Country</option><?php country_dropDown() ?></select>  
    
  3. What have you tried for this?

  4. Not exactly.
user2936213
  • 1,021
  • 1
  • 8
  • 19
-2
  1. No
  2. Just create an empty select tag before your "While"

    <option value="">Select</option>
  3. You should include an if operator in the while method, so if the user's country equals the current country in the while loop, then add an extra selected="selected" inside your "" tag.

  4. This is the way I do it too
Aris Μπ.
  • 67
  • 2
  • 10