3

我将如何为此 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

3 回答 3

11

首先找出特定类别有多少游戏

换行

$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 进行分页,这样就可以了

于 2012-09-07T10:21:06.290 回答
0
//database connation
<?php
    $conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
<html>enter code here
<head>
    <title>View Student Details</title>
    <h1 align="center"> Student Details </h1>

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

</head>
<body>

    <div class="row">
        <div class="col-sm-2"></div>
        <div class="col-sm-8">

            <form>
                <table class="table table-striped">
                    <tr>
                        <th>Sr.No.</th>
                        <th>Student ID</th>
                        <th>Student Name</th>
                        <th>Class</th>
                        <th>Gender</th>
                        <th>Birth of Date</th>
                        <th>Contact No.</th>
                        <th>Action</th>
                    </tr>

                    <?php 
                        $count=0;

                        if(isset($_GET['page_count']))
                        {
                            $count=1;
                            $page_count=$_GET['page_count']-1;
                            $count=$page_count*10;
                        }

                        $q="SELECT * from student_detail LIMIT $count,10";
                        $result=$conn->query($q);
                        $j=0;
                        while($data=$result->fetch_array())
                        { $j=$j+1;
                    ?>

                    <tr>
                        <td><?php echo $j ?></td>
                        <td><?php echo $data['std_id'] ?></td>
                        <td><?php echo $data['std_name'] ?></td>
                        <td><?php echo $data['std_class'] ?></td>
                        <td><?php echo $data['gender'] ?></td>
                        <td><?php echo $data['bod'] ?></td>
                        <td><?php echo $data['contact'] ?></td>
                        <td>
                            <div class="row">
                                <div class="col-sm-12">
                                    <a href="delete_master.php?std_id=<?php echo $data['std_id']; ?>" class="btn btn-danger">Delete</a>
                                    <a href="update.php?std_id=<?php echo $data['std_id']; ?>" class="btn btn-danger">Update</a>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <?php } ?>
                </table>

                <ul class="pagination">
                    <?php 

                        $q="SELECT count(std_id) from student_detail";
                        $result=$conn->query($q);
                        $data=$result->fetch_array();
                        $total=$data[0];
                        $total_page=ceil($total/10);

                        if($total_page>1)
                        {
                            for($i=1;$i<=$total_page;$i++)
                            {
                    ?>
                                <li class="active"><a href="view.php?page_count=<?php echo $i;?>" name="page_count" id="page_count"><?php echo $i; ?></a></li>
                    <?php
                            }
                        }   
                    ?>
                </ul>
            </form>
            <div class="col-sm-2"></div>
        </div>
    </div>
</body>
</html>
于 2017-11-24T07:07:21.047 回答
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";
于 2018-06-29T10:24:23.777 回答