masgas Posted August 15, 2006 Share Posted August 15, 2006 Hi again!I am paginating some mysql results, but I can't get it to work... If I write:$resultados = mysql_query("SELECT * FROM articulos WHERE visible = 1 AND sexo LIKE '%$sexo%' LIMIT $inicio, $registros");seems to work ok. BUT if I add another AND and the query is like this:$resultados = mysql_query("SELECT * FROM articulos WHERE visible = 1 AND sexo LIKE '%$sexo%' AND precio <= '$precio' LIMIT $inicio, $registros"); then the pagination doesn't work, it just shows the pages available but when you click on them no results are printed...Is there a problem with adding AND many times to a single query?Thanks a lot! Quote Link to comment Share on other sites More sharing options...
SharkBait Posted August 15, 2006 Share Posted August 15, 2006 Shouldnt be any problems with adding multiple ANDs in a query statement.Though you are making the result more exact when you keep adding ANDs. How many entries is it returning to you?How many items to you display on each page? Quote Link to comment Share on other sites More sharing options...
masgas Posted August 15, 2006 Author Share Posted August 15, 2006 it seems to select correctly because it gives me a 1 out of 17 pages which is more or less the amount of pages there should be! And it limits the prints to 4 which is the amount I have limited the page to do.$prints = 4; // [color=green]Limit the prints to 4[/color]if (!$page) { $start = 0; $page = 1; } else { $start = ($page - 1) * $prints; } $results = mysql_query("SELECT id FROM articulos WHERE sexo LIKE '%$sexo%' AND precio <= '$precio' and visible = 1");$total_prints = mysql_num_rows($results); $results = mysql_query("SELECT * FROM articulos WHERE sexo LIKE '%$sexo%' and visible = 1 AND precio <= '$precio' LIMIT $start, $prints"); $total_pages = ceil($total_prints / $prints);while($articulo=mysql_fetch_array($results)) { echo "<b>".$articulo["direccion"]."</b><br>"; echo "<b>".$articulo["user"]."</b><br>"; echo "<b>".$articulo["m2"]."</b><br>"; echo "<b>".$articulo["totab"]."</b><br>";}if(($page - 1) > 0) { echo "<a href='paginara.php?page=".($page-1)."'>Back</a> ";}for ($i=1; $i<=$total_pages; $i++){ if ($page == $i) echo "<b>".$page."</b> "; else echo "<a href='paginara.php?page=$i'>$i</a> "; }if(($page + 1)<=$total_pages) { echo " <a href='paginara.php?page=".($page+1)."'>Next</a>";}Any guess where the error might be? thanks! Quote Link to comment Share on other sites More sharing options...
masgas Posted August 15, 2006 Author Share Posted August 15, 2006 help please, thanks Quote Link to comment Share on other sites More sharing options...
masgas Posted August 15, 2006 Author Share Posted August 15, 2006 :[quote author=masgas link=topic=104371.msg416320#msg416320 date=1155663488]help please, thanks[/quote]:) Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 15, 2006 Share Posted August 15, 2006 have you forgotten to pass $precio after the page is reloaded?? Quote Link to comment Share on other sites More sharing options...
otuatail Posted August 15, 2006 Share Posted August 15, 2006 A good idea is to save the sql in a string first. Then use the string. If there is a problem then you could coment out the execution and insert an echo statment. You can now look at the statment in detail to see were things are wrong. good idea to use a sting firs.Des. Quote Link to comment Share on other sites More sharing options...
masgas Posted August 17, 2006 Author Share Posted August 17, 2006 Excellent work! (yours of course!) cause I had forgotten to pass the variables over the pages so it looks like this now, and it works:$registros = 4;if (!$pagina) { $inicio = 0; $pagina = 1; } else { $inicio = ($pagina - 1) * $registros; } //$res = mysql_query ("SELECT * FROM articulos WHERE sexo LIKE '%$sexo%' AND precio <= '$precio' ORDER BY precio");$resultados = mysql_query("SELECT * FROM articulos WHERE visible = '1' AND sexo LIKE '%$sexo%' AND precio <= '$precio'");$total_registros = mysql_num_rows($resultados); $resultados = mysql_query("SELECT * FROM articulos WHERE visible != '' AND sexo LIKE '%$sexo%' AND precio <= '$precio' LIMIT $inicio, $registros"); $total_paginas = ceil($total_registros / $registros);//sexo LIKE '%$sexo%' AND precio <= '$precio'" //ORDER BY precio //id, direccion, user, imagen, m2, habitaciones, totab, otros, email, telefono, precio, alquilarif($total_registros) {while($articulo=mysql_fetch_array($resultados)) { echo "<b>".$articulo["direccion"]."</b><br>"; echo "<b>".$articulo["user"]."</b><br>"; echo "<b>".$articulo["m2"]."</b><br>"; echo "<b>".$articulo["totab"]."</b><br>";}} else { echo "<font color='darkgray'>(sin resultados)</font>"; } mysql_free_result($resultados); if($total_registros) {if(($pagina - 1) > 0) { echo "<a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=".($pagina-1)."'>Anterior</a> ";}for ($i=1; $i<=$total_paginas; $i++){ if ($pagina == $i) echo "<b>".$pagina."</b> "; else echo "<a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=$i'>$i</a> "; }if(($pagina + 1)<=$total_paginas) { echo " <a href='paginara.php?precio=$precio&&sexo=$sexo&&pagina=".($pagina+1)."'>Siguiente</a>";}}thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.