Jump to content

Recommended Posts

I've browsed in quite a few pagination tutorials and i get on the same thing that blocks me from moving on. This is the common thing:

 

$currentpage = $_GET[’page’];

 

That 'page' thing, what's up with it? From where should the script get the 'page' variable? I understand that is from the address bar, but i don't have it. My page for listing clients is named 'listeazaclienti.php', and that's all that does, directly displays a table with all the clients from the database. I don't click on anything to refere it so that the script can get the 'page' variable.

 

Please help me figure this out.. Or i'll have a very long table.

 

 

Link to comment
https://forums.phpfreaks.com/topic/106115-pagination-tutorials-stuck-on-something/
Share on other sites

You can either make the link .php?page=1 so that it will display the first page or you can use php to check if the page has been set and, depending on whether it is or not, do something. E.g:

 

<?php

if (isset($_GET['page'])) {
       // Display the information for whichever page you want here
     }

else {
      // If no page has been set, display the information in here
     }

?>

But the error i'm getting is:

 

Parse error: syntax error, unexpected '$' in C:\xampp\htdocs\evidenta\listeazaclienti.php on line 151

 

That line is:

 

$pageNumber = (isset($_GET["page"]) $$ is_numeric($_GET["page"])) ? $_GET["page"] : 1;

 

 

So i think it can't get the 'page' because there is no 'page' defined anywhere..

First there is some html, and then this:

 

<?php

require_once('config.php');

 

$pageNumber = (isset($_GET["page"]) $$ is_numeric($_GET["page"])) ? $_GET["page"] : 1;

$perPage = 10;

$padding = 5;

$startIndex = ($pageNumber * $perPage) - $perPage;

$totalCount = "SELECT COUNT(*) as 'Total' FROM clienti";

$rsCount = mysql_query($totalCount);

$rowCount = mysql_fetch_object($rsCount);

 

//--------

print "<div align=\"center\">";

print "Hello";

print "</div>";

 

 

 

$cerereSQL = 'SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $startIndex, $perPage';

$rezultat = mysql_query($cerereSQL);

 

