如何创建分页 [英] How to create pagination
本文介绍了如何创建分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以向我解释如何对WordPress数据库中的内容实施分页吗?我们大约有2000条记录,我不想在一个页面中显示它们.
Could anyone explain to me how to implement pagination for the content we have in a WordPress database? We have around 2000 records and I don't want to display them in a single page.
有人可以帮忙吗? 这是我所拥有的代码,可以显示数据库的内容
Could anybody help? This is the code i have through this i can display the contents of my database
<?php
require_once( dirname(__FILE__) . '/wp-load.php' );
global $wpdb;
$table = $wpdb->prefix."car_saver";
$data = $wpdb->get_results("SELECT * FROM " . $table );
if(count($data))
{
?>
<table width="700" cellspacing="0" border="0">
<thead>
<tr>
<th>id</th>
<th>Make</th>
<th>Model</th>
<th>Sub Model</th>
<th>MSRP</th>
<th>Utility</th>
<th>Feature</th>
<th>Year</th>
</tr>
</thead>
<tfoot>
<tr>
<th>id</th>
<th>Make</th>
<th>Model</th>
<th>Sub Model</th>
<th>MSRP</th>
<th>Utility</th>
<th>Feature</th>
<th>Year</th>
</tr>
</tfoot>
<tbody>
<?php
foreach($data as $p): ?>
<tr>
<td align="center"><?php echo $p->car_id; ?></td>
<td align="center"><?php echo $p->Make; ?></td>
<td align="center"><?php echo $p->Model; ?></td>
<td align="center"><?php echo $p->subModel; ?></td>
<td align="center"><?php echo $p->MSRP; ?></td>
<td align="center"><?php echo $p->Utility; ?></td>
<td align="center"><?php echo $p->Feature; ?></td>
<td align="center"><?php echo $p->Year; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
else echo "data not found";
?>
推荐答案
您不仅仅需要尝试此操作来获得大小::
You need more than try this for size::
$results_per_page = 10;
$current = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
global $wpdb;
$table = $wpdb->prefix."car_saver";
$rows = $wpdb->get_results("SELECT * FROM " . $table );
if( !empty($wp_query->query_vars['s']) ):
$args['add_args'] = array('s'=>get_query_var('s'));
endif;
$args = array(
'base' => @add_query_arg('paged','%#%'),
'format' => '',
'total' => ceil(sizeof($rows)/$results_per_page),
'current' => $current,
'show_all' => false,
'type' => 'plain',
);
echo paginate_links($args);
$start = ($current - 1) * $results_per_page;
$end = $start + $results_per_page;
$end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
echo '<br />';
for($i=$start;$i < $end ;++$i ):
$row = $rows[$i];
endfor;
这篇关于如何创建分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文