PHP - 使用分页的MySQL查询

时间:2012-09-07 10:03:06

标签: php mysql pagination

我将如何为这个MySQL&编写分页脚本? PHP查询。

if (isset($_GET['c'])) {
$c = $_GET['c'];
}

$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");

        WHILE($datarows = mysql_fetch_array($query)):

        $id = $datarows['id'];
        $category = $datarows['category'];
        $code = $datarows['code'];

        endwhile;

$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

WHILE($datarows_cat = mysql_fetch_array($query2)):

        $title = $datarows_cat['title'];
        $description = $datarows_cat['description'];
        $imgurl = $datarows_cat['image_name'];
        $category = $datarows_cat['category'];
        $views = $datarows_cat['view_count'];
        $pagename = $datarows_cat['pagename'];
                $featured = $datarows_cat['featured'];

if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
                    if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }

if (strlen($description) > 95) {
    $desc= substr($description,0,95);
    $desmod = "$desc...<br/><a href=\"http://my-site.com/$pagename#1\" title=\"$description\">- Read More</a>";
    }
else {$desmod = "$description";}

        echo "$f - <a href=\"http://my-site.com/$pagename\">$title - $desmod</a><br/>";

endwhile;

例如,当我访问http://my-site.com/categories/Action时,代码在我的类别表中查找该类别,然后一旦获得该类别的唯一代码,它就运行另一个查询以查找另一个表中的所有游戏类别代码。但是,目前我为一个类别加载了200多个游戏,这会导致大量的加载时间。

感谢您的帮助!

4 个答案:

答案 0 :(得分:7)

首先要了解特定类别的游戏数量

更改行

$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);

在其后添加以下内容

$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);

if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start    = ($page - 1) * $per_page;
$sql     = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);

然后显示您想要的页数

<ul id="pagination">
        <?php
        //Show page links
        for ($i = 1; $i <= $pages; $i++)
          {?>
          <li id="<?php echo $i;?>"><a href="linktoyourfile?c=<?php echo $c;?>&page=<?php echo $i;?>"><?php echo $i;?></a></li>
          <?php           
          }
        ?>
      </ul>

使用CSS进行分页这样就可以了解

答案 1 :(得分:0)

initialize(button,R.id.button)

答案 2 :(得分:0)

    Pagiantion, it is working simple and easy 
       <?php  
        $sql = "SELECT COUNT(id) FROM contact_info";

        $rs_result  = $conn->query($sql); 
        $row = mysqli_fetch_row($rs_result);  
        $total_records = $row[0]; 
        echo $total_records;
        $previous   = 1;
        $total_pages = ceil($total_records / $limit); 
        $next  = $_GET["page"] + 1;
        $previous  = $_GET["page"] - 1;
        $pagLink = "<span>"; 
        if($previous ==0)
        {
        $prev  = "<a href='javascript:void(0);' >Previous</a>";
        }
        else
        {
          $prev  = "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$previous."' style='color:black;'>Previous</a>";   
        };
        echo $prev;

        "</span><div class='pagination'>";
        for ($i=1; $i<=$total_pages; $i++) 
        {  
        $pagLink .= "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$i."'>".$i."</a>";  
        };  
        echo $pagLink;

        $nex  = "<span><a href='http://homeacresfinefurniture.com/all-queries.php?page=".$next."' style='color:black;'>Next</a></span>";
        echo $nex;

        ";
        </div>";
      ?>

从数据库获取所有数据。     

            $limit = 1;  
            if (isset($_GET["page"])) 
            {
                $page  = $_GET["page"];
            }
            else 
            {
             $page=1; 
            }  
            $start_from = ($page-1) * $limit;     
            $sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";

答案 3 :(得分:-1)

//此代码可以根据您的要求进行定制 //通读代码注释以了解更多详细信息

访问 https://www.thewebblinders.in/programming/article/php-how-to-create-pagination-links-6022下载 PaginationLinks.php

<?php
include './PaginationLinks.php';
class DemoPagination
{

    /**
     *
     * @param number $requestedPage - current request page number
     * @param number $totalResults  - total number of results , this value may come from a database
     * @param number $nearbyLinks   - specifies how many pagination links to be displayed
     * @param number $numberOfResultsPerPage - number of results you want to display for each page
     * @param string $linksFormat   - specifies how pagination links should be displayed (HTML string)
     * @param string $currentPageLinkFormat - specifies how current page link should be displayed (HTML string - you could use this to highlight current page from the rest of the links)
     */
    public function getPagination(
        $requestedPage,
        $totalResults,
        $nearbyLinks = 5,
        $numberOfResultsPerPage=5,
        $linksFormat = '<a href="?page=%d">%d</a>',
        $currentPageLinkFormat = '<a class="currentPage" href="#">%d</a>') {

        $requestedPage=$this->validatePageNumber($requestedPage,$totalResults,$numberOfResultsPerPage);

        return PaginationLinks::create(
            $requestedPage,
            $this->getTotalPages($totalResults,$numberOfResultsPerPage),
            $nearbyLinks,
            $linksFormat,
            $currentPageLinkFormat
        ); 

    }
    /*
    If the requested page number is not in available page numbers range
    or
    if page number is not set or empty
    we will set page number to 1 (1st page)
     */
    public function validatePageNumber($requestedPage,$totalResults,$numberOfResultsPerPage)
    {
        try{
            return (!isset($requestedPage) || empty($requestedPage)  || !($this->isPositiveInteger($requestedPage)) || (ceil($totalResults / $numberOfResultsPerPage) < $requestedPage)) ? 1 : $requestedPage;
        }
        catch(Exception $e){
            return 1;
        }
    }
    /**
     * Returns total pages that can be created based on number of results and number of results per page
     */
    public function getTotalPages($totalResults,$numberOfResultsPerPage){
        return ceil($totalResults/$numberOfResultsPerPage);
    }

    // Used to make sure the requested page is positive
    public function isPositiveInteger($val)
    {
        $filter_options = array(
            'options' => array('min_range' => 0),
        );

        return filter_var($val, FILTER_VALIDATE_INT, $filter_options);
    }
}

$dp=new DemoPagination();
echo $dp->getPagination(2,45); // USAGE

您可以在此处找到更多有关如何使用上述代码的示例 https://www.thewebblinders.in/programming/article/php-how-to-create-pagination-links-6022