我正在为WordPress开发一个插件。我使用WP_List_Table很好地输出数据库中的行。 我遇到的问题是分页。如果我在数据库中说200行,并且我将其设置为每页只显示50行,则可以使用,并且我会得到4页中的1页。
第1页它显示ID 1到ID 50.但是当我点击第2页时,它显示ID 2到ID 51.所以它只跳1行而不是50.问题出在哪里?
这将处理数据:
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
));
}
}
}
这将输出数据:
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
}
更新
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-&gt;项目: https://core.trac.wordpress.org/browser/tags/4.0.1/src//wp-admin/includes/class-wp-list-table.php#L0
它被称为:
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
答案 0 :(得分:1)
我设法解决了这个问题并回答了我自己的问题。
问题在于:
$paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged']) - 0) : 0;
由于我想每页输出50行,我只需将-1添加到$ _REQUEST ['paged']并添加* 50,这意味着在第1页显示第1-50行,在第2页第51-100行是因为2 * 50显示为100,依此类推。
正确的解决方案是:
$paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged'] -1) * 50) : 0;
答案 1 :(得分:0)
我的项目中存在同样的问题,因此解决方案就在这里
$per_page = 5;
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$paged = isset($_REQUEST['paged']) ? max(0, intval($_REQUEST['paged'] -1) * $per_page) : 0;
我希望现在你的问题能解决
答案 2 :(得分:0)
$found_data = array_slice($this->table_data(),(($current_page-1)*$per_page),$per_page);
$this->items = $found_data;
在您的prepare_items()
中使用上述代码。它会将您的数据列表划分为大块。