Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*******************************************************************************************
3
 Credits: dezignwork.com
4
 Desc:    This class will will create a pagination div with all the links, page numbers/link
5
 and highlighted current page. All the pagination content will be placed in a div.
6
 The style of the div can be changed.
7
 ********************************************************************************************/
8
class Paging {
9
 
10
	var $result;
11
	var $link_class;
12
	var $div_class;
13
	var $current_page;
14
	var $PAGE='Page';
15
 
16
	function loadPaging($result, $per_page=15){
17
		if(is_numeric($result)) {
18
		} else {
19
			$numRows = function_exists('mysqli_query') ? mysqli_num_rows($result) : mysql_num_rows($result);
20
			if(!$result or $numRows<1) { return 0; }
21
		}
22
		$this->result = $result;
23
		$this->per_page = $per_page;
24
	}
25
 
26
	function printPaging($scriptArgs='') {
27
 
28
		$tmp = '';
29
		if(empty($this->current_page)) {
30
			$this->setCurrentPage(1);
31
		}
32
		if(!is_numeric($this->result)) {
33
			$num_rows = function_exists('mysqli_query') ? mysqli_num_rows($this->result) : mysql_num_rows($this->result);
34
		} else {
35
			$num_rows = $this->result;
36
		}
37
 
38
		if($this->per_page==0) {
39
			return 0;
40
		}
41
		$pages_required = ceil($num_rows/$this->per_page);
42
		if(!empty($this->div_class)) { $div_class=' class="'.$this->div_class.'"'; }
43
		$tmp .= "<div$div_class>"; ": "; ": ";
44
		$page_line="";
45
		$start_page=$this->current_page-4;
46
		if($start_page<1) { $start_page=1; }
47
		$end_page=$this->current_page+4;
48
		if($end_page>$pages_required) { $end_page=$pages_required; }
49
		if(($end_page-$start_page)<8) {
50
			$diff=abs($end_page-9);
51
			for($i=0; $i<$diff; $i++) {
52
				if($end_page<$pages_required) {
53
					$end_page++;
54
				}
55
			}
56
		}
57
 
58
		# arrow left
59
		if($start_page>1) {
60
			if(!empty($this->action_path)) {
61
				$this->scriptArgs = $scriptArgs . "&pageno=1";
62
				$page_line .= $this->createPagingLink('&laquo;', '', '&nbsp;&nbsp;');
63
			}
64
		}
65
 
66
		# pages
67
		for($i=$start_page; $i<=$end_page; $i++) {
68
			if($i>$end_page) { break; }
69
			if($this->current_page!=$i) {
70
				if(!empty($this->action_path)) {
71
					$this->scriptArgs = $scriptArgs . "&pageno=$i";
72
					$page_line .= $this->createPagingLink($i, '', '&nbsp; | &nbsp;');
73
				}
74
			} else {
75
				$page_line.='<b>'.$i.'</b>&nbsp; | &nbsp;';
76
			}
77
 
78
		}
79
		$page_line=substr($page_line,0,strlen($page_line)-15);
80
 
81
		# arrow right
82
		if($end_page<$pages_required) {
83
			if(!empty($this->action_path)) {
84
				$this->scriptArgs = $scriptArgs . "&pageno=$pages_required";
85
				$page_line .= $this->createPagingLink('&raquo;', '&nbsp;&nbsp;', '');
86
			}
87
		}
88
		$tmp .= $page_line;
89
		$tmp .= '</div><div style="clear:both;"></div>';
90
		return $tmp;
91
	}
92
 
93
	# func to create paging link
94
	function createPagingLink($linkText='', $linkBefore='', $linkAfter=''){
95
 
96
		if(!empty($this->link_class)) { $linkClass=' class="'.$this->link_class.'"'; }
97
 
98
		if ($this->scriptFunction == 'link') {
99
			$link = "$linkBefore <a href='$this->scriptPath$this->scriptArgs' $linkClass>$linkText</a>$linkAfter";
100
		} elseif($this->scriptFunction == 'scriptDoLoadPost') {
101
			$link = "$linkBefore<a href='javascript:void(0);' $linkClass onclick=\"scriptDoLoadPost('$this->scriptPath', '$this->scriptForm', '$this->showArea', '$this->scriptArgs')\">$linkText</a>$linkAfter";
102
		}else{
103
			$link = "$linkBefore<a href='javascript:void(0);' $linkClass onclick=\"$this->scriptFunction('$this->scriptPath', '$this->showArea', '$this->scriptArgs')\">$linkText</a>$linkAfter";
104
		}
105
		return $link;
106
	}
107
 
108
 
109
	# mutators
110
	function setActionPath($path) {
111
		$this->action_path = $path;
112
	}
113
 
114
	function setLinkClass($class) {
115
		$this->link_class = $class;
116
	}
117
	function setDivClass($class) {
118
		$this->div_class = $class;
119
	}
120
 
121
	function setPageLimit($limit) {
122
		$this->per_page = $limit;
123
	}
124
	function setCurrentPage($page) {
125
		$this->current_page = $page;
126
	}
127
 
128
	# func to print pages
129
	function printPages($scriptPath, $scriptForm='', $scriptfunction='scriptDoLoad', $showArea='content', $scriptArgs=''){
130
 
131
		$this->scriptPath = $scriptPath;
132
		$this->scriptFunction = $scriptfunction;
133
		$this->showArea = $showArea;
134
		$this->scriptForm = $scriptForm;
135
		$this->scriptArgs = $scriptArgs;
136
 
137
		$this->setActionPath(1);
138
 
139
		# determine current page number
140
		$pageNo = 0;
141
		if(isset($_POST["pageno"])){
142
			$pageNo = $_POST["pageno"];
143
		}elseif(isset($_GET["pageno"])){
144
			$pageNo = $_GET["pageno"];
145
		}
146
		if(!empty($pageNo)) {
147
			$this->start = ($pageNo - 1) * $this->per_page;
148
			$this->setCurrentPage($pageNo);
149
		} else {
150
			$this->setCurrentPage(1);
151
			$this->start = 0;
152
		}
153
		return $this->printPaging($scriptArgs);
154
 
155
	}
156
 
157
}
158
?>