Skip to content

Instantly share code, notes, and snippets.

@keks55
Created January 7, 2018 11:29
Show Gist options
  • Save keks55/245d6687ca74de3b7f7cdde3994c3ee3 to your computer and use it in GitHub Desktop.
Save keks55/245d6687ca74de3b7f7cdde3994c3ee3 to your computer and use it in GitHub Desktop.
<?php
$start = isset($_GET['start']) ? intval( $_GET['start'] ) : 0 ;
$critery = isset($_GET['critery']) ? intval( $_GET['critery'] ) : 0 ;
$p = isset($_GET['p']) ? intval( $_GET['p'] ) : 1 ;
$list_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name LIMIT $start, $items_limit"), ARRAY_A);
foreach ($list_data as $v) { ?>
<tr>
<td class="t1"><?php echo $v['name'] ?></td>
<td class="t2"><?php echo $v['type'] ?></td>
<td class="t3"><?php echo $v['address'] ?></td>
<td class="t4">Edit</td>
<td class="t5">Delete</td>
</tr><?php
} ?>
</tr>
</tbody>
</table>
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>"/>
</form>
<?php
// page navigation
$all_items_pgm = $wpdb->get_results($wpdb->prepare("SELECT COUNT(*) AS count FROM $table_name LIMIT $start, $items_limit"), ARRAY_A);
$all_items_pgm = $all_items_pgm[0]["count"];
$total = ceil($all_items_pgm/$items_limit);
$start_last = floor(($all_items_pgm/$items_limit))*$items_limit;
echo "p=".$p."<br>";
echo "start=".$start."<br>";
echo "posts=".$all_items_pgm."<br>";
echo "total=".$total."<br>";
$firt_page = '<li class="first"><a href='.admin_url('admin.php?page=points&p=1&start=0').' >&laquo;</a></li>';
$last_page = '<li class="next"><a href='.admin_url('admin.php?page=points&p=').$total.'&start='.$start_last.'>&raquo;</a></li>';
$prev_page = '<li class="back"><a href='.admin_url('admin.php?page=points&p=').($p - 1).'&start='.(($p - 1)*$items_limit).'>Back</a></li>';
$next_page = '<li class="first"><a href='.admin_url('admin.php?page=points&p=').($p + 1).'&start='.($p*$items_limit).' >Next</a></li>';
if($p - 2 > 0)
$p2left = '<li><a href='.admin_url('admin.php?page=points&p=').($p - 2).'&start='.(($p - 1)*$items_limit).'>'. ($p - 2) .'</a>';
if($p - 1 > 0)
$p1left = '<li><a href='.admin_url('admin.php?page=points&p=').($p - 1).'&start='.($p*$items_limit).'>'. ($p - 1) .'</a>';
if($p + 2 <= $total)
$p2right = '<li><a href='.admin_url('admin.php?page=points&p=').($p + 2).'&start='.(($p + 1)*$items_limit).'>'. ($p + 2) .'</a>';
if($p + 1 <= $total)
$p1right = '<li><a href='.admin_url('admin.php?page=points&p=').($p + 1).'&start='.($p*$items_limit).'>'. ($p + 1) .'</a>';
//echo "<ul class='pagenav'>".$firt_page.$p1left.'<li><span >'.$p.'</span></li>'.$p1right.$p2right.$next_page."</ul>";
/*if ($total > 1){
if($p == 1){
echo "<ul class='pagenav'>".$firt_page.'<li><span >'.$p.'</span></li>'.$p1right.$p2right.$next_page.$last_page."</ul>";
}
elseif($p != $total) {
echo "<ul class='pagenav'>".$firt_page.$prev_page.'<li><span>'.$p.'</span></li>'.$p1right.$p2right.$next_page.$last_page."</ul>";
}
else {
echo "<ul class='pagenav'>".$firt_page.$prev_page.'<li><span>'.$p.'</span></li>'.$p1right."</ul>";
}
}
*/
for( $i = 0; $i < $total; $i++ ) {
// Здесь ($i * $limit) - вычисляет нужное для каждой страницы смещение,
// а ($i + 1) - для того что бы нумерация страниц начиналась с 1, а не с 0
$html .= '<li><a href='.admin_url('admin.php?page=points&p=').($i + 1).'&start='.($i*$items_limit).'>'. ($i + 1) .'</a></li>';
}
echo '<ul class="pagination">' . $html . '</ul>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment