Hi all..

I have a static page called news. This page have more than 100 articles link and small description. Now i am planning to implement a pagination of every 10 links. Anyone help this problem? Thanking you.

Regards,
Ram

Recommended Answers

All 18 Replies

look at this. copy this code and change as your need.
pagination.php

<?php



function Pages($tbl_name,$limit,$path)
{
	$query = "SELECT COUNT(*) as num FROM $tbl_name";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	$adjacents = "2";
	$page = $_GET['page'];
	if($page)
	$start = ($page - 1) * $limit;
	else
	$start = 0;

$sql = "SELECT id FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

	if ($page == 0) $page = 1;
	$prev = $page - 1;
	$next = $page + 1;
	$lastpage = ceil($total_pages/$limit);
	$lpm1 = $lastpage - 1;

	$pagination = "";
if($lastpage > 1)
{   
	$pagination .= "<div class='pagination'>";
if ($page > 1)
	$pagination.= "<a href='".$path."page=$prev'>« previous</a>";
else
	$pagination.= "<span class='disabled'>« previous</span>";   

if ($lastpage < 7 + ($adjacents * 2))
{   
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
	$pagination.= "<span class='current'>$counter</span>";
else
	$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))       
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
	$pagination.= "<span class='current'>$counter</span>";
else
	$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
	$pagination.= "...";
	$pagination.= "<a href='".$path."page=$lpm1'>$lpm1</a>";
	$pagination.= "<a href='".$path."page=$lastpage'>$lastpage</a>";       
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
	$pagination.= "<a href='".$path."page=1'>1</a>";
	$pagination.= "<a href='".$path."page=2'>2</a>";
	$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
	$pagination.= "<span class='current'>$counter</span>";
else
	$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
	$pagination.= "..";
	$pagination.= "<a href='".$path."page=$lpm1'>$lpm1</a>";
	$pagination.= "<a href='".$path."page=$lastpage'>$lastpage</a>";       
}
else
{
	$pagination.= "<a href='".$path."page=1'>1</a>";
	$pagination.= "<a href='".$path."page=2'>2</a>";
	$pagination.= "..";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
	$pagination.= "<span class='current'>$counter</span>";
else
	$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
}
}

if ($page < $counter - 1)
	$pagination.= "<a href='".$path."page=$next'>next »</a>";
else
	$pagination.= "<span class='disabled'>next »</span>";
	$pagination.= "</div>\n";       
}


return $pagination;
}


?>

and next index.php

<?php



	require_once ('pagination.php');

//connect to the mysql
$db = @mysql_connect('localhost', 'root', '') or die("Could not connect database");
@mysql_select_db('dbname', $db) or die("Could not select database");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How To Create A Pagination In PHP</title>

<!-- css skin -->
<link rel="stylesheet" type="text/css" href="style2.css" />
<style type="text/css">
a {
color:#333;
text-transform: capitalize;
}
a:hover{
color: #999;
text-decoration:underline
}
</style>
</head>
<body>
<?php

	$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
	$page = ($page == 0 ? 1 : $page);
	$perpage = 5;//limit in each page
	$startpoint = ($page * $perpage) - $perpage;

	$sql = @mysql_query("select * FROM `tutorials` order by id desc LIMIT $startpoint,$perpage");

while($Row = mysql_fetch_array($sql)) {

	echo '<p style="text-align: center;"><a target="_blank" href="'.$Row['url'].'">'.$Row['title'].'</a></p>';
	
}

	//show pages
	echo Pages("tutorials",$perpage,"index.php?");
	
?>

</body>
</html>

Hi murali...

This code using the ststic page?

is it your page content dynamic or static

ok. this is for dynamic page. first include pagination.php as it is. and next change $q_limit = 10; if you need to display 20 items. and next pagination().

index.php

<?php
ob_start();
extract($_REQUEST);
extract($_POST);
include('paginate.php');

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db('flower',$con);
 $q_limit = 10; 
$errMsg = 0;
if( isset($_GET['start']) ){
	$start = $_GET['start'];
}else{
	$start = 0;
}
$filePath = "index.php"; 
				
 
  ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>

</head>

