Jump to content

drop down list being selected from a database


tawevolution

Recommended Posts

Hey. Currently I have an 'add' for which has a drop down list of 3 languages, which when submitted is added to a database (this bit works). What I want to do is have an 'edit' form which looks at the database, sees what language it is, and then automatically selects the right language in the drop down box automatically. at the moment my edit form has the drop down box (like the add) but next to it i have to have text saying what it WAS.

hope this is explained well, cus i easily confuse myself and others xD
evo
OK you have an sql selecting your data. I do not know what your table structure is like so I will say the langues field is called "language"

[code]<?php
// Select date from database
$sql = "SELECT * FROM table WHERE id = '$id'";
  $res = mysql_query($sql) or die (mysql_error());
    $r = mysql_fetch_assoc($res);
// create language array
$lang = array("English", "French", "Italian");
// start dropdown manu
echo "<select name=lang>";
// Loop through language array
  foreach($lang as $language){
  if($language = $r['language']){
  echo "<option value=\"$language\" selected>$language</option>";
  } else {
  echo "<option value=\"$language\">$language</option>";
  }
  }
  echo "</select>";
?>[/code]

Ray

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.