php - Mysql Php varios 表分页

标签 php mysql pagination

我有这个分页脚本

1 个显示页面

$result_p = mysqli_query($conexao, "select count(*) as count FROM `banner-destaque`"); 
$row_p = mysqli_fetch_array($result_p);
$quant_resul = 10; 

        $pagina = 1; 
        $paginas = ceil($row_p['count'] / $quant_resul);
        $result = mysqli_query($conexao, "select * FROM `banner-destaque` limit 0 , " . $quant_resul);

该脚本对一个 mysql 表运行良好,但我需要对其他大小不同的 mysql 表重用相同的分页脚本。

这是显示结果的表格

<table class="flat-table flat-table-1">
    <thead>
<th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
<th>Photo</th> 
<th>Title</th>
<th>Description</th>

    </thead>
    <tbody>
    <tr>

<?php
                    while ($row = mysqli_fetch_object($result)) {   
                    echo
'
        //and here i need get all columns values to populate the above column.
    <td>'.$row->id.'</td>  
    <td>'.$row->foto.'</td>
    <td>'.$row->titulo.'</td>
    <td>'.$row->descricao.'</td>';


                };


                ?>

有什么解决办法吗?谢谢。我正在使用 mysqli 连接到数据库。

最佳答案

从查询脚本创建一个函数,然后将数据库表名称作为参数传递给它来调用它。然后您可以将其重新用于不同的表

函数分页($conexao,$tblname){

        $result_p = mysqli_query($conexao, "select count(*) as count FROM ".$tblname); 
        $row_p = mysqli_fetch_array($result_p);
        $quant_resul = 10; 

        $pagina = 1; 
        $paginas = ceil($row_p['count'] / $quant_resul);
        $result = mysqli_query($conexao, "select * FROM ".tblname." limit 0 , " . $quant_resul);

        return $result;
    }


    <table class="flat-table flat-table-1">
    <thead>
    <th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
    <th>Photo</th> 
    <th>Title</th>
    <th>Description</th>

        </thead>
        <tbody>
        <tr>

    <?php
                      $result = paginate($conexao, 'banner-destaque');
                        while ($row = mysqli_fetch_object($result)) {   
                        echo
    '
            //and here i need get all columns values to populate the above column.
        <td>'.$row->id.'</td>  
        <td>'.$row->foto.'</td>
        <td>'.$row->titulo.'</td>
        <td>'.$row->descricao.'</td>';


                    };


            ?>

关于php - Mysql Php varios 表分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26913485/

相关文章:

mysql - 使 SQL 查询更快

'WHERE (col1, col2) < (val1, val2)' 的 SQL 语法术语

java - JDBI中如何动态绑定(bind)一个表名

php - MSSQL php pdo 分页,bindParam 上有些错误

php - 在 WordPress 中创建自定义小部件

php - 在 OS X 10.11 El Capitan、macOS 10.12 Sierra、macOS 10.13 High Sierra (< 10.13.3) 上安装 pecl 和 pear

MySQL:按小时和其他不同字段分组

jquery - 使用 ASP.NET MVC 和 AJAX 的分页表

laravel-4 - 如何自动将查询字符串附加到 laravel 分页链接?

php - Yii - 方法在此 View 中包含另一个 View