Php 我想分页到我的wordpress插件的选项页?
我想根据wordpress数据库的结果进行分页…这都是在我的wordpress插件选项页面上完成的 我要从数据库中检索的代码如下Php 我想分页到我的wordpress插件的选项页?,php,javascript,ajax,wordpress,Php,Javascript,Ajax,Wordpress,我想根据wordpress数据库的结果进行分页…这都是在我的wordpress插件选项页面上完成的 我要从数据库中检索的代码如下 $per_page=5; $sql = "SELECT * FROM wp_dive "; $result = $wpdb->get_results($sql_10) or die(mysql_error()); $length=count($result); $pages = ceil($length/$per_page); foreach( $result
$per_page=5;
$sql = "SELECT * FROM wp_dive ";
$result = $wpdb->get_results($sql_10) or die(mysql_error());
$length=count($result);
$pages = ceil($length/$per_page);
foreach( $result as $results )
{
$id=$results->id;
$name= $results->name_cust;
$gender= $results->gender_cust;
$dob= $results->dob_cust;
<?php $html= "<div class=\"divContentBody\">";?>
<?php $html .= "<span class=\"clsOrderNo\">". $id."</span>";?>
<?php $html .= "<span class=\"clsName\">". $name."</span>";?>
<?php $html .= "<span class=\"clsGender\">".$gender."</span>";?>
<?php $html .= "<span class=\"clsDOB\">". $dob ."</span>";?>
<?php $html .= "</div>"?>
<?php
$data_html .=$html;
}
?>
$per_page=5;
$sql=“从wp\U潜水中选择*”;
$result=$wpdb->get_results($sql_10)或die(mysql_error());
$length=计数($result);
$pages=ceil($length/$per_page);
foreach($result作为$results)
{
$id=$results->id;
$name=$results->name\u cust;
$SEXT=$results->SEXT\u cust;
$dob=$results->dob_cust;
我正在动态获取数据..我只想添加分页..在第一页上显示5个条目,并相应地..将其用于您的需要,如:
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 5; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM `wp_dive`" );
$num_of_pages = ceil( $total / $limit );
$result = $wpdb->get_results( "SELECT `id`,`name_cust`,`gender_cust`,`dob_cust` FROM `wp_dive` LIMIT $offset, $limit" );
$data_html = '';
foreach( $result as $results )
{
$id=$results->id;
$name= $results->name_cust;
$gender= $results->gender_cust;
$dob= $results->dob_cust;
?>
<?php $html= "<div class=\"divContentBody\">";?>
<?php $html .= "<span class=\"clsOrderNo\">". $id."</span>";?>
<?php $html .= "<span class=\"clsName\">". $name."</span>";?>
<?php $html .= "<span class=\"clsGender\">".$gender."</span>";?>
<?php $html .= "<span class=\"clsDOB\">". $dob ."</span>";?>
<?php $html .= "</div>"?>
<?php
$data_html .=$html;
}
echo $data_html;
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'aag' ),
'next_text' => __( '»', 'aag' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' .
$page_links . '</div></div>';
}
$pagenum=isset($\u GET['pagenum'])?absint($\u GET['pagenum']):1;
$limit=5;//页面中的行数
$offset=($pagenum-1)*$limit;
$total=$wpdb->get_var(“从'wp_-dive'中选择COUNT(`id`));
$num_of_pages=ceil($total/$limit);
$result=$wpdb->get_results(“从`wp_-dive`限制$offset,$LIMIT中选择`id`、`name_-cust`、`gender_-cust`、`dob_-cust`”);
$data_html='';
foreach($result作为$results)
{
$id=$results->id;
$name=$results->name\u cust;
$SEXT=$results->SEXT\u cust;
$dob=$results->dob_cust;
?>
实现这一点的原生方法是扩展类WP\u List\u Table
,让WordPress处理表显示的所有细节。我是通过插件知道的。下面的示例是执行非常简单SQL查询的精简版本
首先,我们需要一个助手页面(使用PHP5.3+匿名函数):
添加操作('管理菜单',函数())
{
添加菜单页面(
"TE",,
“表格示例”,
“编辑页面”,
“表格示例”,
函数(){
回声';
屏幕图标(“编辑”);
回声“表格示例”;
新的B5F_WP_表();
回声';
},
'http://sstatic.net/stackexchange/img/favicon.ico',
1//在仪表板菜单项之前创建
);
});
这就是最终结果:
这里是执行所有操作的类(注意需要导入主类)。您必须调整数据的查询和表列
if( is_admin() && !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
class B5F_WP_Table extends WP_List_Table
{
private $order;
private $orderby;
private $posts_per_page = 5;
public function __construct()
{
parent :: __construct( array(
'singular' => 'table example',
'plural' => 'table examples',
'ajax' => true
) );
$this->set_order();
$this->set_orderby();
$this->prepare_items();
$this->display();
}
private function get_sql_results()
{
global $wpdb;
$args = array( 'ID', 'post_title', 'post_date', 'post_content', 'post_type' );
$sql_select = implode( ', ', $args );
$sql_results = $wpdb->get_results("
SELECT $sql_select
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY $this->orderby $this->order "
);
return $sql_results;
}
public function set_order()
{
$order = 'DESC';
if ( isset( $_GET['order'] ) AND $_GET['order'] )
$order = $_GET['order'];
$this->order = esc_sql( $order );
}
public function set_orderby()
{
$orderby = 'post_date';
if ( isset( $_GET['orderby'] ) AND $_GET['orderby'] )
$orderby = $_GET['orderby'];
$this->orderby = esc_sql( $orderby );
}
/**
* @see WP_List_Table::ajax_user_can()
*/
public function ajax_user_can()
{
return current_user_can( 'edit_posts' );
}
/**
* @see WP_List_Table::no_items()
*/
public function no_items()
{
_e( 'No posts found.' );
}
/**
* @see WP_List_Table::get_views()
*/
public function get_views()
{
return array();
}
/**
* @see WP_List_Table::get_columns()
*/
public function get_columns()
{
$columns = array(
'ID' => __( 'ID' ),
'post_title' => __( 'Title' ),
'post_date' => __( 'Date' ),
'post_type' => __( 'Type' )
);
return $columns;
}
/**
* @see WP_List_Table::get_sortable_columns()
*/
public function get_sortable_columns()
{
$sortable = array(
'ID' => array( 'ID', true ),
'post_title' => array( 'post_title', true ),
'post_date' => array( 'post_date', true )
);
return $sortable;
}
/**
* Prepare data for display
* @see WP_List_Table::prepare_items()
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array(
$columns,
$hidden,
$sortable
);
// SQL results
$posts = $this->get_sql_results();
empty( $posts ) AND $posts = array();
# >>>> Pagination
$per_page = $this->posts_per_page;
$current_page = $this->get_pagenum();
$total_items = count( $posts );
$this->set_pagination_args( array (
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page )
) );
$last_post = $current_page * $per_page;
$first_post = $last_post - $per_page + 1;
$last_post > $total_items AND $last_post = $total_items;
// Setup the range of keys/indizes that contain
// the posts on the currently displayed page(d).
// Flip keys with values as the range outputs the range in the values.
$range = array_flip( range( $first_post - 1, $last_post - 1, 1 ) );
// Filter out the posts we're not displaying on the current page.
$posts_array = array_intersect_key( $posts, $range );
# <<<< Pagination
// Prepare the data
$permalink = __( 'Edit:' );
foreach ( $posts_array as $key => $post )
{
$link = get_edit_post_link( $post->ID );
$no_title = __( 'No title set' );
$title = ! $post->post_title ? "<em>{$no_title}</em>" : $post->post_title;
$posts[ $key ]->post_title = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";
}
$this->items = $posts_array;
}
/**
* A single column
*/
public function column_default( $item, $column_name )
{
return $item->$column_name;
}
/**
* Override of table nav to avoid breaking with bulk actions & according nonce field
*/
public function display_tablenav( $which ) {
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
<!--
<div class="alignleft actions">
<?php # $this->bulk_actions( $which ); ?>
</div>
-->
<?php
$this->extra_tablenav( $which );
$this->pagination( $which );
?>
<br class="clear" />
</div>
<?php
}
/**
* Disables the views for 'side' context as there's not enough free space in the UI
* Only displays them on screen/browser refresh. Else we'd have to do this via an AJAX DB update.
*
* @see WP_List_Table::extra_tablenav()
*/
public function extra_tablenav( $which )
{
global $wp_meta_boxes;
$views = $this->get_views();
if ( empty( $views ) )
return;
$this->views();
}
}
if(存在类('WP\u List\u Table'))
require_once(ABSPATH.'wp admin/includes/class wp list table.php');
类B5F_WP_Table扩展了WP_List_Table
{
私人$order;
私人$orderby;
私人$posts\u每页=5;
公共函数构造()
{
父项::_构造(数组)(
'单数'=>'表格示例',
“复数”=>“表格示例”,
“ajax”=>正确
) );
$this->set_order();
$this->set_orderby();
$this->prepare_items();
$this->display();
}
私有函数get_sql_results()
{
全球$wpdb;
$args=数组('ID','post_title','post_date','post_content','post_type');
$sql_select=内爆(“,”,$args);
$sql\u results=$wpdb->get\u results(“
选择$sql\u选择
从$wpdb->posts
其中post_状态=‘发布’
订单依据$this->orderby$this->ORDER”
);
返回$sql\u结果;
}
公共函数集_顺序()
{
$order='DESC';
如果(isset($_-GET['order'])和$_-GET['order']))
$order=$_获取['order'];
$this->order=esc\u sql($order);
}
公共函数集_orderby()
{
$orderby='post_date';
if(isset($_GET['orderby'])和$_GET['orderby']))
$orderby=$_GET['orderby'];
$this->orderby=esc\u sql($orderby);
}
/**
*@see WP_List_Table::ajax_user_can()
*/
公共函数ajax\u user\u can()
{
返回当前_用户_can('edit_posts');
}
/**
*@见WP_列表_表::无_项()
*/
公共功能无项目()
{
_e(‘未找到帖子’);
}
/**
*@请参阅WP_列表_表::获取视图()
*/
公共函数获取视图()
{
返回数组();
}
/**
*@请参阅WP_List_Table::get_columns()
*/
公共函数get_columns()
{
$columns=数组(
“ID'=>”(“ID”),
“post_title”=>(“title”),
“发布日期”=>(“日期”),
“post_type”=>(类型)
);
返回$columns;
}
/**
*@see WP_List_Table::get_sortable_columns()
*/
公共函数get_sortable_columns()
{
$sortable=数组(
'ID'=>数组('ID',true),
'post_title'=>数组('post_title',true),
“发布日期”=>数组(“发布日期”,true)
);
返回$sortable;
}
/**
*准备要显示的数据
*@见WP_列表_表::准备_项()
*/
公共职能准备_项目()
{
$columns=$this->get_columns();
$hidden=array();
$sortable=$this->get_sortable_columns();
$this->_column_headers=数组(
$columns,
$hidden,
$sortable
);
//SQL结果
$posts=$this->get_sql_results();
空($posts)和$posts=array();
#>>>>分页
$per_page=$this->posts_per_page;
$current_page=$this->get_pagenum();
$total_items=计数($posts);
$this->set_pagination_args(数组(
“总项目数”=>$total\u项目数,
“每页”=>每页$,
“总页数”=>ceil($total\u items/$per\u page)
) );
$last\u post=$current\u page*$per\u page;
$first_post=$last_post-$per_page+1;
$last\u post>$total\u items和$last\u post=$total\u items;
//设置包含
//邮报
if( is_admin() && !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
class B5F_WP_Table extends WP_List_Table
{
private $order;
private $orderby;
private $posts_per_page = 5;
public function __construct()
{
parent :: __construct( array(
'singular' => 'table example',
'plural' => 'table examples',
'ajax' => true
) );
$this->set_order();
$this->set_orderby();
$this->prepare_items();
$this->display();
}
private function get_sql_results()
{
global $wpdb;
$args = array( 'ID', 'post_title', 'post_date', 'post_content', 'post_type' );
$sql_select = implode( ', ', $args );
$sql_results = $wpdb->get_results("
SELECT $sql_select
FROM $wpdb->posts
WHERE post_status = 'publish'
ORDER BY $this->orderby $this->order "
);
return $sql_results;
}
public function set_order()
{
$order = 'DESC';
if ( isset( $_GET['order'] ) AND $_GET['order'] )
$order = $_GET['order'];
$this->order = esc_sql( $order );
}
public function set_orderby()
{
$orderby = 'post_date';
if ( isset( $_GET['orderby'] ) AND $_GET['orderby'] )
$orderby = $_GET['orderby'];
$this->orderby = esc_sql( $orderby );
}
/**
* @see WP_List_Table::ajax_user_can()
*/
public function ajax_user_can()
{
return current_user_can( 'edit_posts' );
}
/**
* @see WP_List_Table::no_items()
*/
public function no_items()
{
_e( 'No posts found.' );
}
/**
* @see WP_List_Table::get_views()
*/
public function get_views()
{
return array();
}
/**
* @see WP_List_Table::get_columns()
*/
public function get_columns()
{
$columns = array(
'ID' => __( 'ID' ),
'post_title' => __( 'Title' ),
'post_date' => __( 'Date' ),
'post_type' => __( 'Type' )
);
return $columns;
}
/**
* @see WP_List_Table::get_sortable_columns()
*/
public function get_sortable_columns()
{
$sortable = array(
'ID' => array( 'ID', true ),
'post_title' => array( 'post_title', true ),
'post_date' => array( 'post_date', true )
);
return $sortable;
}
/**
* Prepare data for display
* @see WP_List_Table::prepare_items()
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array(
$columns,
$hidden,
$sortable
);
// SQL results
$posts = $this->get_sql_results();
empty( $posts ) AND $posts = array();
# >>>> Pagination
$per_page = $this->posts_per_page;
$current_page = $this->get_pagenum();
$total_items = count( $posts );
$this->set_pagination_args( array (
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page )
) );
$last_post = $current_page * $per_page;
$first_post = $last_post - $per_page + 1;
$last_post > $total_items AND $last_post = $total_items;
// Setup the range of keys/indizes that contain
// the posts on the currently displayed page(d).
// Flip keys with values as the range outputs the range in the values.
$range = array_flip( range( $first_post - 1, $last_post - 1, 1 ) );
// Filter out the posts we're not displaying on the current page.
$posts_array = array_intersect_key( $posts, $range );
# <<<< Pagination
// Prepare the data
$permalink = __( 'Edit:' );
foreach ( $posts_array as $key => $post )
{
$link = get_edit_post_link( $post->ID );
$no_title = __( 'No title set' );
$title = ! $post->post_title ? "<em>{$no_title}</em>" : $post->post_title;
$posts[ $key ]->post_title = "<a title='{$permalink} {$title}' href='{$link}'>{$title}</a>";
}
$this->items = $posts_array;
}
/**
* A single column
*/
public function column_default( $item, $column_name )
{
return $item->$column_name;
}
/**
* Override of table nav to avoid breaking with bulk actions & according nonce field
*/
public function display_tablenav( $which ) {
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
<!--
<div class="alignleft actions">
<?php # $this->bulk_actions( $which ); ?>
</div>
-->
<?php
$this->extra_tablenav( $which );
$this->pagination( $which );
?>
<br class="clear" />
</div>
<?php
}
/**
* Disables the views for 'side' context as there's not enough free space in the UI
* Only displays them on screen/browser refresh. Else we'd have to do this via an AJAX DB update.
*
* @see WP_List_Table::extra_tablenav()
*/
public function extra_tablenav( $which )
{
global $wp_meta_boxes;
$views = $this->get_views();
if ( empty( $views ) )
return;
$this->views();
}
}
<?php
global $wpdb;
$paged = isset( $_GET['paged'] ) ? (int) $_GET['paged'] : 1;
$limit = 10; // Row in a single
$offset = ( $paged - 1 ) * $limit;
$total = $wpdb->get_var( 'SELECT COUNT(`id`) FROM `wp_dive`' );
$num_of_pages = ceil( $total / $limit );
$result = $wpdb->get_results( "SELECT `id`,`name_cust`,`gender_cust`,`dob_cust` FROM `wp_dive` LIMIT $offset, $limit" );
$data_html = '';
foreach( $result as $results )
{
$id=$results->id;
$name= $results->name_cust;
$gender= $results->gender_cust;
$dob= $results->dob_cust;
?>
<?php $html= "<div class=\"divContentBody\">";?>
<?php $html .= "<span class=\"clsOrderNo\">". $id."</span>";?>
<?php $html .= "<span class=\"clsName\">". $name."</span>";?>
<?php $html .= "<span class=\"clsGender\">".$gender."</span>";?>
<?php $html .= "<span class=\"clsDOB\">". $dob ."</span>";?>
<?php $html .= "</div>"?>
<?php
$data_html .=$html;
}
echo $data_html;
$page_links = paginate_links( array(
'base' => add_query_arg( 'paged', '%#%' ),
'format' => '',
'prev_text' => '«',
'next_text' => '»',
'total' => $total,
'current' => $paged
) );
if ( $page_links ) {
echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' .
$page_links . '</div></div>';
}
?>