[Resolvido] Paginação
Olá pessoal! http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif
Primeiro queria agradecer a equipe do fórum, especialmente ao pessoal da parte de PHP, pois tenho sido muito ajudado pelos scripts e dúvidas que são postadas aqui no fórum. Sempre que quero algum script ou tenho alguma dúvida procuro no fórum, mas desta vez não encontrei algo parecido com o que pretendo fazer em meu script... é seguinte, vou explicar:
Tenho um sistema de notícias que funciona muito bem, e quando posto uma nova notícia, esta nova notícia aparece no topo, ao adicionar uma nova notícia, a primeira desce e a nova vai pro topo, assim sucessivamente... mas ao adicionar um sistema de paginação tudo isso mudou, quando adiciono uma nova notícia ela vai pro fim e não pro topo, como era antes do sistema de paginação... vocês podem me ajudar?
Segue o código da página onde são exibidas as notícias:
<?php include("../restringir.php"); ?>
<?php include("../SQL.php"); ?>
<?php
$data = date ("d/m/Y",time());
$hora = strftime ("%H:%M");
?>
<?php
include_once('pagination.php');
$localhost = "localhost";
$username = "root";
$password = "";
$database = "sistema";
$connection = mysql_connect($localhost,$username,$password)
or die(mysql_error());
if($connection){
mysql_select_db($database,$connection)
or die(mysql_error());
}else{
echo "Nao conectei ao banco de dados";
}
?><!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"](http://www.w3.org/1999/xhtml) xml:lang="pt" lang="pt-br">
<head>
<title>Zóio TV - Sistema de Administração</title>
<meta name="author" content="Zie Design - Soluções inteligentes" />
<meta name="content-language" content="pt-br" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="css/estilos.css" type="text/css" />
</head>
<body>
<h2>Notícias</h2>
<?php
$entries_per_page=10;
$page = (isset($_GET['page'])?$_GET['page']:1);
$result = mysql_query("SELECT COUNT(*) from noticias ")
or die (mysql_error());
$num_rows = mysql_fetch_row($result);
if($num_rows[0]!=0){
$total_pages = ceil($num_rows[0]/$entries_per_page);
$pagination = pagination_six($total_pages,$page);
$offset = (($page * $entries_per_page) - $entries_per_page);
$result = mysql_query("SELECT * from noticias LIMIT $offset,$entries_per_page")
or die (mysql_error());
echo $pagination;
for($i=0;$row=mysql_fetch_assoc($result);$i++){
echo "<div id=\"noticias\">
<p><span id=\"titulo\">{$row['nid']}{$row['titulo']}</span> [<span id=\"data\">{$row['data']}</span>]</p>
<p><img src=\"{$row['foto']}\" width=100 height=100></img></p>
<p id=\"chamada\">{$row['chamada']}</p>
</div> <!-- noticias -->";
};
echo $pagination;
}
?>
</body>
</html>
Aqui a arquivo pagination.php
<?php
function pagination_six($total_pages,$page){
global $webpage;
$pagination = '<div class="page_numbers">
<ul>';
if($total_pages!=1){
//the total links visible
$max_links=10;
//$max links_marker is the top of the loop
//$h is the start
$max_links_marker = $max_links+1;
$h=1;
//$link_block is the block of links on the page
//When this is an integer we need a new block of links
$link_block=(($page-1)/$max_links);
//if the page is greater than the top of th loop and link block
//is an integer
if(($page>=$max_links_marker)&&(is_int($link_block))){
//reset the top of the loop to a new link block
$max_links_marker=$page+$max_links;
//and set the bottom of the loop
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if not an integer we are still within a link block
elseif(($page>=$max_links_marker)&&(!is_int($link_block))){
//round up the link block
$round_up=ceil($link_block);
$new_top_link = $round_up*$max_links;
//and set the top of the loop to the top link
$max_links_marker=$new_top_link+1;
//and the bottom of the loop to the top - max links
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if greater than total pages then set the top of the loop to
// total_pages
if($max_links_marker>$total_pages){
$max_links_marker=$total_pages+1;
}
//first and prev buttons
if($page>'1'){
$pagination.='<li class="current"><a href="'.$webpage.'?page=1">Inicio</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Ant</a></li>';
}
//provide a link to the previous block of links
$prev_start = $h-$max_links;
$prev_end = $h-1;
if($prev_start <=1){
$prev_start=1;
}
$prev_block = "$prev_start a $prev_end";
if($page>$max_links){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.$prev.'">'.$prev_block.'</a></li>';
}
//loop through the results
for ($i=$h;$i<$max_links_marker;$i++){
if($i==$page){
$pagination.= '<li><a class="current">'.$i.'</a></li>';
}
else{
$pagination.= '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>';
}
}
//provide a link to the next block o links
$next_start = $max_links_marker;
$next_end = $max_links_marker+$max_links;
if($next_end >=$total_pages){
$next_end=$total_pages;
}
$next_block = "$next_start a $next_end";
if($total_pages>$max_links_marker-1){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.$max_links_marker.'">'.$next_block.'</a></li>';
}
//link to next and last pages
if(($page >="1")&&($page!=$total_pages)){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Prox</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Final</a></li>';
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
$pagination.='</ul>
</div>';
return($pagination);
}
Desde já agradeço.
Discussão (2)
Carregando comentários...