首页 文章

显示帖子总数

提问于
浏览
-4

我想显示帖子总数 . 我使用以下代码来显示表数据 .

<table>
    <tr>
    <th>Id</th>
    <th>title</th> 
    <th>com</th>
    <th>com1</th>
    </tr>
    <tr>
    <?php
        $limit=10;
        if(empty($_GET['p'])){
            $start=0;
        }else{
            $pi=$_GET['p'];
            $end=$pi*$limit;
            $start=$end-$limit;
        }
        if(!empty($_GET['s'])){
            $ss=$_GET['s'];
            $query="select * from sports where title like '%$ss%'";
        }else{
            $query="SELECT * FROM sports limit $start,$limit";
        }
        $result=mysqli_query($connect,$query);
        while($row=mysqli_fetch_assoc($result)){
    ?>
            <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['title'] ?></td>
            <td><?php echo $row['com'] ?></td>
            <td><?php echo $row['com1'] ?></td>
            </tr>   
    <?php 
        }
    ?>
</table>

我的表中总共插入了120条记录 . 请告诉我如何显示帖子总数,如果在表格中插入新数据,则计数会自动增加 .

我希望在开始表之前显示帖子总数 .

4 回答

  • 0

    如果我的问题是正确的,我认为您可以使用mysqli_num_rows来获取结果集中的元素数量:请在此处查看以供参考:mysqli_result::$num_rows

  • 1

    只需更新代码的这一部分:

    <?php
        $count = 1; // here
        $result=mysqli_query($connect,$query);
        while($row=mysqli_fetch_assoc($result)){
    ?>
            <tr>
            <td><?php echo $count; // here ?></td>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['title']; ?></td>
            <td><?php echo $row['com']; ?></td>
            <td><?php echo $row['com1']; ?></td>
            </tr>   
    <?php 
           $count++; // here
        }
    ?>
    

    这部分也是:

    <tr>
    <th>#</th>
    <th>Id</th>
    <th>title</th> 
    <th>com</th>
    <th>com1</th>
    </tr>
    
  • 0

    看到你的代码后,我认为你需要触发其他查询来获取记录的总数,你在那里获取分页数据 . 我已经更新了代码 .

    希望它会有所帮助 .

    <table>
    <tr>
        <th>Id</th>
        <th>title</th> 
        <th>com</th>
        <th>com1</th>
    </tr>
    <tr>
    <?php
        $limit=10;
    
        // Declared variable total
        $total = 0;
        if(empty($_GET['p'])) {
            $start=0;
        } else {
            $pi=$_GET['p'];
            $end=$pi*$limit;
            $start=$end-$limit;
        }
    
        if(!empty($_GET['s'])) {
            $ss=$_GET['s'];
            $query="SELECT * FROM sports WHERE title LIKE '%$ss%'";
            $result=mysqli_query($connect,$query);
    
            //Get total number of records for search query
            $total=$result->num_rows;
        } else {
            // Get total number of records for pagination
            $totalQuery = "SELECT * FROM sports";
            $totalResult = mysqli_query($connect, $totalQuery);
            $total = $totalResult->num_rows;
    
            // Get page data
            $query = "SELECT * FROM sports limit $start,$limit";
            $result = mysqli_query($connect,$query);
        }
    
        // you can echo $total to display total records
        // anywhere you want
    
        while($row=mysqli_fetch_assoc($result)) {
    ?>
            <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['title'] ?></td>
            <td><?php echo $row['com'] ?></td>
            <td><?php echo $row['com1'] ?></td>
            </tr>   
    <?php 
        }
    ?>
    
  • 0

    试试这个 . 在我的两个查询之间编写我的代码

    $result=mysqli_query($connect,$query);
     $count=mysqli_num_rows($result);//it will store your total selected row
     while($row=mysqli_fetch_assoc($result)){
     }
    

相关问题