Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/04/2011, 15:09
luisdelbar
 
Fecha de Ingreso: marzo-2009
Mensajes: 61
Antigüedad: 15 años, 10 meses
Puntos: 2
Pregunta Problema con Paginación de resultados, Buscador PHP - MYSQL

Hola amigos, les cuento:

Tengo una base de datos MySql, y se realizan consultas a través de php, prácticamente es un buscador, ya me muestra resultados y todo está perfecto como pueden ver aqui: http://www.yellowstein.com/search.php?query=steel&submit=Search

El único detalle es la paginación, aunque la realiza correctamente, calcula correctamente el número de páginas en base a los resultados, el único problema es que no funciona correctamente al hacer click.

Como pueden ver, si intentan dar click en la siguiente página o en cualquiera, aunque la URL si cambia en función de la página que debe mostrar, es decir si cambia de página, en realidad permanecen los mismos resultados y todo, igual, no me muestra los 10 siguientes resultados... les dejó mi código completo, la página completa, por si alguien puede ayudarme y decirme porque sucede eso, se los agradeceré infinitamente.



Código:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<?php

$db_addr = 'host';		// address of MySQL server.
$db_user = 'user';		// Username to access server.
$db_pass = 'pass';		// Password access server.
$db_name = 'database';	// Name of database to connect to.
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");

$query = $_GET['query'];

if (!($connect)) // If no connect, error and exit().
{
     echo("<p>Unable to connect to the database server.</p>");
     exit();
}

if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
{
     echo("<p>Unable to locate the $db_name database.</p>");
     exit();
}

if (!($limit)){
     $limit = 10;} // Default results per-page.

if (!($page)){
     $page = 0;} // Default page value.

$numresults = mysql_query("SELECT * FROM 986_first_table WHERE TITLETXT LIKE '%$query%' or ROUTETXT like '%$query%' "); // the query.

$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.

if ($numrows == 0){
     echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.
     exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows % $limit) {
$pages++;} // has remainder so add one page

$current = intval($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

?>

<html>
<head>
<title>Search Results for <?=$query?></title>
</head>
<body>
<center><h2>Search Results for <?=$query?></h2></center>
<table width="100%" border="0">
 <tr>
  <td width="50%" align="left">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">
Page <b><?=$current?></b> of <b><?=$total?></b>
  </td>
 </tr>
 <tr>
  <td colspan="2" align="right">&nbsp;

  </td>
 </tr>
 <tr>
  <td colspan="2" align="right">
Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
  </td>
 </tr>
</table>

<?php
//Go back into PHP mode.
// Now we can display results.
$results = mysql_query("SELECT * FROM 986_first_table WHERE TITLETXT LIKE '%$query%' or ROUTETXT like '%$query%' ORDER BY TITLETXT ASC LIMIT $page, $limit");
while ($data = @mysql_fetch_array($results)){
	echo '<table width="100%" >';
	echo '<tr>';
	echo '<td rowspan="2"><div align="right">'.$data['STORECODE'];
	echo '</div></td>';
	echo '<td><div align="left">'.$data['TITLECODE'];
	echo '</div></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td><div align="left"><font size=1>'.$data['ROUTETXT'];
	echo '</font></div></td>';
	echo '</tr>';
	echo '</table>';
	}
?>

<p align="center">

<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b>$i</b>\n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");}
?>
</p>
</body>
</html>

Última edición por luisdelbar; 23/04/2011 a las 15:17 Razón: convert links