Guest User

Untitled

a guest
Dec 2nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. function mostrarInfo(){
  2.  
  3. var categoria=document.getElementById('categoria').value;
  4. window.location= 'buscadorNoticias.php?categoria='+ categoria;
  5. }
  6. </script>
  7. </head>
  8. <body>
  9. <h2>Bucador de Noticias</h2>
  10. <?php
  11. //REALIZO LA CONEXIÓN
  12. $db_host="localhost";
  13. $db_user="root";
  14. $db_pass="";
  15. $db_name="lindavista";
  16.  
  17.  
  18. $conexion=mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die("No se pudo conectar a la BBDD");
  19. $conexion->query("SET NAMES 'UTF8'");
  20.  
  21. //LA FORMA CORTA DE HACERLO
  22.  
  23. $categoria=$_GET['categoria'];
  24.  
  25. if($categoria=="todo"){
  26. $instruccion1="SELECT * FROM noticias";
  27. $consulta1=mysqli_query($conexion, $instruccion1);
  28.  
  29. echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
  30. while($fila=mysqli_fetch_array($consulta1)){
  31.  
  32. echo "<tr><td>" . $fila['titulo'] . "</td>";
  33. echo "<td>" . $fila['texto'] . "</td>";
  34. echo "<td>" . $fila['categoria'] . "</td></tr>";
  35. if(empty($fila['imagen'])){
  36. echo "<td></td>";
  37. }else{
  38. echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
  39.  
  40. }
  41. }
  42. }else{
  43. $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)
  44. $consulta1=mysqli_query($conexion, $instruccion1);
  45.  
  46. echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
  47. while($fila=mysqli_fetch_array($consulta1)){
  48.  
  49. echo "<tr><td>" . $fila['titulo'] . "</td>";
  50. echo "<td>" . $fila['texto'] . "</td>";
  51. echo "<td>" . $fila['categoria'] . "</td></tr>";
  52. if(empty($fila['imagen'])){
  53. echo "<td></td>";
  54. }else{
  55. echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
  56.  
  57. }
  58. }
  59. }
  60. ?>
  61.  
  62. <!--CREO LA SELECCIÓN-->
  63. <form action='<?php echo $_SERVER['PHP_SELF']?>' method='GET'> <!--ENCTYPE='multipart/form-data'-->
  64. <select id="categoria" name='categoria' onchange='mostrarInfo()'>
  65. <option value='todo'>Todo</option>
  66. <!--while($fila=mysqli_fetch_array($consulta)){
  67. echo "<option value='" . $fila['categoria'] . "'>".$fila['categoria']."</option>";
  68. }-->
  69. <option value='promociones'>Promociones</option>;
  70. <option value='costas'>Costas</option>
  71. <option value='ofertas'>Ofertas</option>
  72. </select>
  73. </form>
  74.  
  75. <?php
  76. $instruccion="SELECT * FROM noticias";//Realizo la consulta para sacar todas las noticias que hay en mi BBDD
  77. $consulta=mysqli_query($conexion, $instruccion);
  78.  
  79. echo "<table><tr><th>Título</th><th>Noticia</th><th>Categoría</th><th>Imagenes</th></tr>";
  80. while($fila=mysqli_fetch_array($consulta)){
  81.  
  82. echo "<tr><td>" . $fila['titulo'] . "</td>";
  83. echo "<td>" . $fila['texto'] . "</td>";
  84. echo "<td>" . $fila['categoria'] . "</td></tr>";
  85. if(empty($fila['imagen'])){
  86. echo "<td></td>";
  87. }else{
  88. echo "<td><a href='img/" .$fila["imagen"]. "'><img src='ico-fichero.gif'></a></td>";
  89.  
  90. }
  91. }
  92. echo "</table>";
  93. ?>
  94. </body>
Add Comment
Please, Sign In to add comment