<?php
/**
 * Diccionario - buscar2.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-06-15
 * @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('Buscar 2', MENU_2, CABECERA_SIN_CURSOR);

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

if (isset($_REQUEST['definiciones'])) {
    $campo = "significado";
} else {
    $campo = "palabra";
}
    
$orden = 'ASC';

$consulta = "SELECT COUNT(*) FROM $dbTabla 
    WHERE $campo LIKE '%$termino%'";
$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 {
    $consulta = "SELECT DISTINCT palabra FROM $dbTabla
        WHERE $campo LIKE '%$termino%'
        ORDER BY palabra $orden ";
    $result = $db->query($consulta);
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";    
    } else {
        print "<p>Listado completo de definiciones:</p>\n<dl>\n";
        foreach ($result as $valor) {
            print "  <dt>$valor[palabra]</dt>\n";
            $consulta = "SELECT * FROM $dbTabla
              WHERE palabra='$valor[palabra]' 
              AND $campo LIKE '%$termino%'
              ORDER BY significado $orden";
            $result2 = $db->query($consulta);
            if (!$result2) {
                print "      <dd>Error en la consulta.</dd>\n";    
            } else {
                foreach ($result2 as $valor2) {
                    print "      <dd>$valor2[significado]</dd>\n";
                }
            }
        }
        print "</dl>\n";
    }
}
$db = NULL;
pie();
?>
