[php] pagination script doesn't works

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonguim
Forum Newbie
Posts: 21
Joined: Mon Jun 30, 2003 12:53 pm
Location: Ouagadougou, BURKINA FASO
Contact:

[php] pagination script doesn't works

Post by tonguim »

Hi,

i would like to write a script that display all my database records (10 records per page into 2 columns: so 5 records per columns); i would like also to paginate all the page that i'll get by this way: 1, 2, 3, .... (1, 2, 3, ... are hypertext numbers; that means that if i click on "1", i'll go on the first page; if i click on "2" i'll go on the second page etc.)

My code doesn't display any record: only the title of the page with the hypertext numbers are displayed. There are no php errors; the fields name of my database table are rights. I think that the problem comes from my "while" and my "for" loops. Please can you help me to fix my code? Thanks.

Code: Select all

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
	<head>
       <title>Title here!</title>
	</head>
	<body>
		<table>
		<tr>
			<td colspan="2"><h2><center>Actualités</center></h2>
			</td>
		</tr>
		<tr>
			<td>
	<?php
			include("connexion.php");	
		
			$requete=mysql_query("select * from articles order by date desc"); //query 
			$nombreLignes=mysql_num_rows($requete); //number of database records
			$nombreArticlePage=10; //number of article per columns
			$nombrePg=ceil($nombreLignes/$nombreArticlePage); //number of pages to be display			
			$numPgUrl=@$_GET["num"]; //number of page in the url
			$numPgCour=0;
			$col1=0; //first columns of html table
			$col2=0; //second columns of html table
			while($numPgCour < $numPgUrl)
        	{
				for($i = 1; $i<=$nombreArticlePage/2; $i++) 
				{
					mysql_fetch_array($requete);	
				}
				$numPgCour++; 
			}
			for($i = 1; $i<=$nombreArticlePage/2; $i++) 
			{
				if ($col1=mysql_fetch_array($requete) == 0) break;
				if ($col2=mysql_fetch_array($requete) == 0) break;
          		echo "<tr><td><b>" .$col1['titre']. "</b></td><td><b>".$col2['titre']."</b></td></tr>
           			<tr><td>" .$col1['resume']. "</td><td>" .$col2['resume']. "</td></tr>";
        	}			
			?>Page<? 
			for($i = 1; $i<=$nombrePg; $i++) 
			{
   				if($numPgUrl == $i)
				{ 
      				echo $i;
   				} 
				else 
				{ ?>
      				<a href="?num=<? echo $i ?>"
      			>&nbsp;<? echo $i; ?>&nbsp;</a>
   				<? }
			}						
	?>
		</table>
	</body>
</html>
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

viewtopic.php?t=38830

may be of help.

its a pagination class that Jcart wrote

theres a few other pagination tutorials in the forums you can search for.

your script looks pritty confusing, hard to read and understand what what is doing.

for example

for($i = 1; $i<=$nombreArticlePage/2; $i++)
{
mysql_fetch_array($requete);
}

thats just going to give the same results over and over :?
tonguim
Forum Newbie
Posts: 21
Joined: Mon Jun 30, 2003 12:53 pm
Location: Ouagadougou, BURKINA FASO
Contact:

Post by tonguim »

Last edited by tonguim on Mon Oct 31, 2005 9:10 am, edited 1 time in total.
tonguim
Forum Newbie
Posts: 21
Joined: Mon Jun 30, 2003 12:53 pm
Location: Ouagadougou, BURKINA FASO
Contact:

Post by tonguim »

Hi mickd,

i have fixed a lot of things on my code; it is still doesn't display the records.

Code: Select all

<html>
	<body>
	<?
		include("connexion.php");		

		$nbMsgPage = 6;

		$requete = mysql_query('select count(*) as nb_messages from articles'); //query to count my db records
		$resultat = mysql_fetch_array($requete);
		$nbMsgTotal = $resultat['nb_messages']; //number of records

		$nbPages = ceil($nbMsgTotal/$nbMsgPage); //number of pages to display

		echo 'Page : ';
		for ($i=1; $i<=$nbPages; $i++)
		{
			echo '<a href="pagination7.php?page=' . $i . '">' . $i . '</a> '; //echo hypertext numbers 1, 2, 3 ...
		}
	?>
	<?
		$page=1; // we are on the page 1
		if(isset($_GET['page'])) // if there is something in the url,
		{
			$page=(int)$_GET['page']; // we get it
		}
		// we check if we have 0 or a negative number
		if ($page<=0) // if number is negative
			$page=1; // we go on the first page

		// on est qu'on est au moins sur la première page
		// calcul de la valeur a utiliser dans LIMIT pour le debut
		$debut=($page-1)*$nbMsgPage;
		
		//query to check the good records for the right page
		$requete=mysql_query('select * from articles order by date desc limit '.$debut.', '.$nbMsgPage);
		?><table><?
		while((($ligne1=mysql_fetch_array($requete))!=0) && (($ligne2=mysql_fetch_array($requete))!=0))
		{
			if (($ligne1=mysql_fetch_array($requete)) == 0) break;
			if (($ligne2=mysql_fetch_array($requete)) == 0) break;
	?>
	<? //this is to display the records in 2 columns
		echo "<tr><td><b>" .$ligne1['titre']. "</b></td><td><b>".$ligne2['titre']."</b></td></tr>
        	<tr><td>" .$ligne1['resume']. "</td><td>" .$ligne2['resume']. "</td></tr>";	
	}
	?></table>
	</body>
