用于varios表的Mysql Php分页

时间:2014-11-13 16:18:46

标签: 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连接数据库。

1 个答案:

答案 0 :(得分:0)

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

function paginate($ 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>';


                    };


            ?>