<body >
<?
//echo		 "select * from product order by auto_id desc LIMIT $start , $q_limit ";exit;

        $selectproducts=mysql_query("select * from tablename");
		$no_rows=mysql_num_rows($selectproducts); 
		$select1=mysql_query("select * from tablename order by auto_id desc LIMIT $start , $q_limit ");
		
		while($fetch=mysql_fetch_array($select1))
		{ 
		
		?>
		<!--dispaly your content here-->

						<?
						}
                         ?>
		<span class="pagetext">
                <? paginate($start,$q_limit,$no_rows,$filePath,"");?> <!--write this function where you want-->
              </span>
		
</body>
</html>

pagination.php

<?
function paginate($start,$limit,$total,$filePath,$otherParams) {
	global $lang;

	$allPages = ceil($total/$limit);

	$currentPage = floor($start/$limit) + 1;

	$pagination = "";
	if ($allPages>10) {
		$maxPages = ($allPages>9) ? 9 : $allPages;

		if ($allPages>9) {
			if ($currentPage>=1&&$currentPage<=$allPages) {
				$pagination .= ($currentPage>4) ? " ... " : " ";

				$minPages = ($currentPage>4) ? $currentPage : 5;
				$maxPages = ($currentPage<$allPages-4) ? $currentPage : $allPages - 4;

				for($i=$minPages-4; $i<$maxPages+5; $i++) {
					$pagination .= ($i == $currentPage) ? "[".$i."] " : "<a class='linktext' href=\"".$filePath."?start=".(($i-1)*$limit).$otherParams."\">".$i."</a> ";
				}
				$pagination .= ($currentPage<$allPages-4) ? " ... " : " ";
			} else {
				$pagination .= " ... ";
			}
		}
	} else {
		for($i=1; $i<$allPages+1; $i++) {
			$pagination .= ($i==$currentPage) ? "[".$i."] " : "<a class='linktext' href=\"".$filePath."?start=".(($i-1)*$limit).$otherParams."\">".$i."</a> ";
		}
	}

	if ($currentPage>1) $pagination = "[<a class='linktext' href=\"".$filePath."?start=0".$otherParams."\">&lt;&lt;</a>] [<a  class='linktext' href=\"".$filePath."?start=".(($currentPage-2)*$limit).$otherParams."\">&lt;</a>] ".$pagination;
	if ($currentPage<$allPages) $pagination .= " [<a class='linktext' href=\"".$filePath."?start=".($currentPage*$limit).$otherParams."\">&gt;</a>] [<a class='linktext' href=\"".$filePath."?start=".(($allPages-1)*$limit).$otherParams."\">&gt;&gt;</a>]";

	echo $pagination;
}
?>

Hi...

Sorry murali. My page is static. Earlier my post i mentioned "I have a static page called news". I used ur code as a dynamic. Its works perfect. But my page is static.

Thanks!

welcome. problem solved or not. if it solved mark as solved.

If a dynamic page, the problem is solved. But i am looking a static page pagination.

hmm...go through this.

<?php

class display {
	function pagination($rows, $per_page, $current_page, $page_link) {
		global $core,$C;
		
		// Create a Page Listing
		$this->pages = ceil($rows / $per_page);
		
		// If there's only one page, return now and don't bother
		if($this->pages == 1) {
			return;
		}
		
		// Pagination Prefix
                $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
		$output = "Pages: ";
		
		// Should we show the FIRST PAGE link?
		if($current_page > 2) {
			$output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
		}
		
		// Should we show the PREVIOUS PAGE link?
		if($current_page > 1) {
			$previous_page = $current_page - 1;
			$output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
		}
		
		// Current Page Number
		$output .= "<strong>[ ". $current_page ." ]</strong>";
		
		// Should we show the NEXT PAGE link?
		if($current_page < $this->pages) {
			$next_page = $current_page + 1;
			$output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
		}
		
		// Should we show the LAST PAGE link?
		if($current_page < $this->pages - 1) {
			$output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
		}
		
		// Return the output.
		return $output;
	}
}

$display = new display;
echo $display->pagination("45", "15", "1", "http://mysite.com/index.php");
?>

or reffer this snippet once http://www.daniweb.com/code/snippet216842.html

