2016-04-05 54 views
0

我目前正在創建一個小應用程序,它具有很多內容,我想讓這些內容以有組織的方式顯示,並且我可以看到這是使用分頁的最佳方式。我已經瀏覽了bootstrap文檔,但是我無法解決這個問題,因爲我之前從未使用過它。如何使用Netbeans使用引導3分頁與PHP MVC?

型號:

public function showFilms($UserID){ 

    //declare new film array 
    $film = array(); 

    //SQL Query Selecting equivalent information to be displayed 
    $sqlQuery = 'SELECT FilmID, Title, Director,YearofRelease, Genre, Image, MainActor, MainActor1, MainActor2, Synopsis FROM films WHERE User ="' . $UserID . '" ORDER BY Title DESC'; 

    // prepare a PDO statement 
    $statement = $this->_dbHandle->prepare($sqlQuery); 
    // Execute PDO statement 
    $statement->execute(); 

    // While loop fetches each row matching query 
    while ($row = $statement->fetch()) { 
     $film[] = array('FilmID' => $row['FilmID'], 
      'Title' => $row['Title'], 
      'User' => $UserID, 
      'Director' => $row['Director'], 
      'YearofRelease' => $row['YearofRelease'], 
      'Genre' => $row['Genre'], 
      'Image' => $row['Image'], 
      'MainActor' => $row['MainActor'], 
      'MainActor1' => $row['MainActor1'], 
      'MainActor2' => $row['MainActor2'], 
      'Synopsis'=> $row['Synopsis'] 
     ); 

    } 
    //returning film array 
    return $film; 

} 

查看:

<div id="FilmArea"> 
    <!-- Count for Films --> 
    <?php 
    if (count($film)) { 
     ?> 
     <br> 
     <!-- Displaying each entry using count of hidden list value of id of entry --> 
     <?php foreach ($film as $list): ?> 
      <!-- Panel Start --> 
      <div class="panel panel-info"> 

       <!-- Panel Header --> 
       <div class="panel-heading"> 
        <h3 class="panel-title" style="font-family: sans-serif; font-size: 20px; color: black; "> 
         <?= ($list['Title']) ?> <small> Directed By : <?= ($list['Director']) ?></small> 
        </h3> 
       </div> 
       <!-- Panel Body --> 
       <div class="panel-body"> 
        <div id='dvdHolder'> 
         <?= ($list['Image'] <> "" ? "<img style='max-width:200px; max-height:200px;' src='Images/{$list['Image']}'/>" : "") ?> 
        </div> 
        <div id='dvdInfo'> 
         <p style='font-weight: bold;'> Year Released : <?= ($list['YearofRelease']) ?></p> 
         <p style='font-weight: bold;'> Film Genre : <?= ($list['Genre']) ?></p> 
         <p style="font-weight: bold;"> Starring : <?= ($list['MainActor']) ?> , <?= ($list['MainActor1']) ?> , <?= ($list['MainActor2']) ?> </p> 
         <p style='font-weight: bold;'> Synopsis : <?= ($list['Synopsis']) ?></p> 
        </div> 
       </div> 
      </div> 
      <!-- End of Foreach Statement for Entry --> 
     <?php endforeach; ?> 
     <!-- Else show this message if user has no entries --> 
     <?php 
    }else { 
     ?> 
     <p><b>No Films Added yet!</b></p> 
    <?php } ?> 
</div> 

控制器:

<?php 
session_start(); 

require_once('Model/UserDataSet.php'); 
require_once('Model/FilmDataSet.php'); 

$view = new stdClass(); 
$view->pageTitle = 'My Vault'; 




$aa = new FilmDataSet(); 
$film = array(); 
$film = $aa->showFilms($_SESSION['Email']); 



require_once('View/home.phtml'); 

Screenshot of Application

我如何能實現分頁任何幫助或信息進入我的應用程序將是偉大的。

+0

您使用的是什麼數據庫引擎? –

+0

您好我正在使用phpMyAdmin – MrWeiser

回答

0

首先,您必須編輯查詢以獲取每頁顯示的記錄數。

$sqlQuery = 'SELECT FilmID, Title, Director,YearofRelease, Genre, Image, MainActor, MainActor1, MainActor2, Synopsis FROM films WHERE User ="' . $UserID . '" ORDER BY Title DESC LIMIT '.$perPage.' OFFSET '.(($page - 1)*$perPage).''; 

要呈現頁數,您必須計算總記錄數以瞭解您擁有多少頁。當你渲染的頁數引導必須把鏈接到頁面的數量,例如:

<li><a href="localhost:3000/films?page=1">1</a></li> 
<li><a href="localhost:3000/films?page=2">2</a></li> 
<li><a href="localhost:3000/films?page=3">3</a></li> 

控制器,你必須捕捉的頁面在這種情況下:

$page = htmlspecialchars($_GET["page"]); 
$film = $aa->showFilms($_SESSION['Email'], $page); 

也可以看看這個教程,有實現的方式與類分頁:

http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928

最後,我建議你使用MVC弗拉姆ework,這可以幫助您大大提高代碼的組織性,安全性和分頁選項。

+0

感謝您取回答案我已嘗試更改SQL,因爲您已經提示與其他事情一起,但它出現此錯誤 - 警告:缺少FilmDataSet :: showFilms()的參數3,在第14行調用C:\ Users \ lenny \ Desktop \ TheVault \ vault.php並在第29行中的C:\ Users \ lenny \ Desktop \ TheVault \ Model \ FilmDataSet.php中定義。這是SQL語句的行我改變了 – MrWeiser

+0

是的,限制和偏移量將會到語句的結尾,我只是編輯它。 –