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!
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.
<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>
<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>
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:
<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>