2012-09-14 4 views
0

저는 PHP에 익숙하지 않고 지난 며칠간 처음으로 프로젝트를 만들려고 노력했습니다. 대부분 블로그로 작동합니다. 데이터베이스와 기타 등을 쿼리하는 방법에 대한 느낌이 들었습니다.Digg 스타일 페이지 매김 클래스

는 내가 지금 내 페이지에 결과를 페이지를 매기 할 수 있도록하려면

, 이것은 내가 지금까지 무엇을 가지고 :

<?php 
include_once 'inc/functions.inc.php'; 
include_once 'inc/db.inc.php'; 

$db = new PDO(DB_INFO, DB_USER, DB_PASS); 

$id = (isset($_GET["id"])) ? (int) $_GET["id"] : NULL; 

$e = retrievePosts($db, $id); 

$fulldisp = array_pop($e); 
?> 
<div id="blogposts"> 
    <?php 
    if ($fulldisp == 1) { 
     ?> 
     <span class="postdate"><?php echo $e["date"] ?></span> 
     <span class="posttitle"><?php echo $e["title"] ?></span> 
     <br/> 
     <br/> 
     <span class="postbody"> 
      <?php echo $e["body"] ?> 
     </span> 
     <?php 
    }//end if 
    else { 
     foreach ($e as $entry) { 
      ?> 
      <div class="postholder"> 
       <span class="postdate"><?php echo $entry["date"] ?></span> 
       <span class="posttitle"> 
        <a href="?id=<?php echo $entry['id'] ?>"><?php echo $entry['title'] ?></a> 
       </span> 
       <br/> 
       <br/> 
       <span class="postbody"> 
     <?php echo $entry["resume"] ?> 
       </span> 
      </div> 
        <?php 
       }//end foreach 
      }//end 
      ?> 
</div> 

그리고이 제 기능 :

<?php 

function retrievePosts($db, $id = NULL) { 
    if (isset($id)) { //se foi fornecido ID 
     $sql = "SELECT title, body, date 
      FROM blog 
      WHERE id=? 
      LIMIT 1"; 
     $stmt = $db->prepare($sql); 
     $stmt->execute(array($_GET["id"])); 

     $e = $stmt->fetch(); //guardar em array 
     $fulldisp = 1; //uma só entrada 
    } 
    else 
    { 
     $sql = "SELECT id, title, resume, date 
      FROM blog 
      ORDER BY date DESC"; 

     foreach($db->query($sql) as $row) 
     { 
      $e[] = array("id" => $row["id"], "title" => $row["title"], "resume" => $row["resume"], "date" => $row["date"]); 
     } 
     $fulldisp = 0; //multiplas entradas 
    } 
    //funçoes só retornam um valor, inserimos fulldisp no fim de $e e separamos 
    //os valores de novo no index.php 
    array_push($e, $fulldisp); 
    return $e; 
} 

?> 

이 작동하는 방법입니다 , 페이지는 URL에 따라 작성됩니다.

거기에 ID가 있으면 해당 ID의 내용 만 표시됩니다.

URL에 ID가 없으면 페이지의 모든 항목이 표시됩니다.

는 지금 이러한 항목 페이지를 매기는 할 수 있도록하려면, 그래서 유래 주위에 모양을 가진 후에는 가장 인기있는 솔루션은 여기에 설명 된 디그 스타일 매김 클래스 사용하는 것입니다 보인다 http://mis-algoritmos.com/digg-style-pagination-class

나 '를 클래스를 다운로드했지만 올바르게 작동하는 방법을 알아낼 수 없다. 클래스에 DB 쿼리가 없다는 사실에 혼란 스럽다. (나는 LIMIT를 사용하기를 기대한다.) 나는 궁금해했다. 스택에있는 누군가가 내 코드에서이 클래스를 구현하는 데 도움이 될 수 있거나이를 수행하는 방법에 대한 문서화 된 출처가있는 경우.

답변

0
function retrievePosts($db, $id = NULL, $page = 1, $rpp = 5) { 
    if (isset($id)) { //se foi fornecido ID 
     $sql = "SELECT title, body, date 
      FROM blog 
      WHERE id=? 
      LIMIT 1"; 
     $stmt = $db->prepare($sql); 
     $stmt->execute(array($id)); 

     $e = $stmt->fetch(); //guardar em array 
     $fulldisp = 1; //uma só entrada 
    } 
    else 
    { 
     if(!is_numeric($page)) { 
      $page = 1; 
     } 

     if(!is_numeric($rpp)) { 
      $rpp = 5; 
     } 

     $sql = "SELECT id, title, resume, date 
      FROM blog 
      ORDER BY date DESC 
      LIMIT ? 
      OFFSET ? 
     "; 

     $stmt = $db->prepare($sql); 
     $stmt->execute(array($page, $rpp * $page)); 

     foreach($stmt->fetch() as $row) 
     { 
      $e[] = array("id" => $row["id"], "title" => $row["title"], "resume" => $row["resume"], "date" => $row["date"]); 
     } 
     $fulldisp = 0; //multiplas entradas 
    } 
    //funçoes só retornam um valor, inserimos fulldisp no fim de $e e separamos 
    //os valores de novo no index.php 
    array_push($e, $fulldisp); 
    return $e; 
} 

$의 RPP - 페이지 당 기록

또한 내 생각 :

$stmt->execute(array($_GET["id"])); 

가 있어야한다 :

$stmt->execute(array($id));