<?php
/**
 * Capitales del mundo - preguntar1.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-11-28
 * @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('Preguntar 1', MENU_VOLVER);

$consulta = "SELECT COUNT(*) FROM $dbTabla";
$result = $db->query($consulta);
$numeroRegistros = $result->fetchColumn();

if (!$result) {
    print "<p>Error en la consulta.</p>\n";
} elseif ($numeroRegistros==0) {
    print "<p>No se ha creado todavía ningún registro.</p>\n";
} else {
    $numeroPregunta  = rand(1, $numeroRegistros);
    $consulta = "SELECT * FROM $dbTabla 
       WHERE id=$numeroPregunta";
    $result = $db->query($consulta);
    $valor = $result->fetch();
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";
    } else {
        print "<form action=\"preguntar2.php\" method=\"".FORM_METHOD."\">
  <table>
    <tbody>
      <tr>
        <td>¿Cuál es la capital de $valor[pais]?</td>
        <td><input type=\"text\" name=\"ciudad\" size=\"$tamCiudad\" "
        ."maxlength=\"$tamCiudad\" /></td>
      </tr>
    </tbody>
  </table>
  <p><input type=\"hidden\" name=\"id\" value=\"$valor[id]\" /></p>
  <p><input type=\"submit\" value=\"Comprobar\" /></p>
</form>\n";
    }
}
$db = NULL;
pie();
?>
