Jump to content

Archived

This topic is now archived and is closed to further replies.

masgas

SELECT from AND AND AND

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
:[quote author=masgas link=topic=104371.msg416320#msg416320 date=1155663488]
help please, thanks
[/quote]:)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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, alquilar
if($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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.