I want a pagination according to the results coming from wordpress database...This is all done on my options page of wordpress plugin..
My code to retrieve from database is as follows
$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;
}
?>
I m getting the data dynamically ..I just want to add pagination ..to show 5 entries on first page and accordingly ...
Take this https://stackoverflow.com/a/16358219/1596547 and adopt it to your needs like:
You are querying with
posts_per_page
, but in order to use pagination you also need to append the'paged'
parameter to your query.See the WP Codex here:
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
The native way of doing this is extending the class
WP_List_Table
and let WordPress handle all the specifics of the table display. I know it from the plugin Internal Link Check, by kaiser. The example bellow is a stripped version that performs a very simple SQL query.First, we need a helper page (using PHP5.3+ anonymous functions):
This is the end result:
And here the class that performs everything (note the need of importing the main class). You'll have to adjust the query and the table columns for your data.
Helper plugin to style the admin: WordPress Admin Style, by bueltge