Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mostrarInfo(){
- var categoria=document.getElementById('categoria').value;
- window.location= 'buscadorNoticias.php?categoria='+ categoria;
- }
- </script>
- </head>
- <body>
- <h2>Bucador de Noticias</h2>
- <?php
- //REALIZO LA CONEXIÓN
- $db_host="localhost";
- $db_user="root";
- $db_pass="";
- $db_name="lindavista";
- $conexion=mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die("No se pudo conectar a la BBDD");
- $conexion->query("SET NAMES 'UTF8'");
- //LA FORMA CORTA DE HACERLO
- $categoria=$_GET['categoria'];
- if($categoria=="todo"){
- $instruccion1="SELECT * FROM noticias";
- $consulta1=mysqli_query($conexion, $instruccion1);
- echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
- while($fila=mysqli_fetch_array($consulta1)){
- echo "<tr><td>" . $fila['titulo'] . "</td>";
- echo "<td>" . $fila['texto'] . "</td>";
- echo "<td>" . $fila['categoria'] . "</td></tr>";
- if(empty($fila['imagen'])){
- echo "<td></td>";
- }else{
- echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
- }
- }
- }else{
- $instruccion1="SELECT * FROM noticias WHERE categoria='$categoria'"; //no funcionaba al principio con promociones ya que en el value del select tenia "promos"(que es un nombre distinto al que aparece en la bbdd)
- $consulta1=mysqli_query($conexion, $instruccion1);
- echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
- while($fila=mysqli_fetch_array($consulta1)){
- echo "<tr><td>" . $fila['titulo'] . "</td>";
- echo "<td>" . $fila['texto'] . "</td>";
- echo "<td>" . $fila['categoria'] . "</td></tr>";
- if(empty($fila['imagen'])){
- echo "<td></td>";
- }else{
- echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
- }
- }
- }
- ?>
- <!--CREO LA SELECCIÓN-->
- <form action='<?php echo $_SERVER['PHP_SELF']?>' method='GET'> <!--ENCTYPE='multipart/form-data'-->
- <select id="categoria" name='categoria' onchange='mostrarInfo()'>
- <option value='todo'>Todo</option>
- <!--while($fila=mysqli_fetch_array($consulta)){
- echo "<option value='" . $fila['categoria'] . "'>".$fila['categoria']."</option>";
- }-->
- <option value='promociones'>Promociones</option>;
- <option value='costas'>Costas</option>
- <option value='ofertas'>Ofertas</option>
- </select>
- </form>
- <?php
- $instruccion="SELECT * FROM noticias";//Realizo la consulta para sacar todas las noticias que hay en mi BBDD
- $consulta=mysqli_query($conexion, $instruccion);
- echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
- while($fila=mysqli_fetch_array($consulta)){
- echo "<tr><td>" . $fila['titulo'] . "</td>";
- echo "<td>" . $fila['texto'] . "</td>";
- echo "<td>" . $fila['categoria'] . "</td></tr>";
- if(empty($fila['imagen'])){
- echo "<td></td>";
- }else{
- echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
- }
- }
- echo "</table>";
- ?>
- </body>
Add Comment
Please, Sign In to add comment