【问题标题】:Mysql Php Pagination for varios table用于 varios 表的 Mysql Php 分页
【发布时间】:2014-11-14 00:47:13
【问题描述】:

我有这个分页脚本

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

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

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

    函数分页($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>';
    
    
                        };
    
    
                ?>
    

    【讨论】:

    • 好的,谢谢,但是对于 和 区域呢?这些表格的大小不同,例如 1º -- ID - 标题 - 照片 -- 2º --ID -- 标题 -- 描述 -- 照片 --。我需要根据表名和表值获取 td 和 th。
    • 任何解决方案或其他方式?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签