</html>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

you should really consider using the class here

viewtopic.php?t=38830

if jcart did not fix the errors then the last post of mine is a working example of the code. its a great pagentation class, use it
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Not sure what the default is, but:

Code: Select all

//Instead of this:
$resultat = mysql_fetch_array($requete);

//Use this:
$resultat = mysql_fetch_array($requete, MYSQL_ASSOC);
tonguim
Forum Newbie
Posts: 21
Joined: Mon Jun 30, 2003 12:53 pm
Location: Ouagadougou, BURKINA FASO
Contact:

Post by tonguim »

Hi all,
thank you for your posts. My problem has not been fix yet. In the first page, i've some articles, but in the others pages, there are notihin.

Code: Select all

<html>
	<body>
	<?
		include("connexion.php");		

		$nbMsgPage = 10;

		$requete = mysql_query('select count(*) as nb_messages from articles'); //query to count my db records
		$resultat = mysql_fetch_array($requete, MYSQL_ASSOC);
		$nbMsgTotal = $resultat['nb_messages']; //number of records

		$nbPages = ceil($nbMsgTotal/$nbMsgPage); //number of pages to display

		echo 'Page : ';
		for ($i=1; $i<=$nbPages; $i++)
		{
			echo '<a href="pagination7.php?page=' . $i . '">' . $i . '</a> '; //echo hypertext numbers 1, 2, 3 ...
		}
	?>
	<?
		$page=1; // we are on the page 1
		if(isset($_GET['page'])) // if there is something in the url,
		{
			$page=(int)$_GET['page']; // we get it
		}
		// we check if we have 0 or a negative number
		if ($page<=0) // if number is negative
			$page=1; // we go on the first page

		// on est qu'on est au moins sur la première page
		// calcul de la valeur a utiliser dans LIMIT pour le debut
		$debut=($page-1)*$nbMsgPage;
		
		//query to check the good records for the right page
		$requete=mysql_query('select * from articles order by date desc limit '.$debut.', '.$nbMsgPage);
		?><table><?
		while(($ligne1=mysql_fetch_array($requete, MYSQL_ASSOC))!=0)
		{

                        if (($ligne2=mysql_fetch_array($requete, MYSQL_ASSOC)) == 0)
                                         echo "";
	?>
	<?			
		echo "<tr><td><b>" .$ligne1['titre']. "</b></td><td><b>".$ligne2['titre']."</b></td></tr>
        	<tr><td>" .$ligne1['resume']. "</td><td>" .$ligne2['resume']. "</td></tr>";	
	}
	?></table>
	</body>
</html>
tonguim
Forum Newbie
Posts: 21
Joined: Mon Jun 30, 2003 12:53 pm
Location: Ouagadougou, BURKINA FASO
Contact:

Post by tonguim »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi i finally find the right code; this is working fine:

Code: Select all

<html>
	<body>
	<?
		include("connexion.php");		
 
		$nbMsgPage = 10;
 
		$requete = mysql_query('select count(*) as nb_messages from articles');
		$resultat = mysql_fetch_array($requete, MYSQL_ASSOC);
		$nbMsgTotal = $resultat['nb_messages'];
		$debut=0;
		$nbPages = ceil($nbMsgTotal / $nbMsgPage);
 
		echo 'Page : ';
		for ($i=1; $i<=$nbPages; $i++)
		{
			echo '<a href="pagination8.php?page=' . $i . '">' . $i . '</a> ';
		}
		//$requete = mysql_query('select * from articles order by date desc limit 0, $nbMsgPage');
	?>
	<?
		$page=1; // par defaut, on est sur la premiere page
		if(isset($_GET['page'])) // si on a un parametre dans l'url
		{
			$page=(int)$_GET['page']; // on le recupere, le (int) devant c'est pour s'assurer de recuperer un nombre
		}
		// on verifie si on n'a pas 0 dans $page
		if ($page<=0) // si on a 0 ou un nombre negatif
			$page=1; // on se remet sur la premiere page
 
		// on est qu'on est au moins sur la première page
		// calcul de la valeur a utiliser dans LIMIT pour le debut
		$debut=($page-1)*$nbMsgPage;
 
		//requete pour chercher les articles adequats a la page où on se trouve :
		//$requete=mysql_query('select * from articles order by date desc limit '.$debut.', '.$nbMsgPage);
		$requete=mysql_query('select * from articles order by date desc limit '.$debut.', '.$page*$nbMsgPage);		
		?><table><?		
		//ci dessous je divise parce que je veux afficher les articles en 2 colonnes sur chaque page
		$i=0;
		echo"<tr>";
		while(($ligne=mysql_fetch_array($requete))
		{		
			$i++;			
			echo "<td><b>" .$ligne1['titre']. "</b></td>";	
			if($i==2)]
			{
				echo"</tr><tr>";
				$i=0;
			}
		}
		echo"</tr>";
	?>
	</table>
	</body>
</html>
Thanks to all


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply