doubi1928 2014-11-28 07:19
浏览 131
已采纳

WordPress中WP_List_table的分页问题

I'm developing a plugin for WordPress. I use WP_List_Table which output rows from database nicely. The issue I have is the pagination. If I have let's say 200 rows in the database and I set it to only display 50 rows per page, this works and I do get 1 of 4 pages.

On page 1 It displays ID 1 to ID 50. But when I click on page 2 it displays ID 2 to ID 51. So it only jumps 1 row and not 50. Where's the issue?

This will handle the data:

    function prepare_items() {
        global $wpdb;

        /* Select project for user */
        $table_name2 = $wpdb->prefix . 'project_name';

        $current_user = wp_get_current_user();

        $sql2 = "SELECT * FROM " . $wpdb->prefix . "project_name WHERE alloweduser = " . $current_user->ID;

        $results2 = $wpdb->get_results($sql2) or die(mysql_error());

            foreach( $results2 as $result2 ) {

                $table_name = $wpdb->prefix . "project_name_" . $result2->projectname;

        /* Define how many rows to show per page */
        $per_page = 50;

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();

        // here we configure table headers, defined in our methods
        $this->_column_headers = array($columns, $hidden, $sortable);

        // [OPTIONAL] process bulk action if any
        $this->process_bulk_action();

        // will be used in pagination settings
        $total_items = $wpdb->get_var("SELECT COUNT(cid) FROM $table_name");


        // prepare query params, as usual current page, order by and order direction
        $paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 0) : 0;
        $orderby = (isset($_REQUEST['orderby']) && in_array($_REQUEST['orderby'], array_keys($this->get_sortable_columns()))) ? $_REQUEST['orderby'] : 'cid';
        $order = (isset($_REQUEST['order']) && in_array($_REQUEST['order'], array('ASC', 'DESC'))) ? $_REQUEST['order'] : 'ASC';

        // Define $items array
        // notice that last argument is ARRAY_A, so we will retrieve array
        $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name ORDER BY $orderby $order LIMIT %d OFFSET %d", $per_page, $paged), ARRAY_A);

        // configure pagination
        $this->set_pagination_args(array(
            'total_items' => $total_items, // total items defined above
            'per_page' => $per_page, // per page constant defined at top of method
            'total_pages' => ceil($total_items / $per_page) // calculate pages count
        ));

        }

    }
}

This will output the data:

function custom_table_example_submissions_page_handler() {
    global $wpdb;

    $table = new Custom_Table_Example_List_Table();
    $table->prepare_items();

    $message = '';
    if ('delete' === $table->current_action()) {
        $message = '<div class="updated below-h2" id="message"><p>' . sprintf(__('Deleted %d submission(s).', 'custom_table_example'), count($_REQUEST['id'])) . '</p></div>';
    }
    ?>
    <div class="wrap">

        <div class="icon32 icon32-posts-post" id="icon-edit">
            <br></div>
            <h2><?php _e('Submissions', 'custom_table_example')?> 
            </h2>
            <?php echo $message; ?>

            <form id="submissions-table" method="GET">
                <input type="hidden" name="page" value="<?php echo $_REQUEST['page']; ?>"/>
                <?php //$table->display(array('contributorname', 'email')); ?>
                <?php $table->display(); ?>
            </form>

    </div>
<?php
}

Update:

get_columns:

function get_columns() {
    $columns = array(
        'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
        'name' => __('Name', 'custom_table_example'),
        'upload' => __('Upload', 'custom_table_example'),
        'upload2' => __('Upload', 'custom_table_example'),
        'upload3' => __('Upload', 'custom_table_example'),
        'upload4' => __('Upload', 'custom_table_example'),
        'rate' => __('Rate', 'custom_table_example'),
    );
    return $columns;
}

$this->items is retrieved from: https://core.trac.wordpress.org/browser/tags/4.0.1/src//wp-admin/includes/class-wp-list-table.php#L0

It's called with:

if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
  • 写回答

2条回答 默认 最新

  • dongzan2740 2014-11-28 12:34
    关注

    I managed to solve it and answer my own question.

    The issue was here:

    $paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 0) : 0;
    

    Since I want to output 50 rows per page I simply added -1 to $_REQUEST['paged'] and added * 50 which means that on page 1 row 1-50 is displayed and on page 2 rows 51-100 is displayed since 2*50 is 100 and so on.

    Correct solutions is this:

    $paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged'] -1) * 50) : 0;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势