while($rand = mysql_fetch_array($rezultat)) {

//print_r($rand);

echo '<tr><td bgcolor="#d5ffef">'.$rand['numefirpf'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['regcomcnp'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['cifserienr'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['adresa'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['oras'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['numepperc'].'</td>';

echo '<td bgcolor="#d5ffef">'.$rand['telefon'].'</td></tr>';

 

}

?>

 

The pagination isn't finished yet, but, in this state, at least it was supposed to show something without the error.

<?php
require_once('config.php');

//line below replace $$ with &&
$pageNumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
$perPage = 10;
$padding = 5;
$startIndex = ($pageNumber * $perPage) - $perPage;
$totalCount = "SELECT COUNT(*) as 'Total' FROM clienti";
$rsCount = mysql_query($totalCount);
$rowCount = mysql_fetch_object($rsCount);

//--------
print "<div align=\"center\">";
print "Hello";
print "</div>";



$cerereSQL = 'SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $startIndex, $perPage';
$rezultat = mysql_query($cerereSQL);

while($rand = mysql_fetch_array($rezultat)) {
//print_r($rand);
   echo '<tr><td bgcolor="#d5ffef">'.$rand['numefirpf'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['regcomcnp'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['cifserienr'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['adresa'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['oras'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['numepperc'].'</td>';
   echo '<td bgcolor="#d5ffef">'.$rand['telefon'].'</td></tr>';

      }
?>

Thanks a lot MadTechie, kind of a stupid error, damn $ and & :D

 

But now i get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\evidenta\listeazaclienti.php on line 170

 

That line is

while($rand = mysql_fetch_array($rezultat)) {

 

Which is connected to this

$cerereSQL = 'SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $startIndex, $perPage';

$rezultat = mysql_query($cerereSQL);

 

I must say, before i tried to insert pagination, everything would be just fine, the query was ok.

 

 

While waiting for an answer, i've "implemented" this:

 

<?php
require_once('config.php');

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC $max") or die(mysql_error()); 

//This is where you display your query results------------------------


while($rand = mysql_fetch_array($data_p)) {
//print_r($rand);
echo '<tr><td bgcolor="#d5ffef">'.$rand['numefirpf'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['regcomcnp'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['cifserienr'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['adresa'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['oras'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['numepperc'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['telefon'].'</td></tr>';

	}


// This shows the user what page they are on, and the total number of pages----------------------------
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?>

 

 

Kind of a long code, but if you read it maybe you could help me. The above code works in the way that is shows, on the first page, the correct number of entries from $page_rows, but when i click NEXT it shows the exact same 10 records, nothing changes, just in browser, it shows "listeazaclienti.php?pagenum=2", so the change is there. So now i'm on the second page (event though it shows the same, first 10 records :D), but when i click NEXT again, the link is still with pagenum=2.

 

So basically nothing works, the PREVIOUS link doesn't even show.

 

 

MadTechie, i hope you have a solution for either one of the pagination things  :-*

 

As well a max record you also need to tell the database where you want to start getting your data.

 


$min = $total_num_of_records * ($page - 1);
/* $total_num_records is gained from mysql_num_rows() on your first query */

$data_p = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $min, $max") or die(mysql_error()); 

 

Hope it helps...

I've inserted what you said here:

 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$min = $total_num_of_records * ($page - 1);

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC $min, $max") or die(mysql_error()); 

 

(i've put $min under $max, and added $min in the query).

 

Now i get two errors:

Notice: Undefined variable: page in C:\xampp\htdocs\evidenta\listeazaclienti.php on line 181

 

That line is:

$min = $total_num_of_records * ($page - 1);

 

And the second error:

Notice: Undefined variable: total_num_of_records in C:\xampp\htdocs\evidenta\listeazaclienti.php on line 181

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, limit 0,10' at line 1

 

<?php
require_once('config.php');

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$min = $rows * ($pagenum - 1);

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $min, $page_rows") or die(mysql_error()); 

//This is where you display your query results------------------------


while($rand = mysql_fetch_array($data_p)) {
//print_r($rand);
echo '<tr><td bgcolor="#d5ffef">'.$rand['numefirpf'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['regcomcnp'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['cifserienr'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['adresa'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['oras'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['numepperc'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['telefon'].'</td></tr>';

	}


// This shows the user what page they are on, and the total number of pages----------------------------
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?>

 

I've corrected the script so it ues the correct variables.

Thanks a lot for the help, i see you've combined the two scripts. But now i have the same problem described earlier i had with the second script, this:

 

Kind of a long code, but if you read it maybe you could help me. The above code works in the way that is shows, on the first page, the correct number of entries from $page_rows, but when i click NEXT it shows the exact same 10 records, nothing changes, just in browser, it shows "listeazaclienti.php?pagenum=2", so the change is there. So now i'm on the second page (event though it shows the same, first 10 records ), but when i click NEXT again, the link is still with pagenum=2.

 

So basically nothing works, the PREVIOUS link doesn't even show.

 

Thanks again, maybe you know what's wron  :-\

The $pagenum variable is not being abstracted from the link. 

 

Modify:

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

 

To this:

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} else {
$pagenum = $_GET['pagenum']; // abstract var from link
}

Sure.

 

I have my first page, index.php, with links. When i click "listeazaclienti.php" it lists a table with all my clients, taken from the database, table "clienti". And i want to make pagination, so it wouldn't be a loong table. Listeazaclienti.php has this code, with the (half working) pagination included:

 

<?php
session_start();

if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
header('Location: login.php');
exit;
}
?>
<html>
<head>
<title>Telecom Tech - Evidenta Clienti</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="stil.css" type="text/css" />
<script language="JavaScript" type="text/javascript">
var d=new Date();
var monthname=new Array("Ianuarie","Februarie","Martie","Aprilie","Mai","Iunie","Iulie","August","Septembrie","Octombrie","Noiembrie","Decembrie");
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
</script>
<style type="text/css">
<!--
.style5 {font-size: 18px}
.style8 {
font-size: 16px
}
.style9 {color: #ACE5FF}
table.clienti td {color:black; text-align:center; font-size:12px;}
table.clienti th {color:black; text-align:center; font-size:12px;}
-->
</style>
</head>
<body bgcolor="#C0DFFD">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="#0c7eb3">
    <td width="500" colspan="3" rowspan="2"><img src="logo.jpg" alt="Header image" width="500" height="127" border="0" /></td>


  </tr>

  <tr bgcolor="#0c7eb3">
    <td height="64" colspan="3" align="center" valign="center" class="style8" id="tagline"><B><h2 class="style9">Evidenta Clienti</h2>
    </B></td>
<td width="100%"> </td>
  </tr>

  <tr>
    <td colspan="7" bgcolor="#003366"><img src="mm_spacer.gif" alt="" width="1" height="1" border="0" /></td>
  </tr>

  <tr bgcolor="#faff7c">
  	<td height="25" colspan="7" bgcolor="#faff7c" id="dateformat">  
    <script language="JavaScript" type="text/javascript">
      document.write(TODAY);	</script>	</td>
  </tr>
<tr>
    <td colspan="7" bgcolor="#003366"><img src="mm_spacer.gif" alt="" width="1" height="1" border="0" /></td>
  </tr>

<tr>
    <td width="165" valign="top" bgcolor="#3276b4">
<table border="0" cellspacing="0" cellpadding="0" width="165" id="navigation">

         <tr>
        	<td width="165" align="center" height="50"><span class="style5"><a href="index.php" class="style5">Pagina Principala</a></span></td>
        </tr>
        <tr>
        	<td width="165" align="center" height="50"><span class="style5">Clienti</span></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazaclienti.php" class="navText">Listeaza Clienti</a></td>
        </tr>
        <tr>
          <td width="165"><a href="adaugaclient.php" class="navText">Adauga Client</a></td>
        </tr>
        <tr>
          <td width="165"><a href="stergec.php" class="navText">Sterge Client</a></td>
        </tr>
        <tr>
          <td width="165"><a href="editclient.php" class="navText">Editeaza Client</a></td>
        </tr>
        <tr>
          <td width="165"><a href="cauta.php" class="navText">Cauta Client</a></td>
        </tr>
        <tr>
          <td width="165"><a href="fpentruc.php" class="navText">Situatie Client</a></td>
        </tr>
        <tr>
        	<td width="165" align="center" height="50"><span class="style5">Situatie Facturi</span></td>
        </tr>
        <tr>
          <td width="165"><a href="adaugafactura.php" class="navText">Adauga Factura</a></td>
        </tr>
        <tr>
          <td width="165"><a href="update.php" class="navText">Incaseaza Factura</a></td>
        </tr>
        <tr>
          <td width="165"><a href="stergef.php" class="navText">Sterge Factura</a></td>
        </tr>
        <tr>
          <td width="165"><a href="adaugarestanta.php" class="navText">Adauga Restante</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturi.php" class="navText">Facturi Emise</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturiachitate.php" class="navText">Facturi Achitate</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturineachitate.php" class="navText">Facturi Neachitate</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturiachpart.php" class="navText">Facturi Achitate Partial</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturiscadente.php" class="navText">Facturi Scadente</a></td>
        </tr>
        <tr>
          <td width="165"><a href="listeazafacturirestanta.php" class="navText">Facturi cu Restanta</a></td>
        </tr>
      </table>
	 <br />
  	 <br />
  		</td>
    <td width="50"><img src="mm_spacer.gif" alt="" width="50" height="1" border="0" /></td>
    <td width="305" colspan="2" valign="top"><img src="mm_spacer.gif" alt="" width="305" height="1" border="0" /><br />
 <br />
 <br />
<table border="0" cellspacing="0" cellpadding="0" width="660">
        <tr>
          <td width="364" class="pageName"> </td>
	</tr>
        <tr>
        <a href="exportclienti.php"><h5><em>Exporta lista clientilor in Excel</em></h5></a>
	</tr>
	<tr>
          <td class="bodyText"><center><h2><font color="#3366CC">Lista Clienti</font></h2></center>

<table width=100% border=1 class="clienti">
<tr>
<th bgcolor="#faff7c">Denumire Firma/Persoana fizica</th>
<th bgcolor="#faff7c">Registrul comertului/CNP</th>
<th bgcolor="#faff7c">CIF/Serie si Numar</th>
<th bgcolor="#faff7c">Adresa</th>
<th bgcolor="#faff7c">Oras</th>
<th bgcolor="#faff7c">Nume si prenume persoana contact</th>
<th bgcolor="#faff7c">Telefon</th>
</tr>

          <?php
require_once('config.php');

//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} else {
$pagenum = $_GET['pagenum']; // abstract var from link
}

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$min = $rows * ($pagenum - 1);

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM clienti ORDER BY numefirpf ASC LIMIT $min, $page_rows") or die(mysql_error()); 

//This is where you display your query results------------------------


while($rand = mysql_fetch_array($data_p)) {
//print_r($rand);
echo '<tr><td bgcolor="#d5ffef">'.$rand['numefirpf'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['regcomcnp'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['cifserienr'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['adresa'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['oras'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['numepperc'].'</td>';
echo '<td bgcolor="#d5ffef">'.$rand['telefon'].'</td></tr>';

	}


// This shows the user what page they are on, and the total number of pages----------------------------
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
} 

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?>
</table>
           </td>
        </tr>
      </table>
   <br />	  </td>

<td width="100%"> </td>
  </tr>
  <tr>
    <td width="165"> </td>
    <td width="50"> </td>
    <td width="167"> </td>
    <td width="138"> </td>
    <td width="50"> </td>
    <td width="190"> </td>
<td width="100%"> </td>
  </tr>
</table>
<font color="#3276b4"><center><h5>&copy Apostol Vlad, 2008</h5></center></font>
</body>
</html>

 

 

The above code works in the way that is shows, on the first page, a table with the correct number of entries from $page_rows, but when i click NEXT it shows the exact same 10 records, nothing changes, just in browser, it shows "listeazaclienti.php?pagenum=2", so the change is there. So now i'm on the second page (event though it shows the same, first 10 records ), but when i click NEXT again, the link is still with pagenum=2.

 

So basically nothing works, the PREVIOUS link doesn't even show.

sasa  is correct

but personally i would do this:~

//This checks to see if there is a page number. If not, it will set it to page 1 
if (empty($_GET['pagenum']) || $_GET['pagenum'] < 1 )
{ 
$pagenum = 1; 
} else {
$pagenum = (int)$_GET['pagenum']; // abstract var from link
}

Ok sasa, we're so close i can smell it  :D

 

I made the modification sasa said, now when i click NEXT i get to second page, third page etc, PREVIOUS shows, BUT, except the first page, the rest of the pages are blank, no records, no nothing. Just the headers of the table defined before the php.

 

MadTechie, i've also done what you said but it's the exact same result as with sasa's modification (now i have the script with sasa's mod).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.