Hi...

This code not shown the result properly.

I am using the below parameter.

echo $display->pagination("45", "10", "1", "http://mysite.com/press.php");

Now the page shows, Pages: [ 1 ]> >>. But this page [1] nothing display. and i clicks on the ">" button, its display all the links(totally 30) and not display the pagination like "<< [2] >>". In my understanding we pass the parameter "10" for the page [1]. But its not display properly. Hope u understand.

Thanks!

If it works for Dynamic pages then it should not be hard for static. Just rename your pages to something like story1.htm, story2.htm et al and create that dynamically, something like below. This is not working code, so implement in Murali's code.
But why would it be "strictly" Static?

i = 1; //story1
include("story".i);

Hi Murali...

Pls see below my code.

<?php

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CONDITIONS);

  $breadcrumb->add('Media News', 'press.php');
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="3" cellpadding="3">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><span>Media News</span></td>
            <td class="pageHeading" align="right"><?php  ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td class="main">
 <?php include(DIR_WS_LANGUAGES . $language . '/' . [B]FILENAME_DEFINE_MAINPAGE2[/B]); ?> 
[B]<?php

class display {
    function pagination($rows, $per_page, $current_page, $page_link) {
        global $core,$C;

        // Create a Page Listing
        $this->pages = ceil($rows / $per_page);

        // If there's only one page, return now and don't bother
        if($this->pages == 1) {
            return;
        }

        // Pagination Prefix
             $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
        $output = "Pages: ";

        // Should we show the FIRST PAGE link?
        if($current_page > 2) {
            $output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
        }

        // Should we show the PREVIOUS PAGE link?
        if($current_page > 1) {
            $previous_page = $current_page - 1;
            $output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
        }

        // Current Page Number
        $output .= "<strong>[ ". $current_page ." ]</strong>";

        // Should we show the NEXT PAGE link?
        if($current_page < $this->pages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
        }

        // Should we show the LAST PAGE link?
        if($current_page < $this->pages - 1) {
            $output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
        }

        // Return the output.
        return $output;
    }
}

$display = new display;
echo $display->pagination("45", "10", "1","pagination.php");
?>[/B]

                </td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>

    </table></td>
<!-- body_text_eof //-->
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- right_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

The DEFINE_MAINPAGE2 have all the static contents.

Thanks!

Hi Murali...

Pls see below my code.

<?php

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CONDITIONS);

  $breadcrumb->add('Media News', 'press.php');
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="3" cellpadding="3">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><span>Media News</span></td>
            <td class="pageHeading" align="right"><?php  ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td class="main">
 <?php include(DIR_WS_LANGUAGES . $language . '/' . [B]FILENAME_DEFINE_MAINPAGE2[/B]); ?> 
[B]<?php

class display {
    function pagination($rows, $per_page, $current_page, $page_link) {
        global $core,$C;

        // Create a Page Listing
        $this->pages = ceil($rows / $per_page);

        // If there's only one page, return now and don't bother
        if($this->pages == 1) {
            return;
        }

        // Pagination Prefix
             $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
        $output = "Pages: ";

        // Should we show the FIRST PAGE link?
        if($current_page > 2) {
            $output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
        }

        // Should we show the PREVIOUS PAGE link?
        if($current_page > 1) {
            $previous_page = $current_page - 1;
            $output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
        }

        // Current Page Number
        $output .= "<strong>[ ". $current_page ." ]</strong>";

        // Should we show the NEXT PAGE link?
        if($current_page < $this->pages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
        }

        // Should we show the LAST PAGE link?
        if($current_page < $this->pages - 1) {
            $output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
        }

        // Return the output.
        return $output;
    }
}

$display = new display;
echo $display->pagination("45", "10", "1","pagination.php");
?>[/B]

                </td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>

    </table></td>
<!-- body_text_eof //-->
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- right_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->
    </table></td>
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

The DEFINE_MAINPAGE2 have all the static contents. This page my customer using the contents for cop/paste method continuously. But in the site, we need to display every 10 links have one page.

Thanks!

your customer continuosly updates that page. so store in database. then retrive from database. then paginate. its simple work

Hi all...

Thanks for your support.. I done using the dynamic page.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.