PHP Forums Archive

Pagination PHP script

Tags: php, pagination

julius1984 posted on 2008-02-28 16:26:19 #

Hi I'm a newbie in php...

is there anybody could help me to create pagination php script...

here is my display code.

$query = mysql_query("SELECT * FROM cust_qa_table WHERE status = '{$qastat}' ORDER BY ques_date");

$f=0;

while($row=mysql_fetch_array($query))
   {
   $query2 = mysql_query("SELECT * FROM cust_table WHERE username = '{$row[username]}'");

     echo $row['name'];

			$f=1;
			}

			if($f==0)
				{
				echo "<font color='red'><br><br>No members currently ask question.</font><br><br>";
				}

Comments / discussions

Olaf posted on 2008-02-28 16:30:43 #

Hello, do you tried this snippet or this class?

julius1984 posted on 2008-02-28 16:47:06 #

I already downloaded class but I got headache when reading the code... I think I choice snippet... in my code above... can you teach me where should I insert my code?

thank you OLaf

Olaf posted on 2008-02-28 18:38:52 #

you could start with my snippet while using your sql

check the php manual about the mysql functions, this will help ;)

felipe_br posted on 2008-10-16 13:13:33 #

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META name=AUTHOR content="Felipe">
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<title>movimento</title>
<style type="text/css">
<!--
body {
        font-size: 11px;
        font-family: arial, helvetica, sans-serif;
}
a {
        font-family: arial, helvetica, sans-serif;
        font-size: 12px;
        color: #666666;
        text-decoration: none;
}
a:hover {
        font-family: arial, helvetica, sans-serif;
        font-size: 12px;
        color: #ceffe7;
        text-decoration: none;
}
h1 {
        font-family: arial, helvetica, sans-serif;
        font-size: 14px;
        font-weight: bold;
}
table{
        padding: 5;
}
th {
        font-size : 11px;
        font-family : arial, helvetica, sans-serif;
        color : #FFFFFF;
        text-align : center;
        font-weight : bold;
        background-color : #336699;
}
tr {
        font-family: arial, helvetica, sans-serif;
        font-size: 11px;
        background-color : #FFFFFF;
}
td {
        font-family: arial, helvetica, sans-serif;
        font-size: 11px;
}
TABLE.Mtable TD {
        BORDER-RIGHT: #93bee2 1px solid;
        BORDER-BOTTOM: #c1cdd8 1px solid;
}
TABLE.Mtable TH {
        BORDER-RIGHT: #93bee2 1px solid;
}
TABLE.Mtable {
        border: 1px solid #336699;
}

.TRalter {
        background-color : #dcdcdc;
}

TABLE.searchTable {
        border: 1px solid;
		  border-bottom-style: solid;
		  border-color: #446699;

}
input {
        font-size: 11px;
        font-family: arial, helvetica, sans-serif;
}

-->
</style>
</head>
<body>
<h1>movimento</h1>

<!-- search engine -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET" name="mform" id="mform">
  <table width="200" border="0" cellpadding="0" cellspacing="0" class="searchTable">
    <tr>
      <td nowrap> <b>Pesquisar</b>
        <input type="text" name="search" value="<?php if(!empty($_GET['search']))echo $_GET['search']; ?>">
   <input type="submit" value="ok">
   <input type="submit" value="Ver todo(s) resultado(s)" onClick="document.mform.search.value='';">
      </td>
 </tr>
</table>
</form>
<!-- search engine -->

<?php

/*********************************************************
   Cridado por Felipe em (10/16/2008)
   http://localhost/Learning/

   Table : movimento
*********************************************************/

 $link = mysql_connect("localhost", "root", "######") or die("Impossivel conectar : " . mysql_error());
 mysql_select_db("controle") or die(mysql_error());
 $query = "SELECT
                  Operacao,
                  Data,
                  Valor,
                  Descricao,
                  Conta
           FROM
                  movimento";

 // ************* search *****************/
 if( isset($_GET['search']) && !empty($_GET['search']))
 {
    // WHERE found ?
    if(!stristr($query,"WHERE "))
	 $query .= " WHERE (";
    else
	 $query .= " AND (";

    // add field
     $query .= " Operacao LIKE '%".addslashes($_GET['search'])."%' OR";
     $query .= " Descricao LIKE '%".addslashes($_GET['search'])."%' OR";
     $query .= " Conta LIKE '%".addslashes($_GET['search'])."%' OR";

// delete last OR
$query = substr($query,0,strlen($query)-3);
$query .= ")";

 }
 // ************* end of search *****************/  

 $result = mysql_query($query) or die(mysql_error());
 $numrows = mysql_num_rows($result); // result of count query

