Foros del Web » Programando para Internet » PHP »

ayuda con paginacion de consulta mysql

Estas en el tema de ayuda con paginacion de consulta mysql en el foro de PHP en Foros del Web. manes aqui yo otra vez con mis problemas T_T tengo este table que es para un pequeño foro que estoy creando lo que deseo es ...
  #1 (permalink)  
Antiguo 29/12/2009, 20:07
 
Fecha de Ingreso: mayo-2006
Mensajes: 86
Antigüedad: 17 años, 10 meses
Puntos: 0
ayuda con paginacion de consulta mysql

manes aqui yo otra vez con mis problemas T_T

tengo este table que es para un pequeño foro que estoy creando

lo que deseo es poder paginar los datos que me salen de esta consulta

pero el detalle es que no se como U_U

alguan idea o sugerencia porfavor gracias

Código:
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td height="25"><div id="moduloforos"><? echo $lang['titulomoduloforo'] ?></div></td>
                  </tr>
<?
$connection = mysql_connect ($lang['bd_host'],$lang['bd_usuario'],$lang['bd_password']);
if(!$connection) { die(mysql_error()); }
mysql_select_db($lang['bd_base']);
if(!@mysql_select_db($lang['bd_base'],$connection)) { die(mysql_error()); }
if ($cat == 0){$sql_text = "SELECT * FROM johwic_topicos WHERE foro_id <> '$cat' order by grupo_id desc "; }
else {$sql_text = "SELECT * FROM johwic_topicos WHERE foro_id = '$cat' order by grupo_id desc "; }
if ($r = mysql_query($sql_text,$connection)){
while ($row = mysql_fetch_array($r)){
	

                 $cats++; 

$resto = substr ($row['topico_titulo'], 0, 48 );

?>
                  <tr>
                    <td width="" align="left" valign="top" id="resultadosdb" onClick="MM_goToURL('parent','ficha.php?ndii=<? echo $row['topico_id'] ?>');return document.MM_returnValue"><div id="moduloforos2">
					
							<? $idforo = $row['foro_id']; 
							if($idforo == 1) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/flash.gif" width="20" height="20"><? } 
							if($idforo == 2) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/temas.gif" width="20" height="20"><? }
							if($idforo == 3) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/arte.gif" width="20" height="20"><? }
							if($idforo == 4) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/autos.gif" width="20" height="20"><? }
							if($idforo == 5) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/cel.png" width="20" height="20"><? }
							if($idforo == 6) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/Comic.gif" width="20" height="20"><? }
							if($idforo == 7) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/deportes.png" width="20" height="20"><? }
							if($idforo == 8) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/descargas.gif" width="20" height="20"><? }
							if($idforo == 9) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/tutoriales.gif" width="20" height="20"><? }
							if($idforo == 10) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/economia.gif" width="20" height="20"><? }
							if($idforo == 11) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mujeres.png" width="20" height="20"><? }
							if($idforo == 12) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/humor.gif" width="20" height="20"><? }
							if($idforo == 13) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/imagenes.gif" width="20" height="20"><? }
							if($idforo == 14) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/informacion.png" width="20" height="20"><? }
							if($idforo == 15) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/juegos.png" width="20" height="20"><? }
							if($idforo == 16) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/link.gif" width="20" height="20"><? }
							if($idforo == 17) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/linux.gif" width="20" height="20"><? }
							if($idforo == 18) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mac.gif" width="20" height="20"><? }
							if($idforo == 19) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/manga.png" width="20" height="20"><? }
							if($idforo == 20) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mascota.gif" width="20" height="20"><? }
							if($idforo == 21) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/musica.jpg" width="20" height="20"><? }
							if($idforo == 22) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/noticias.png" width="20" height="20"><? }
							if($idforo == 23) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/offtopic.gif" width="20" height="20"><? }
							if($idforo == 24) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/patrocinados.jpg" width="20" height="20"><? }
							if($idforo == 25) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/recetas.gif" width="20" height="20"><? }
							if($idforo == 26) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/salud.png" width="20" height="20"><? }
							if($idforo == 27) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/sociales.png" width="20" height="20"><? }
							if($idforo == 28) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/j.png" width="20" height="20"><? }
							if($idforo == 29) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/turismo.gif" width="20" height="20"><? }
							if($idforo == 30) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/peliculas.gif" width="20" height="20"><? }
                            if($idforo == 31) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/video.png" width="20" height="20"><? }
							?>                      
							
							<? $idgrupo = $row['grupo_id']; 
							if($idgrupo == 0) { ?><? } 
							if($idgrupo == 1) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/registrate.png" width="20" height="20"><? }
							if($idgrupo == 2) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/j.png" width="20" height="20"><? }
							?> 
							
				<span id="forspan"><? echo $resto; ?>...</span></div></td>
                  </tr>
<?
                 }}
mysql_close($connection);

              if($cats== 0) {

                  $cats= 0; }
