2013-11-26 18 views
-1

我希望做一個分頁我的文章..我想要做一個面向對象的分頁我的文章

這裏是類:

<?php 

class Mynews { 

    public $conn; 

    public function __construct() { 
     $this->conn = mysqli_connect("localhost", "root", "", "mynews"); 
    } 

    public function readAllarticles() { 
     $sql = "SELECT * FROM articles WHERE status='publish'"; 
     $query = mysqli_query($this->conn, $sql); 
     return mysqli_fetch_all($query, MYSQLI_ASSOC); 
    } 

} 

$obj = new Mynews; 
?> 

display post: <?php 
include('includes/crud.php'); 

foreach ($obj->readAllarticles() as $art) { 
    extract($art); 
    ?> 
    <h3><?php echo $title; ?></h3> 
    <p> 
     <?php 
     $post = explode(" ", $content); 
     $slice = array_slice($post, 0, 10); 
     echo implode(" ", $slice), '...'; 
     ?> 
    </p> 

    <?php 
} 
?> 

現在我想的功能我班上做一個分頁.. 。和我的壞工程抱歉.. :(

+0

好吧,那就請便吧。 – deceze

+0

從關於頁面:'不要問你沒有試圖找到答案的問題(顯示你的工作!)'http://stackoverflow.com/about http://www.whathaveyoutried.com/ – LuckySpoon

回答

1

您可以使用它:

<?php 

class Pagination{ 
function Paginate($values,$per_page){ 
$total_values = count($values); 

if(isset($_GET['page'])){ 
$current_page = $_GET['page']; 
}else{ 
$current_page = 1; 
} 
$counts = ceil($total_values/$per_page); 
$param1 = ($current_page - 1) * $per_page; 
$this->data = array_slice($values,$param1,$per_page); 

for($x=1; $x<= $counts; $x++){ 
$numbers[] = $x; 
} 
return $numbers; 
} 
function fetchResult(){ 
$resultsValues = $this->data; 
return $resultsValues; 
} 
} 


// Sample Usage 

$pag = new Pagination(); 
$data = array("Hello","Rex","Prosper","Adrivan","Hehe"); 
$numbers = $pag->Paginate($data,2); 
$result = $pag->fetchResult(); 
foreach($result as $r){ 
echo '<div>'.$r.'</div>'; 

} 

foreach($numbers as $num){ 
echo '<a href="classpagination.php?page='.$num.'">'.$num.'</a>'; 
} 



?> 
0

我用這個和它的作品對我來說

PHP

function pagination_one($webpage, $total_pages,$page){ 

    // Maximum number of links per page. If exceeded, google style pagination is generated 
    $max_links = 6; 
    $h=1; 
    if($page>$max_links){ 
     $h=(($h+$page)-$max_links); 
    } 
    if($page>=1){ 
     $max_links = $max_links+($page-1); 
    } 
    if($max_links>$total_pages){ 
     $max_links=$total_pages+1; 
    } 
    echo '<div class="page_numbers"> 
     <ul>'; 
    if($page>"1"){ 
     echo '<li class="current"><a href="'.$webpage.'?pagenum=1">First</a></li> 
       <li class="current"><a href="'.$webpage.'?pagenum='.($page-1).'">Prev</a></li> '; 
    } 

    if($total_pages!=1){ 
     for ($i=$h;$i<$max_links;$i++){ 
      if($i==$page){ 
       echo '<li><a class="current">'.$i.'</a></li>'; 
      } 
      else{ 
       echo '<li><a href="'.$webpage.?pagenum='.$i.'">'.$i.'</a> </li>'; 
      } 
     } 
    } 

    if(($page >="1")&&($page!=$total_pages)){ 
     echo '<li class="current"><a href="'.$webpage.'?pagenum='.($page+1).'">Next</a></li> 
       <li class="current"><a href="'.$webpage.'?pagenum='.$total_pages.'">Last</a></li>'; 
    } 

    echo '</ul> </div>'; 
} 

function pagination_one($webpage, $total_pages,$page){ 

    // Maximum number of links per page. If exceeded, google style pagination is generated 
    $max_links = 6; 
    $h=1; 
    if($page>$max_links){ 
     $h=(($h+$page)-$max_links); 
    } 
    if($page>=1){ 
     $max_links = $max_links+($page-1); 
    } 
    if($max_links>$total_pages){ 
     $max_links=$total_pages+1; 
    } 
    echo '<div class="page_numbers"> 
     <ul>'; 
    if($page>"1"){ 
     echo '<li class="current"><a href="'.$webpage.'?pagenum=1">First</a></li> 
       <li class="current"><a href="'.$webpage.'?pagenum='.($page-1).'">Prev</a></li> '; 
    } 

    if($total_pages!=1){ 
     for ($i=$h;$i<$max_links;$i++){ 
      if($i==$page){ 
       echo '<li><a class="current">'.$i.'</a></li>'; 
      } 
      else{ 
       echo '<li><a href="'.$webpage.?pagenum='.$i.'">'.$i.'</a> </li>'; 
      } 
     } 
    } 

    if(($page >="1")&&($page!=$total_pages)){ 
     echo '<li class="current"><a href="'.$webpage.'?pagenum='.($page+1).'">Next</a></li> 
       <li class="current"><a href="'.$webpage.'?pagenum='.$total_pages.'">Last</a></li>'; 
    } 

    echo '</ul> </div>'; 
} 


// get the pagenum. If it doesn't exist, set it to 1 
if(isset($_GET['pagenum']) ? $page = $_GET['pagenum']:$page = 1); 
// set the number of entries to appear on the page 
$entries_per_page = 6; 
// total pages is rounded up to nearest integer 
$total_pages = ceil($getresult/$entries_per_page); 
// offset is used by SQL query in the LIMIT 
$offset = (($page * $entries_per_page) - $entries_per_page); 


$sql = "SELECT * FROM articles WHERE status='publish' LIMIT $offset,$entries_per_page"; 

// do your query results 

pagination_one('articles.php', $total_pages,$page); 

CSS

.page_numbers { 
    width:100%; 
    background:#fff9f0; 
    overflow:hidden; 
    position:relative; 
    padding:50px 0; 
} 
.page_numbers ul, .pagenums ul { 
    clear:left; 
    float:left; 
    list-style:none; 
    margin:0; 
    padding:0; 
    position:relative; 
    left:50%; 
    text-align:center; 
} 
.page_numbers ul li,.pagenums ul li { 
    display:block; 
    float:left; 
    list-style:none; 
    margin:1px; 
    padding:0; 
    position:relative; 
    right:50%; 
    background: #a8a189; 
    width:25px; 

} 
.page_numbers ul li a, .pagenums ul li a { 
    display:block; 
    background: #fff; 
    border: 1px solid #a8a189; 
    padding:3px 6px; 
    text-decoration: none; 
    color: #7a7564; 
    font:bold 11px arial, verdana,sans-serif; 
} 

.page_numbers li.current, 
.pagenums li.current{ 
    width:50px; 
} 

.page_numbers a.current, .page_numbers li a:hover, 
.pagenums a.current, .pagenums li a:hover { 
    background: #a8a189; 
    color: #fff; 
} 
相關問題