I'm pretty new here in terms of activity and I'd like some help in making my pagination function better.
More specifically, I only want help regarding a certain aspect of it.
I need my pagination function to be able to take queries and return a paginated list using 'almost' only the query and of course the other usual parameters such as $per_page.
Please find attached the basic db creation... (db.txt)
Essentially it functions as such...
<?php
########################################################################
# Title: Pagination Script
# Author: synju
# Email: nuerotronic@gmail.com
########################################################################
# Establish Database Connection
mysql_connect('localhost','root','rootpass');
mysql_select_db('pagination');
# Create link_pagination()
function link_pagination($table_selection, $column_selection, $max_left, $per_page, $max_right) {
# Amount of $pages
$pages_query = " SELECT
COUNT(`uid`)
FROM
`{$table_selection}`
WHERE
`active` = 1
ORDER BY `uid` ASC
";
$pages_result = mysql_query($pages_query);
$pages = ceil((mysql_result($pages_result, 0)) / ($per_page));
# Generate $current_page
$current_page = 1;
if(isset($_GET['page'])) {
$i = (int)$_GET['page'];
if(is_int($i)) {
if(($i <= $pages) && ($i > 0)) {
$current_page = $i;
}
}
}
# Generate $start_page
$start_page = ($current_page - 1) * $per_page;
# Generate $list_content
$list_query = " SELECT
`{$column_selection}`
FROM
`{$table_selection}`
WHERE
`active` = 1
ORDER BY `uid` ASC
LIMIT
{$start_page},{$per_page}
";
$list_result = mysql_query($list_query);
$list_content = '';
while($query_row = mysql_fetch_assoc($list_result)) {
$list_content .= "
<div>{$query_row['name']}</div>
";
}
# Generate $page_links
$left_pages = max(1, $current_page - $max_left);
$right_pages = min($pages, $current_page + $max_right);
$page_links = '';
for($i = $left_pages; $i <= $right_pages; $i++) {
if($pages >= 1) {
if($i == $current_page) {
$page_links .= " [ <b>{$i}</b> ] ";
}
else {
$page_links .= " <a href='?page={$i}'>{$i}</a> ";
}
}
}
# Generate $previous_page and $next_page Links
$previous_page = "";
if($current_page > 1) {
$p = $current_page - 1;
$previous_page = "<a href='?page={$p}'>Previous</a>";
}
$next_page = "";
if($current_page < $pages) {
$n = $current_page + 1;
$next_page = "<a href='?page={$n}'>Next</a>";
}
# Generate $first_page and $last_page Links
$first_page = "";
if($current_page > (1 + $max_left)) {
$first_page = "<a href='?page=1'>First</a>";
}
$last_page = "";
if($current_page < ($pages - $max_right)) {
$last_page = "<a href='?page={$pages}'>Last</a>";
}
# Generate $link_collection
$link_collection = "{$previous_page} {$first_page} {$page_links} {$last_page} {$next_page}";
# Generate $link_pagination_array
$link_pagination_array = array($list_content, $link_collection);
# Return $link_pagination_array
return $link_pagination_array;
}
# Generate Paginated Listing With:
# link_pagination($table_selection, $column_selection, $max_left, $per_page, $max_right)
list($list,$links) = link_pagination('names','name',1,3,1);
# Generate HTML
$html = "
<html>
<head>
<title>Pagination Script</title>
</head>
<body>
{$list}
{$links}
</body>
</html>
";
# Display HTML
print $html;
?>
Attached File(s)
-
db.txt (1.16K)
Number of downloads: 26

New Topic/Question
Reply




MultiQuote





|