2017-06-05 70 views
0

我想創建一個從我的數據庫中顯示縮略圖的頁面。不過,我想創建一個分頁,以防我想更新我的數據庫。只有第3表應顯示在第1頁等等...分頁縮略圖PHP

這裏是我的縮略圖腳本

<?php 
    /* Attempt MySQL server connection. Assuming you are running MySQL 
    server with default setting (user 'root' with no password) */ 
    $link = mysqli_connect("localhost", "root", "", "test"); 

    // Check connection 
    if($link === false){ 
     die("ERROR: Could not connect. " . mysqli_connect_error()); 
    } 

    // Attempt select query execution 
    $sql = "SELECT * FROM news limit 3"; 
    if($result = mysqli_query($link, $sql)){ 
     if(mysqli_num_rows($result) > 0){ 

      echo "<div class=\"container\">"; 
      echo "<table>"; 
      echo "<tr>"; 
      echo "</tr>"; 
      while($row=mysqli_fetch_array($result)){ 

       echo "<div class=\"col-md-4\">"; 
       echo "<div class=\"thumbnail\">"; 
       echo "<img alt=\"News\" src=\"images/{$row["image"]}\">"; 
       echo "<div class=\"caption\">"; 
       echo "<b><h4>{$row["title"]}</b></h4>"; 
       echo "<p>{$row["caption"]}</p>"; 
       echo "<p align=\"right\">"; 
       echo "<a class=\"btn btn-primary\" href=\"{$row["newsupdate"]}\">Read More</a>"; 
       echo "</p>"; 
       echo "</div>"; 
       echo "</div>"; 
       echo "</div>"; 
       echo "</div>"; 
      } 
      echo "</table>"; 

      // Free result set 
      mysqli_free_result($result); 
     } else{ 
      echo "No records matching your query were found."; 
     } 
    } else{ 
     echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
    } 

    // Close connection 
    mysqli_close($link); 
?> 

<ul class="pagination"> 
    <li><a href="#">1</a></li> 
    <li><a href="#">2</a></li> 
    <li><a href="#">3</a></li> 
    <li><a href="#">4</a></li> 
    <li><a href="#">5</a></li> 
</ul> 
+0

sql查詢的分頁部分不存在 - 您需要一起使用當前頁面和限制 - 遞增頁面以繼續/返回到先前的結果。即:'SELECT * FROM news limit {$ currentpage},3' - 您如何確定記錄總數以及各種正向/反向鏈接是否涉及更多 – RamRaider

回答

0

爲了通過您的記錄集進行分頁,從而一次將顯示限制爲指定數量的記錄,您需要更改sql查詢以使用limit子句的offsettotal值。通常分頁使用GET變量來確定用戶當前所在的頁面,並在SQL查詢中使用此變量。

以下是我剛剛通過特定記錄集寫入分頁的基本示例 - 使用代碼並進行更改以適合您的特定查詢應該很直接。

<?php 
    $dbhost = 'localhost'; 
    $dbuser = 'xxx'; 
    $dbpwd = 'xxx'; 
    $dbname = 'xxx'; 
    $db  = new mysqli($dbhost, $dbuser, $dbpwd, $dbname); 
?> 
<!doctype html> 
<html> 
    <head> 
     <title>Basic Pagination</title> 
     <style> 
      #paging{border-top:1px solid black;margin:1rem auto;float:none;clear:both;} 
      #paging *{margin:0.5rem 1rem;clear:none!important;display:inline-block;} 
      .minimal{margin:0.5rem 0.1rem!important;} 
     </style> 
    </head> 
    <body> 
     <div id='results'> 
      <?php 
       /* Single user supplied parameter - the PAGE */ 
       $page = isset($_GET['page']) ? filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT) : 0; 


       if(is_numeric($page) && $page >= 0){ 

        $rpp = 10; /* Records Per Page */ 
        $offset = $page * $rpp; /* paging offset */ 

        /* 
         In order to determine the paging you need to know the total number of records available 
         that are returned by the query with any given WHERE clause. The sub-query therefore uses 
         the same WHERE clause and returns a number to be used later. 

         Edit the sql to suit your needs and edit the data that is rendered in the while loop. 
        */ 
        $sql="select 
         (select count(*) from `maps` where `location_enabled`=1) as 'count', 
         `id`, 
         `location_name` as 'name' 
         from `maps` 
         where `location_enabled`=1 
         limit {$offset},{$rpp}"; 



        /* 
         Ignoring the possible SQL injection vulnerability - run the query 
        */ 
        $results=$db->query($sql); 

        if($results && $results->num_rows > 0){ 

         /* Process the recordset however is appropriate for your use-case */ 
         while($rs=$results->fetch_object()){ 

          /* From sql query, the total number of records that satisfy the "WHERE" clause */ 
          $count=$rs->count; 

          /* output data - thumbnails etc etc */ 
          echo "<div>{$rs->id} - {$rs->name}</div>"; 
         } 


        } else { 
         echo "Error! Unable to fetch results"; 
        } 
       } else { 
        echo "Error... Bad foo!"; 
       } 
      ?> 
     </div> 
     <div id='paging'> 
      <?php 
       /* 
        Calculate links for basic pagination 
        (First,Previous,Next,Last) 
       */ 
       if(is_numeric($page)){ 

        $total_pages = floor($count/$rpp); 

        $text='First'; 
        if($page == 0){ 
         echo "<div>$text</div>"; 
        } else { 
         echo "<a href='?page=0'>$text</a>"; 
        } 

        $text='Previous'; 
        if($page > 0){ 
         echo "<a href='?page=".max(0, $page - 1)."'>$text</a>"; 
        } else { 
         echo "<div>$text</div>"; 
        } 

        $text='Next'; 
        if($page >= $total_pages){ 
         echo "<div>$text</div>"; 
        } else { 
         echo "<a href='?page=".min($total_pages, $page + 1)."'>$text</a>"; 
        } 

        $text='Last'; 
        if($page==$total_pages){ 
         echo "<div>$text</div>"; 
        } else { 
         echo "<a href='?page={$total_pages}'>$text</a>"; 
        } 


        /* Generate basic hyperlink for each possible page */ 
        for($i=0; $i <= $total_pages; $i++) { 
         echo "<a class='minimal' href='?page={$i}'>".($i + 1)."</a>"; 
        } 
       } 
      ?> 
     </div> 
    </body> 
</html> 
<?php 

    $db->close(); 

?> 
+0

無法獲取結果?我改變了sql已經 –

+0

你可以把你的原始問題更新顯示什麼/你是如何實現分頁系統? – RamRaider