2017-06-05 71 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