<?php
/**
 * Citas - 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-05-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('Buscar 2', MENU_2, CABECERA_SIN_CURSOR);

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

$campo = recogeParaConsulta($db, 'campo', 'autor'); 
$campo = quitaComillasExteriores($campo); 
$orden = recogeParaConsulta($db, 'orden', 'ASC'); 
$orden = quitaComillasExteriores($orden);

$consulta = "SELECT COUNT(*) FROM $dbTabla 
    WHERE autor LIKE '%$texto%'
    OR cita LIKE '%$texto%'";
$result = $db->query($consulta);
if (!$result) {
    print "<p>Error en la consulta.</p>\n";
} elseif ($result->fetchColumn()==0) {
    print "<p>No se han encontrado registros.</p>\n";
} else {
    $consulta = "SELECT * FROM $dbTabla 
        WHERE autor LIKE '%$texto%'
        OR cita LIKE '%$texto%'
        ORDER BY $campo $orden";
    $result = $db->query($consulta);
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";
    } else {
        $datos = "texto=$texto&amp;campo";
        print "<p>Registros encontrados:</p>\n<table border=\"1\">
  <thead>
    <tr class=\"neg\">
      <th><a href=\"$_SERVER[PHP_SELF]?$datos=autor&amp;orden=ASC\">
        <img src=\"abajo.png\" alt=\"A-Z\" title=\"A-Z\" /></a>
        Nombre
        <a href=\"$_SERVER[PHP_SELF]?$datos=autor&amp;orden=DESC\">
        <img src=\"arriba.png\" alt=\"Z-A\" title=\"Z-A\" /></a></th>
      <th><a href=\"$_SERVER[PHP_SELF]?$datos=cita&amp;orden=ASC\">
        <img src=\"abajo.png\" alt=\"A-Z\" title=\"A-Z\" /></a>
        Apellidos 
        <a href=\"$_SERVER[PHP_SELF]?$datos=cita&amp;orden=DESC\">
        <img src=\"arriba.png\" alt=\"Z-A\" title=\"Z-A\" /></a></th>
        </tr>
  </thead>
  <tbody>\n";
        foreach ($result as $valor) {
            print "    <tr>
      <td>$valor[autor]</td>
      <td>$valor[cita]</td>
    </tr>\n";
        }
        print "  </tbody>\n</table>\n";
    }
}
$db = NULL;  
pie();
?>