?>
                </table>
  #2 (permalink)  
Antiguo 29/12/2009, 21:01
 
Fecha de Ingreso: enero-2008
Mensajes: 381
Antigüedad: 16 años, 3 meses
Puntos: 19
Respuesta: ayuda con paginacion de consulta mysql

mejor que te expliques mejor, no entiendo que pretendes
  #3 (permalink)  
Antiguo 29/12/2009, 23:09
Avatar de Vun
Vun
Colaborador
 
Fecha de Ingreso: agosto-2009
Ubicación: Benalmádena, España
Mensajes: 2.265
Antigüedad: 14 años, 8 meses
Puntos: 150
Respuesta: ayuda con paginacion de consulta mysql

Si tu haces:

SELECT * FROM johwic_topicos

Te selecciona todos ¿ok?

Si haces

SELECT * FROM johwic_topicos LIMIT 0,20

Te selecciona 20 registros a partir del 0, es decir, los 20 primeros (pagina 1)

Si haces

SELECT * FROM johwic_topicos LIMIT 20,20

Te selecciona 20 registros a partir del 20 (pagina 2)

Con estos ejemplos deberias ser capaz de trastear y sacar una forma de paginar la consulta dependiendo de un valor pasado por la url, tipo pagina.php?pagina=0 , pagina.php?pagina=1 , etc

Última edición por Vun; 29/12/2009 a las 23:41
  #4 (permalink)  
Antiguo 29/12/2009, 23:33
Avatar de Vitesse92  
Fecha de Ingreso: diciembre-2009
Ubicación: Internet
Mensajes: 145
Antigüedad: 14 años, 4 meses
Puntos: 5
Respuesta: ayuda con paginacion de consulta mysql

y yo te voy a recomendar esta pagina, ahi hay un buen ejemplo de paginacion ;)
http://phpya.com.ar/temarios/descripcion.php?cod=53
y esta otra para aprender lo mas basico de mysql
www.mysqlya.com.ar ;)
visita esas dos paginas, practica * practic ** 3 = buenos resultados en poco tiempo
vale,..suerte
  #5 (permalink)  
Antiguo 30/12/2009, 03:52
 
Fecha de Ingreso: mayo-2006
Mensajes: 86
Antigüedad: 17 años, 10 meses
Puntos: 0
Respuesta: ayuda con paginacion de consulta mysql

muchas gracias fue de gran ayuda

este es el code resultante aunque falta dar un toke de diseño pero funciona al 100%

Código:
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td height="25"><div id="moduloforos"><? echo $lang['titulomoduloforo'] ?></div></td>
                  </tr>
<?
$connection = mysql_connect ($lang['bd_host'],$lang['bd_usuario'],$lang['bd_password']);
if(!$connection) { die(mysql_error()); }
mysql_select_db($lang['bd_base']);
if(!@mysql_select_db($lang['bd_base'],$connection)) { die(mysql_error()); }
if ($cat == 0){$sql_text = "SELECT * FROM johwic_topicos WHERE foro_id <> '$cat' order by grupo_id desc limit $inicio,$fin"; }
else {$sql_text = "SELECT * FROM johwic_topicos WHERE foro_id = '$cat' order by grupo_id desc limit $inicio,$fin"; }
$cats=0;
if ($r = mysql_query($sql_text,$connection)){
while ($row = mysql_fetch_array($r)){
	

                 $cats ++; 

$resto = substr ($row['topico_titulo'], 0, 48 );

?>
                  <tr>
                    <td width="" align="left" valign="top" id="resultadosdb" onClick="MM_goToURL('parent','ficha.php?ndii=<? echo $row['topico_id'] ?>');return document.MM_returnValue"><div id="moduloforos2">
					
							<? $idforo = $row['foro_id']; 
							if($idforo == 1) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/flash.gif" width="20" height="20"><? } 
							if($idforo == 2) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/temas.gif" width="20" height="20"><? }
							if($idforo == 3) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/arte.gif" width="20" height="20"><? }
							if($idforo == 4) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/autos.gif" width="20" height="20"><? }
							if($idforo == 5) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/cel.png" width="20" height="20"><? }
							if($idforo == 6) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/Comic.gif" width="20" height="20"><? }
							if($idforo == 7) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/deportes.png" width="20" height="20"><? }
							if($idforo == 8) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/descargas.gif" width="20" height="20"><? }
							if($idforo == 9) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/tutoriales.gif" width="20" height="20"><? }
							if($idforo == 10) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/economia.gif" width="20" height="20"><? }
							if($idforo == 11) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mujeres.png" width="20" height="20"><? }
							if($idforo == 12) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/humor.gif" width="20" height="20"><? }
							if($idforo == 13) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/imagenes.gif" width="20" height="20"><? }
							if($idforo == 14) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/informacion.png" width="20" height="20"><? }
							if($idforo == 15) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/juegos.png" width="20" height="20"><? }
							if($idforo == 16) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/link.gif" width="20" height="20"><? }
							if($idforo == 17) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/linux.gif" width="20" height="20"><? }
							if($idforo == 18) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mac.gif" width="20" height="20"><? }
							if($idforo == 19) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/manga.png" width="20" height="20"><? }
							if($idforo == 20) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/mascota.gif" width="20" height="20"><? }
							if($idforo == 21) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/musica.jpg" width="20" height="20"><? }
							if($idforo == 22) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/noticias.png" width="20" height="20"><? }
							if($idforo == 23) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/offtopic.gif" width="20" height="20"><? }
							if($idforo == 24) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/patrocinados.jpg" width="20" height="20"><? }
							if($idforo == 25) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/recetas.gif" width="20" height="20"><? }
							if($idforo == 26) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/salud.png" width="20" height="20"><? }
							if($idforo == 27) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/sociales.png" width="20" height="20"><? }
							if($idforo == 28) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/j.png" width="20" height="20"><? }
							if($idforo == 29) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/turismo.gif" width="20" height="20"><? }
							if($idforo == 30) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/peliculas.gif" width="20" height="20"><? }
                            if($idforo == 31) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/video.png" width="20" height="20"><? }
							?>                      
							
							<? $idgrupo = $row['grupo_id']; 
							if($idgrupo == 0) { ?><? } 
							if($idgrupo == 1) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/registrate.png" width="20" height="20"><? }
							if($idgrupo == 2) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/j.png" width="20" height="20"><? }
							?> 
							
				<span id="forspan"><? echo $resto; ?>...</span></div></td>
                  </tr>
<?
                 }}
