4 Replies - 349 Views - Last Post: 11 September 2012 - 03:57 AM Rate Topic: -----

#1 Synjunitzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-March 11

Pagination Function - Help Wanted.

Posted 03 September 2012 - 10:39 AM

Hey all,

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)

  • Attached File  db.txt (1.16K)
    Number of downloads: 26


Is This A Good Question/Topic? 0
  • +

Replies To: Pagination Function - Help Wanted.

#2 Synjunitzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-March 11

Re: Pagination Function - Help Wanted.

Posted 03 September 2012 - 10:47 AM

Um.. okay, that didn't post the way it was shown in the preview...

here's index.php: http://codepad.org/dKckpA7j
and well the db.txt is attached.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2503
  • View blog
  • Posts: 8,564
  • Joined: 08-August 08

Re: Pagination Function - Help Wanted.

Posted 03 September 2012 - 10:54 AM

Does it work? Are there errors?
Was This Post Helpful? 0
  • +
  • -

#4 Synjunitzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-March 11

Re: Pagination Function - Help Wanted.

Posted 03 September 2012 - 11:13 AM

View PostCTphpnwb, on 03 September 2012 - 10:54 AM, said:

Does it work? Are there errors?


No errors...
I just need to alter the function to take queries instead of just 'table' and 'column' ...

And I'm struggling.

Also for anyone else, I'm NOT looking for ANOTHER pagination script.

So please, if possible, help me modify this function to take whole queries instead of 'table' and 'column'

:wink:
Was This Post Helpful? 0
  • +
  • -

#5 Synjunitzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-March 11

Re: Pagination Function - Help Wanted.

Posted 11 September 2012 - 03:57 AM

Bump.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1