php查询数据库并分页,输出分页的html代码
$db->query("SELECT COUNT(*) FROM snippets s WHERE s.user_id = '$user_id'"); $numRecords = mysql_result($db->result, 0, 0); $numPages = ceil($numRecords / $perpage); $page = intval($_GET['page']); if($page > $numPages) $page = $numPages; if($page < 1) $page = 1; $start = ($page - 1) * $perpage; $nextPage = ($page + 1 > $numPages) ? $numPages : $page + 1; $prevPage = ($page - 1 < 1) ? 1 : $page - 1; if($page == 1 && $numPages == 1) $paging = "[Page 1 of $numPages]"; elseif($page == 1 && $numPages > 1) $paging = "[Page 1 of $numPages] <a href='/users/$username/page/$nextPage'>Next >></a>"; elseif($page == $numPages) $paging = "<a href='/users/$username/page/$prevPage'><< Prev</a> [Page $page of $numPages]"; else $paging = "<a href='/users/$username/page/$prevPage'><< Prev</a> [Page $page of $numPages] <a href='/users/$username/page/$nextPage'>Next >></a>";