mysql_close($connection);
if($cats == 0) {
$cats = 0; }
?>
           <tr>
           <td align="center" bgcolor="#C1C1C1">  
		  <table width="200" border="1">
		    <tr>
		      <td><? if ($inicio==0) { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/humor.gif" width="20" height="20" /><? }
else
{
  $anterior=$inicio-$fin;
 ?><div id="ante" onclick="MM_goToURL('parent','?cat=<? echo $cat ?>&pag=<? echo $anterior ?>');return document.MM_returnValue" onMouseOver="MM_changeProp('ante','','color','#000','DIV');MM_changeProp('ante','','backgroundColor','#96C8E2','DIV');MM_changeProp('ante','','textDecoration','underline','DIV')" onMouseOut="MM_changeProp('ante','','textDecoration','none','DIV');MM_changeProp('ante','','color','#FFF','DIV');MM_changeProp('ante','','backgroundColor','#4FA0CC','DIV')"><? echo $lang['monbremenu2boton2'] ?></div> <? } ?> 
 			
            </td>
			<td><?
if ($cats==$fin)
{
  $proximo=$inicio+$fin;
  ?><div id="sigi" onclick="MM_goToURL('parent','?cat=<? echo $cat ?>&pag=<? echo $proximo ?>');return document.MM_returnValue" onMouseOver="MM_changeProp('sigi','','color','#000','DIV');MM_changeProp('sigi','','backgroundColor','#96C8E2','DIV');MM_changeProp('sigi','','textDecoration','underline','DIV')" onMouseOut="MM_changeProp('sigi','','textDecoration','none','DIV');MM_changeProp('sigi','','color','#FFF','DIV');MM_changeProp('sigi','','backgroundColor','#4FA0CC','DIV')"><? echo $lang['monbremenu2boton2'] ?></div> <?
}
else
  { ?><img src="http://www.forosdelweb.com/f18/imagenes/icopost/j.png" width="20" height="20" /><? } ?>
  			
            </td>
		      </tr>
		    </table>

             </td></tr>
                </table>

y antes del head


Código:
<? $cat = $_GET['cat']; 

if (isset($_REQUEST['pag']))
  $inicio=$_REQUEST['pag'];
else
  $inicio=0;
  $fin=20;
?>
lo demas es solo variables y otros includes que no tiene que ver mucho

espero a alguien que lo necesesite sea de ayuda

bye bye
  #6 (permalink)  
Antiguo 30/12/2009, 08:09
Avatar de Vun
Vun
Colaborador
 
Fecha de Ingreso: agosto-2009
Ubicación: Benalmádena, España
Mensajes: 2.265
Antigüedad: 14 años, 8 meses
Puntos: 150
Respuesta: ayuda con paginacion de consulta mysql

Me alegra que te haya podido servir mi explicacion con el LIMIT.

Otra cosa que sirve para la paginacion es conocer de antemano en tu pagina el numero de registros:

SELECT count(*) as total from tabla

De esta manera y dividiendo el total por el numero de resultados por pagina tienes el numero de paginas totales, para luego crear el tipico esquema de:

<Ant 1 2 3 4 5 Sig>

O en caso de estar por ejemplo en la ultima pagina:

<Ant 1 2 3 4 5


Saludos, buen fin de año y buen karma
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 04:42.