<?php
# This include gives us a set of functions to manipulate id/name pairs in a database
# Specifically, we're using key_id and key_name.

# gaa, I have to get rid of the $link parameter... global it? where?
function fetch_id ($key_id, $key_name, $table, $link) {
  $name			= $table ."_name";
  $id			= $table ."_id";
  $fetch_id		= "SELECT $id, $name FROM $table WHERE $name = '$key_name'";
  $id_result	= mysql_query($fetch_id, $link);
  $key_row		= mysql_fetch_array($id_result, MYSQL_ASSOC);
  $key_id		= $key_row["$id"];
  return $key_id;
}

function fetch_name ($key_id, $key_name, $table, $link) {
  $name			= $table ."_name";
  $id			= $table ."_id";
  $fetch_name	= "SELECT $id, $name FROM $table WHERE $id = '$key_id'";
  $name_result	= mysql_query($fetch_name, $link);
  $key_row		= mysql_fetch_array($name_result, MYSQL_ASSOC);
  $key_name		= $key_row["$name"];
  return $key_name;

}

function dropdown_select_name ($key_name, $table, $link) {
echo "<b>key_name:</b> $key_name<br>\n";
echo "<b>size:</b> $size<br>\n";
echo "<b>table:</b> $table<br>\n";
echo "<b>link:</b> $link<br>\n";
  $name				= $table ."_name";
  $id				= $table ."_id";
  $select_name		= "SELECT $id, $name FROM $table ORDER BY $name";
  $select_result	= mysql_query($select_name, $link);
  echo "<select name=$name>\n";
  echo "<option>$key_name\n";
  while ( $select_row = mysql_fetch_array($select_result, MYSQL_ASSOC) ) {
    echo "<option>{$select_row["$name"]}\n";
  }
  echo "</select>";

}

function dropdown_select_id ($key_id, $table, $link) {
  #$name				= $table ."_name";
  $id				= $table ."_id";
  #$select_id		= "SELECT $id FROM $table ORDER BY $id'";
  $select_id		= "SELECT $id FROM $table ORDER BY $id";
  $select_result	= mysql_query($select_id, $link);
  echo "<select name=$id>\n";
  echo "<option>$key_id\n";
  while ( $select_row = mysql_fetch_array($select_result, MYSQL_ASSOC) ) {
    echo "<option>{$select_row["$id"]}\n";
  }
  echo "</select>";

}

?>
