<?php
/**
 * Convertidor de unidades - convertir2.php
 * 
 * @author    Bartolomé Sintes Marco <bartolome.sintes+mclibre@gmail.com>
 * @copyright 2011 Bartolomé Sintes Marco
 * @license   http://www.gnu.org/licenses/agpl.txt AGPL 3 or later
 * @version   2011-08-31
 * @link      http://www.mclibre.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

include('funciones.php');
$db = conectaDb();
cabecera('Convertir 2', MENU_2, CABECERA_CON_CURSOR);

$unidad1 = recogeParaConsulta($db, 'unidad1');
$unidad1 = quitaComillasExteriores($unidad1);

$orden = 'ASC';

$consulta = "SELECT COUNT(*) FROM $dbTabla 
    WHERE unidad1='$unidad1'";
$result = $db->query($consulta);
if (!$result) {
    print "<p>Error en la consulta.</p>\n";
} elseif ($result->fetchColumn()==0) {
    print "<p>No se ha creado todavía ningún registro.</p>\n";
} else {
        print "<form action=\"convertir3.php\" method=\"".FORM_METHOD."\">
  <p>Escriba la cantidad y la unidad a convertir:</p>
  <p><input type=\"text\" name=\"numero\" size=\"$tamValor\" "
        ."maxlength=\"$tamValor\" id=\"cursor\" /> $unidad1
     <input type=\"hidden\" name='unidad1' value='$unidad1' /></p>
  <table>
    <tbody>
      \n";
    $consulta = "SELECT DISTINCT unidad2 FROM $dbTabla
        WHERE unidad1='$unidad1' 
        ORDER BY unidad1 $orden ";
    $result2 = $db->query($consulta);
    if (!$result2) {
        print "<p>Error en la consulta.</p>\n";    
    } else {
        foreach ($result2 as $valor) {
            print "    <tr>
      <td><input type=\"radio\" value=\"$valor[unidad2]\" name=\"unidad2\" /></td>
      <td>$valor[unidad2]</td>
    </tr>\n";
        }
    print "    </tbody>
  </table>
  <p><input type=\"submit\" value=\"Siguiente\" /></p>
</form>\n";
    }
}
$db = NULL;
pie();
?>
