2011-10-25 4 views
0

두 가지 작은 문제점이 있습니다.페이지 번호 옆에 이전 (뒤로) 및 다음 버튼 추가하기 PHP

1. 이 코드에 이전/다음 버튼을 추가하고 싶습니다. 2. 이전과 다음 사이의 링크를 최대 10 개까지만 표시하고 싶습니다. 50 개의 숫자/링크가 있다면 페이지에 50 개의 링크가 아니라 10 개의 링크 만 표시됩니다. 내가 코드가 작동 클리오

에 검색 한

는 만에 그 두 가지 옵션이 필요합니다. 누군가 나를 도울 수 있습니까? 고맙습니다 !

<?php 

     include 'includes/connection.php'; 
     $per_page = 8; 

     $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`"); 
     $pages = ceil(mysql_result($pages_query, 0)/$per_page); 

     $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
     $start = ($page - 1) * $per_page; 



     $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page"); 
     while ($query_row = mysql_fetch_assoc($query)) { 
      echo '<p>', $query_row['name'] ,'</p>'; 
     } 

     if ($pages >= 1 && $page <= $pages) { 
      for ($x=1; $x<=$pages; $x++) { 
       //echo $x, ' '; 

       echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> '; 
      } 
     }else{ 
      header("location:index.php?page=1"); 
     } 


    ?> 

답변

1

첫째, 당신은 단지 좋은 습관를 들어, "종료;"놓아야합니다 끝에 header()를 호출 한 후. 이 특정 스크립트에서는 차이가 없지만 리디렉션 전에 헤더 ("위치 : ...") 호출 다음의 모든 코드가 실행된다는 것을 명심하십시오. 귀하의 질문에 이제

이 (UPDATE. :이 코드는 테스트 및 작동되었습니다) 시도

<?php 
include 'includes/connection.php'; 
$per_page = 8; 
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`"); 
$pages = ceil(mysql_result($pages_query, 0)/$per_page); 

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
$start = ($page - 1) * $per_page; 

$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page"); 
while ($query_row = mysql_fetch_assoc($query)) 
{ 
    echo '<p>' . $query_row['name'] . '</p>'; 
} 

// If the requested page is less than 1 or more than the total number of pages 
// redirect to the first page 
if($pages < 1 || $page > $pages) 
{ 
    header('Location: ?page=1'); 
    // end execution of the rest of this script 
    // it will restart execution after redirection 
    exit; 
} 
// If more than one page, show pagination links 
if($pages > 1) 
{ 
    $html = array(); 
    $html[] = '<strong>'; 
    // if you're on a page greater than 1, show a previous link 
    $html[] = (($page > 1) ? '<a href="?page=' . ($page - 1) . '">Previous</a> ' : ''); 
    // First page link 
    $pageFirst = '<a href="?page=1">1</a>'; 
    $html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst); 

    if ($pages > 6) 
    { 
     $start_cnt = min(max(1, $page - (6 - 1)), $pages - 6); 
     $end_cnt = max(min($pages, $page + 4), 8); 

     $html[] = ($start_cnt > 1) ? '...' : ' '; 

     for ($i = $start_cnt + 1; $i < $end_cnt; $i++) 
     { 
      $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>'; 
      if ($i < $end_cnt - 1) 
      { 
       $html[] = ' '; 
      } 
     } 

     $html []= ($end_cnt < $pages) ? '...' : ' '; 
    } 
    else 
    { 
     $html[] = ' '; 

     for ($i = 2; $i < $pages; $i++) 
     { 
      $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>'; 
      if ($i < $pages) 
      { 
       $html[] = ' '; 
      } 
     } 
    } 
    // last page link 
    $pageLast = '<a href="?page=' . $pages . '">' . $pages . '</a>'; 
    $html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast); 
    // Show next page link if you're on a page less than the total number of pages 
    $html[] = ($page < $pages) ? ' <a href="?page=' . ($page + 1) . '">Next</a>' : ''; 
    // If you're not on the last page, show a next link 
    $html[] = '</strong>'; 
} 
else 
{ 
    // show page number 1, no link. 
    $html[] = '<strong>1</strong>'; 
} 
echo implode('', $html); 

또한주의 그 마지막> 다음 HTML 코드가없는 PHP 파일에 필요하지? PHP 코드, 그래서 그것을 떠났다.

+0

시간 내 주셔서 감사합니다. 페이지에 표시 : 1 2 1 "> 다음 및 -1"> 이전 1 2 무엇이 잘못 되었습니까? – MOTIVECODEX

+0

또한 이전과 다음은 링크가 없습니다 – MOTIVECODEX

+1

나는 다소 재 작성 작업을하고 있습니다. – imkingdavid

관련 문제