DB dropdowns

Tags

Making the select tag from DB entities

The options we just saw were written into the HTML page. However, often the list of options comes from a database.

Let's see how we'd make a dropdown listing the comedians. It will look like this to users:

Output

As comedians are added and removed from the DB, the list will change automatically. W00t!

Our PHP makes HTML to make this list. Here's the HTML the PHP outputs.

  • <label>Who is the best comedian?<br>
  •     <select name="best-comedian">
  •         <option value="0">(Not given)</option>
  •         <option value='1'>Aisling Bea</option>
  •         <option value='9'>David Mitchell</option>
  •         <option value='10'>Eddie Izzard</option>
  •         <option value='11'>Frankie Boyle</option>
  •         <option value='5'>Isy Suttie</option>
  •         <option value='7'>Jimmy Carr</option>
  •         <option value='8'>Joe Wilkinson</option>
  •         <option value='2'>Paula Poundstone</option>
  •         <option value='4'>Roisin Conaty</option>
  •         <option value='12'>Russel Howard</option>
  •         <option value='3'>Sara Pascoe</option>
  •         <option value='6'>Sarah Millican</option>
  •         <option value='15'>Trevor Noah</option>
  •     </select>
  • </label>

Let's check the PHP that makes this HTML.

  1. <?php
  2. // Connect to the database.
  3. require_once 'library/db-connect.php';
  4. // Load comedians.
  5. $sql = "
  6.     SELECT *
  7.     FROM comedians
  8.     ORDER BY name;";
  9. /** @var PDO $dbConnection */
  10. $stmnt = $dbConnection->prepare($sql);
  11. $stmnt->execute();
  12. $comedianRows = $stmnt->fetchAll();
  13. ?><!doctype html>
  14. <html lang="en">
  15.     <head>
  16.         <meta charset="UTF-8">
  17.         <title>Best comedian</title>
  18.         <link rel="stylesheet" href="library/styles.css">
  19.     </head>
  20.     <body>
  21.         <h1>Best comedian</h1>
  22.         <form method="post">
  23.             <p>
  24.                 <label>Who is the best comedian?<br>
  25.                     <select name="best-comedian">
  26.                         <option value="0">(Not given)</option>
  27.                         <?php
  28.                         foreach ($comedianRows as $comedianRow) {
  29.                             $comedianId = $comedianRow['comedian_id'];
  30.                             $comedianName = $comedianRow['name'];
  31.                             print "<option value='$comedianId'>$comedianName</option>\n";
  32.                         }
  33.                         ?>
  34.                     </select>
  35.                 </label>
  36.             </p>
  37.         </form>
  38.     </body>
  39. </html>

The SQL puts all the comedians into the array $comedianRows (line 12):

  • $comedianRows = $stmnt->fetchAll();

Then we loop over the rows, making an option for each one:

  • <select name="best-comedian">
  •   <option value="0">(Not given)</option>
  •     <?php
  •     foreach ($comedianRows as $comedianRow) {
  •       $comedianId = $comedianRow['comedian_id'];
  •       $comedianName = $comedianRow['name'];
  •       print "<option value='$comedianId'>$comedianName</option>\n";
  •     }
  •     ?>
  • </select>
Georgina
Georgina

Cool!

Aye, it is.

Best comedian: add versus edit

When adding a record, the best comedian won't have anything selected:

Nothing selected for add

But when editing a record, there will be an existing value.

Comedian selected for edit

BTW, Aisling is a fave at the Mathiesons.

You can show the existing value, by adding adding checked to the right option, as your code makes the HTML.

Here's some code to add to the above for that. Assume $bestComedianId has an entity's existing selection.

  1. <?php
  2. foreach ($comedianRows as $comedianRow) {
  3.     $comedianId = $comedianRow['comedian_id'];
  4.     $comedianName = $comedianRow['name'];
  5.     $selected = '';
  6.     if ($comedianId == $bestComedianId) {
  7.         $selected = 'selected';
  8.     }
  9.     print "<option value='$comedianId' $selected>$comedianName</option>\n";
  10. }
  11. ?>

The variable $selected is always output (line 9). Each time through the loop, $selected is set to MT. When the option's id matches the current one for the entity, then $selected is set to to the text selected.

Up next

The comedians select list, with the current value selected, shows how we represent relationships on forms. Let's see more of that.