// ************** pager **************************
 $limit = 10; // Default results per-page.
 if(empty($_GET['page']))$_GET['page'] = 0; // Default page value

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

 if($numrows%$limit) $pages++; // has remainder so add one page
 $current = ($_GET['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 = $_GET['page'] + 1; // The first result.

 if(((($_GET['page'] + $limit) / $limit) >= $pages) && $pages != 1)
         $last = $_GET['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.
// ************** end of pager **************************

 if($numrows == 0)
 {
     echo "<b>Nenhum registro localizado</b>

         <a rel="nofollow" href=\"javascript:history.back();\">[<< Pagina anterior]</a>\n";
 }
 else
 {

	$query = "SELECT
                       Operacao,
                       Data,
                       Valor,
                       Descricao,
                       Conta
                FROM
                       movimento";

 // ************* search *****************/
 if( isset($_GET['search']) && !empty($_GET['search']))
 {
    // WHERE found ?
    if(!stristr($query,"WHERE "))
	 $query .= " WHERE (";
    else
	 $query .= " AND (";

    // add field
     $query .= " Operacao LIKE '%".addslashes($_GET['search'])."%' OR";
     $query .= " Descricao LIKE '%".addslashes($_GET['search'])."%' OR";
     $query .= " Conta LIKE '%".addslashes($_GET['search'])."%' OR";

// delete last OR
$query = substr($query,0,strlen($query)-3);
$query .= ")";

 }
 // ************* end of search *****************/  

     $query .= " LIMIT ".$_GET['page'].", $limit"; // add query LIMIT
     $result = mysql_query($query) or die(mysql_error());
     $numrows = mysql_num_rows($result);

     //echo our table
     echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
         echo "\t<th>".ucfirst("Operacao")."</th>\n";
         echo "\t<th>".ucfirst("Data")."</th>\n";
         echo "\t<th>".ucfirst("Valor")."</th>\n";
         echo "\t<th>".ucfirst("Descricao")."</th>\n";
         echo "\t<th>".ucfirst("Conta")."</th>\n";

     $i = 0;

     while ($row = mysql_fetch_assoc($result))
     {
        // alternate color
        if($i%2 == 0)
               echo "<tr class=\"TRalter\">\n";
        else
        echo "<tr>\n";

         echo "\t<td>&nbsp;".$row["Operacao"]."</td>\n";
         echo "\t<td>&nbsp;".$row["Data"]."</td>\n";
         echo "\t<td>&nbsp;".$row["Valor"]."</td>\n";
         echo "\t<td>&nbsp;".$row["Descricao"]."</td>\n";
         echo "\t<td>&nbsp;".$row["Conta"]."</td>\n";

        echo "</tr>\n";
        $i++;
     }
     echo "</table>\n";

     mysql_free_result($result);
 }

// ************** bottom pager  **************************
 echo "<table width=\"100%\" border=\"0\">\n";
 echo "<tr>\n";
 echo "\t<td align=\"center\">\n";

 if (!empty($_GET['page']) && $_GET['page'] > 0)
 {
   $back_page = $_GET['page'] - $limit;
   $url = $_SERVER["PHP_SELF"]."?page=$back_page";

   if(!empty($_GET['search']))$url .= "&search=".$_GET['search']; // add search  

   echo "<a rel="nofollow" href=\"$url\">[<< Enterior]</a>    \n";
 }

 // loop through each page and give link to it.
 // no page when only one page
 if($pages > 1)
 {
         for ($i=1; $i <= $pages; $i++)
         {
           $ppage = $limit*($i - 1);
           if ($ppage == $_GET['page'])
                  echo("<b>$i</b> \n");
           else
           {
                  $url = $_SERVER["PHP_SELF"]."?page=$ppage";

                  if(!empty($_GET['search']))$url .= "&search=".$_GET['search']; // add search 

                  echo("<a rel="nofollow" href=\"$url\">$i</a> \n");
           }
         }
 }

 if(((($_GET['page']+$limit) / $limit) < $pages) && $pages != 1)
 {
   $next_page = $_GET['page'] + $limit;// If last page don't give next link.
   $url = $_SERVER["PHP_SELF"]."?page=$next_page";

   if(!empty($_GET['search']))$url .= "&search=".$_GET['search'];// add search   

   echo "    <a rel="nofollow" href=\"$url\">[Proximo >>]</a>\n";
 }

echo "\t</td>\n";
echo "</tr>\n";
echo "</table>";
// ************** end of bottom pager **************************

 mysql_close($link);  

?>
</body>
</html>

Alter your Connections rules, tables and fields name. You got it preety working. This script has a search engine included a pagination and alternate row colors.