2017-11-10 1 views
-2

Ajax와 PHP를 통해 긴 게시물을 페이지 매김하는 코드를 찾으려고합니다. 예 : 내 게시물의 길이가 2,000 단어를 넘었으므로이 페이지를 여러 페이지로 나누고 싶습니다. & 이전 링크 각 페이지는 500 단어를 가지고 있습니다. 총 4 개의 링크가 표시되어야합니다.Ajax PHP 기사 Paginator

내가 지금까지 얻은 것은 데이터베이스 레코드를 페이지 매기는 플러그인이다.

모든 리드가 높이 평가 될 것입니다.

+0

질문 권장하거나 책, 도구, 소프트웨어 라이브러리, 튜토리얼을 찾기 위해 우리를 묻는 또는 다른 오프 사이트 리소스는 독창적 인 답변과 스팸을 끌어 들이기 때문에 스택 오버플로에 대한 주제와 관련이 없습니다. 대신 문제를 설명하고 지금까지 해결 된 문제를 설명하십시오. –

+0

짧은 -> 아약스는 PHP 파일에 게시, PHP 파일 분할 된 데이터를 가져옵니다 (그래서 당신은 귀하의 쿼리에 대한 제한/오프셋 변수를 통과) 결과를 페이지로 반환합니다 .. PHP 페이지에서 당신이 만들 줄 이전/다음/전체 페이지 버튼에 대한 구조는 모든 정보를 제공합니다. – Matt

답변

0

기사에 얼마나 많은 문자가 있는지 확인하려면 PHP strlen() 함수를 사용할 수 있습니다. 그렇다면 그것이 500보다 큰 경우 $_GET ...으로 새 링크를 설정할 수 있습니다. 데이터베이스가 있다는 것은 잘 모르겠지만 기사를 mysql 데이터베이스에 저장하는 것이 좋습니다.

코드 :

// This first query is just to get the total count of rows 
$sql = "SELECT COUNT(id) FROM articles WHERE account_name=?"; // or you can use an strlen function here 
$stmt = $conn->prepare($sql); 
$stmt->bind_param("s",$u); 
$stmt->execute(); 
$stmt->bind_result($rows); 
$stmt->fetch(); 
$stmt->close(); 
// Here we have the total row count 
// This is the number of results we want displayed per page 
$page_rows = 10; 
// This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 
// This makes sure $last cannot be less than 1 
if($last < 1){ 
    $last = 1; 
} 
// Establish the $pagenum variable 
$pagenum = 1; 
// Get pagenum from URL vars if it is present, else it is = 1 
if(isset($_GET['pn'])){ 
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); 
} 
// This makes sure the page number isn't below 1, or more than our $last page 
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
} 
// This sets the range of rows to query for the chosen $pagenum 
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
// Establish the $paginationCtrls variable 
$paginationCtrls = ''; 
// If there is more than 1 page worth of results 
if($last != 1){ 
    /* First we check if we are on page one. If we are then we don't need a link to 
     the previous page or the first page so we do nothing. If we aren't then we 
     generate links to the first page, and to the previous page. */ 
    if ($pagenum > 1) { 
     $previous = $pagenum - 1; 
     $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$previous.'#posts">Previous</a> &nbsp; &nbsp; '; // here we set up the link 
     // Render clickable number links that should appear on the left of the target page number 
     for($i = $pagenum-4; $i < $pagenum; $i++){ 
      if($i > 0){ 
       $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$i.'#posts">'.$i.'</a> &nbsp; '; 
      } 
     } 
    } 
    // Render the target page number, but without it being a link 
    $paginationCtrls .= ''.$pagenum.' &nbsp; '; 
    // Render clickable number links that should appear on the right of the target page number 
    for($i = $pagenum+1; $i <= $last; $i++){ 
     $paginationCtrls .= '<a href="user.php?u='.$u.'&pn='.$i.'#posts">'.$i.'</a> &nbsp; '; 
     if($i >= $pagenum+4){ 
      break; 
     } 
    } 
    // This does the same as above, only checking if we are on the last page, and then generating the "Next" 
    if ($pagenum != $last) { 
     $next = $pagenum + 1; 
     $paginationCtrls .= ' &nbsp; &nbsp; <a href="user.php?u='.$u.'&pn='.$next.'#posts">Next</a> '; 
    } 
} 

난 당신이 바로이 비디오를 시청 원하는 작업 모르는 이후 : pagination

+0

Hello Mark Frankli, 답장을 보내 주셔서 감사합니다. 그러나 내가 지적한 바와 같이, 당신이 내게 주신 것은 데이터베이스 레코드를위한 Ajax 페이징 기입니다. 내가 필요한 건 기사의 아약 페이징 (AJAX paginator)이었습니다. 예를 들어 Wordpress의 게시물에는 긴 블로그 게시물을위한 다음과 이전 버튼이 있습니다. – rkelindungu

+0

매우 긴 기사가 하나만있는 경우 페이지 아래쪽에 도달 할 때마다 콘텐츠를로드하는 자동화 된 기능을 간단하게 사용할 수 있습니다. 하나의 기사를 더 많은 부분으로 나누는 것은 권하지 않지만, 정말로 원한다면 데이터베이스 상호 작용 부분을 삭제하고 다시 작성하여 위의 코드로 쉽게 할 수 있습니다. –