Rows op pagina
Op mijn website www.deltaboatcenter.nl worden boten verkocht maar nu wil ik dus bij de boten niet 5 boten onder elkaar maar alle boten op een pagina.
Ik heb hier een script (pagination) dat er voor moet zorgen dat ik dit kan veranderen maar ik heb in dit script aangegeven dat ik 30 boten op de pagina wil maar dat doet hij dus niet.
iemand een idee wat er fout aan is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
*/
// ------------------------------------------------------------------------
class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
*/
function PS_Pagination($connection, $sql, $rows_per_page = 30, $links_per_page = 10) {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = $rows_per_page;
$this->links_per_page = $links_per_page;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if(isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}
/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
if(!$this->conn) {
if($this->debug) echo "MySQL connection missing<br />";
return false;
}
$all_rs = @mysql_query($this->sql);
if(!$all_rs) {
if($this->debug) echo "SQL query failed. Check your query.<br />";
return false;
}
$this->total_rows = mysql_num_rows($all_rs);
@mysql_close($all_rs);
$this->max_pages = ceil($this->total_rows/$this->rows_per_page);
//Check the page value just in case someone is trying to input an aribitrary value
if($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page-1);
//Fetch the required result set
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.<br />";
return false;
}
return $rs;
}
/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag='First') {
if($this->page == 1) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
}
}
/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag='Last') {
if($this->page == $this->max_pages) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag=' >>') {
if($this->page < $this->max_pages) {
return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag='<<') {
if($this->page > 1) {
return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav() {
for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
if($this->page >= $i) {
$start = $i;
}
}
if($this->max_pages > $this->links_per_page) {
$end = $start+$this->links_per_page;
if($end > $this->max_pages) $end = $this->max_pages+1;
}
else {
$end = $this->max_pages;
}
$links = '';
for( $i=$start ; $i<$end ; $i++) {
if($i == $this->page) {
$links .= " $i ";
}
else {
$links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> ';
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();
}
/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
*/
// ------------------------------------------------------------------------
class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
*/
function PS_Pagination($connection, $sql, $rows_per_page = 30, $links_per_page = 10) {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = $rows_per_page;
$this->links_per_page = $links_per_page;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if(isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}
/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
if(!$this->conn) {
if($this->debug) echo "MySQL connection missing<br />";
return false;
}
$all_rs = @mysql_query($this->sql);
if(!$all_rs) {
if($this->debug) echo "SQL query failed. Check your query.<br />";
return false;
}
$this->total_rows = mysql_num_rows($all_rs);
@mysql_close($all_rs);
$this->max_pages = ceil($this->total_rows/$this->rows_per_page);
//Check the page value just in case someone is trying to input an aribitrary value
if($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page-1);
//Fetch the required result set
$rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}");
if(!$rs) {
if($this->debug) echo "Pagination query failed. Check your query.<br />";
return false;
}
return $rs;
}
/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag='First') {
if($this->page == 1) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
}
}
/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag='Last') {
if($this->page == $this->max_pages) {
return $tag;
}
else {
return '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag=' >>') {
if($this->page < $this->max_pages) {
return '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag='<<') {
if($this->page > 1) {
return '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
}
else {
return $tag;
}
}
/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav() {
for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
if($this->page >= $i) {
$start = $i;
}
}
if($this->max_pages > $this->links_per_page) {
$end = $start+$this->links_per_page;
if($end > $this->max_pages) $end = $this->max_pages+1;
}
else {
$end = $this->max_pages;
}
$links = '';
for( $i=$start ; $i<$end ; $i++) {
if($i == $this->page) {
$links .= " $i ";
}
else {
$links .= ' <a href="'.$this->php_self.'?page='.$i.'">'.$i.'</a> ';
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst().' '.$this->renderPrev().' '.$this->renderNav().' '.$this->renderNext().' '.$this->renderLast();
}
/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
Kind regarts
Voorbeelden:
http://www.hollandsail.nl/
http://www.devriessails.nl/
Gewijzigd op 08/12/2010 13:49:06 door Joakim Broden
Waarom doe je dan niet gewoon zo:
want je pagina nummering heb je dan niet nodig.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$sql = "SELECT naam FROM boten";
$query = mysql_query($sql) or die (mysql_error ());
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
$sql = "SELECT naam FROM boten";
$query = mysql_query($sql) or die (mysql_error ());
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
Gewijzigd op 08/12/2010 15:31:20 door Mitchel V
Mitchel V op 08/12/2010 15:30:43:
Klopt het dat dit een script is voor pagina nummers waarbij je je boten DB hebt geselecteerd?
Waarom doe je dan niet gewoon zo:
want je pagina nummering heb je dan niet nodig.
Waarom doe je dan niet gewoon zo:
want je pagina nummering heb je dan niet nodig.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$sql = "SELECT naam FROM boten";
$query = mysql_query($sql) or die (mysql_error ());
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
$sql = "SELECT naam FROM boten";
$query = mysql_query($sql) or die (mysql_error ());
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
Gebruik geen die, toon geen errors en hij wilt juist maar een paar boten per pagina. Niet alle boten, zoals jij doet.
Nick cortez op 08/12/2010 13:31:29:
Beste,
Op mijn website www.deltaboatcenter.nl worden boten verkocht maar nu wil ik dus bij de boten niet 5 boten onder elkaar maar alle boten op een pagina.
Ik heb hier een script (pagination) dat er voor moet zorgen dat ik dit kan veranderen maar ik heb in dit script aangegeven dat ik 30 boten op de pagina wil maar dat doet hij dus niet.
iemand een idee wat er fout aan is.
(...)
Kind regarts
Op mijn website www.deltaboatcenter.nl worden boten verkocht maar nu wil ik dus bij de boten niet 5 boten onder elkaar maar alle boten op een pagina.
Ik heb hier een script (pagination) dat er voor moet zorgen dat ik dit kan veranderen maar ik heb in dit script aangegeven dat ik 30 boten op de pagina wil maar dat doet hij dus niet.
iemand een idee wat er fout aan is.
(...)
Kind regarts
Leuk hoor, maar geef even aan wat er precies mis gaat. Geef ons niet alleen een klasse. Die gaan we niet bekijken. Laat relevante code zien. (Die klasse is overigs php4...)
Groetjes
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* PHP 4 Database Class
* Performs basic MySQL database interactions
*/
class Database {
var $host = 'localhost';
var $user = '******';
var $pass = '******';
var $db = '******';
var $conn; // Database connection
var $last_query; // Results of last query
var $last_sql; // String that contains last sql query
var $show_errors = true; // Whether or not to show error messages
/**
* Constructor function
* Connects and selects database
*
* @param string MySQL Host
* @param string MySQL Username
* @param string MySQL Password
* @param string MySQL Database name
* @return link Connection link
*/
function Database($host = '', $user = '', $pass = '', $db = '') {
$host = !empty($host) ? $host : $this->host;
$user = !empty($user) ? $user : $this->user;
$pass = !empty($pass) ? $pass : $this->pass;
$db = !empty($db) ? $db : $this->db;
$this->conn = mysql_connect($host, $user, $pass) or $this->error('Could not connect to database. Make sure settings are correct.');
if (is_resource($this->conn)) {
mysql_select_db($db, $this->conn) or $this->error("Database '$db' could not be found.");
return $this->conn;
}
return false;
}
/**
* Execute a query on the database
*
* @param string SQL query to execute
* @return query The query executed
*/
function query($sql)
{
is_resource($this->conn) || $this->Database();
$this->last_sql = $sql;
return $this->last_query = mysql_query($sql, $this->conn) or $this->error();
}
/**
* Very simple select
*
* @param string Table name to select from
* @param string What to order by
* @param string Where statement
* @param string Columns to select
* @return result Result of query
*/
function select($table, $orderby = '', $where = '', $cols = '*', $limit = '',$groupby='') {
$groupby = !empty($groupby) ? "GROUP BY $groupby" : '';
$orderby = !empty($orderby) ? "ORDER BY $orderby" : '';
$where = !empty($where) ? "WHERE $where" : '';
$limit = !empty($limit) ? "limit $limit" : '';
/*echo "SELECT $cols FROM $table $where $groupby $orderby $limit";
exit;*/
return $this->query("SELECT $cols FROM $table $where $groupby $orderby $limit ");
//echo $table;
//exit;/
}
/**
* Performs an insert query
*
* @param string Table name to query
* @param array Associative array of Column => Value to insert
* @return result Result of query
*/
function insert($table, $data) {
if (!is_array($data))
return false;
foreach ($data as $col => $value)
$data[$col] = $this->escape($value);
$cols = array_keys($data);
$vals = array_values($data);
//echo "INSERT INTO $table (".implode(',', $cols).") VALUES (".implode(',', $vals).")";
//exit;
$this->query("INSERT INTO $table (".implode(',', $cols).") VALUES (".implode(',', $vals).")");
//exit;
return mysql_insert_id();
}
/**
* Updates a row
*
* @param string Table name to query
* @param array Associtive array of columns to update
* @param string Where clause
* @return result Result of query
*/
function update($table, $data, $where) {
if (!is_array($data))
return false;
foreach ($data as $col => $value) {
$vals[] = $col.' = '.$this->escape($value);
}
//echo "UPDATE $table SET ".implode(',', $vals)." WHERE $where";
return $this->query("UPDATE $table SET ".implode(',', $vals)." WHERE $where");
}
/**
* Delete a single row
*
* @param string Table name to query
* @param string The column to match against
* @param string Value to match against column
* @return result Result of query
*/
function delete($table, $where) {
return $this->query("DELETE FROM $table WHERE $where");
}
/**
* Get results of query
*
* @param string Return as object or array
* @return result Result of query
*/
function get($type = 'object') {
$type = $type == 'object' ? 'mysql_fetch_object' : 'mysql_fetch_assoc';
if (is_resource($this->last_query)) {
while($rows = $type($this->last_query))
$results[] = $rows;
}
else $this->error();
return (!empty($results)) ? $results : null;
}
/**
* Get first result of query
*
* @param string Return as object or array
* @return result Result of query
*/
function get_first($type = 'object') {
$type = $type == 'object' ? 'mysql_fetch_object' : 'mysql_fetch_assoc';
if (is_resource($this->last_query))
return $type($this->last_query);
else $this->error();
}
/**
* Escape strings
*
* @param mixed String to escape
* @return string Escaped string, ready for SQL insertion
*/
function escape($data) {
switch(gettype($data)) {
case 'string':
$data = "'".mysql_real_escape_string($data)."'";
break;
case 'boolean':
$data = (int) $data;
break;
case 'double':
$data = sprintf('%F', $data);
break;
default:
$data = ($data === null) ? 'null' : $data;
}
return (string) $data;
}
/**
* Show simple error messages to help aid development process
*
* @param string Custom error message to show
* @return death Error page
*/
function error($msg = '') {
if ($this->show_errors === true) {
$error = '<h1>Error!</h1>';
if (!empty($msg))
$error .= "$msg<br />";
if (mysql_error())
$error .= '<b>MySQL Error:</b> '.mysql_error().'<br />';
if (isset($this->last_sql))
$error .= '<b>SQL Statement:</b> '.$this->last_sql;
die($error);
}
}
function numRecord()
{
return mysql_num_rows($this->last_query);
}
function lastid()
{
return mysql_insert_id();
}
function getConnection()
{
return $this->conn ;
}
}
?>
/**
* PHP 4 Database Class
* Performs basic MySQL database interactions
*/
class Database {
var $host = 'localhost';
var $user = '******';
var $pass = '******';
var $db = '******';
var $conn; // Database connection
var $last_query; // Results of last query
var $last_sql; // String that contains last sql query
var $show_errors = true; // Whether or not to show error messages
/**
* Constructor function
* Connects and selects database
*
* @param string MySQL Host
* @param string MySQL Username
* @param string MySQL Password
* @param string MySQL Database name
* @return link Connection link
*/
function Database($host = '', $user = '', $pass = '', $db = '') {
$host = !empty($host) ? $host : $this->host;
$user = !empty($user) ? $user : $this->user;
$pass = !empty($pass) ? $pass : $this->pass;
$db = !empty($db) ? $db : $this->db;
$this->conn = mysql_connect($host, $user, $pass) or $this->error('Could not connect to database. Make sure settings are correct.');
if (is_resource($this->conn)) {
mysql_select_db($db, $this->conn) or $this->error("Database '$db' could not be found.");
return $this->conn;
}
return false;
}
/**
* Execute a query on the database
*
* @param string SQL query to execute
* @return query The query executed
*/
function query($sql)
{
is_resource($this->conn) || $this->Database();
$this->last_sql = $sql;
return $this->last_query = mysql_query($sql, $this->conn) or $this->error();
}
/**
* Very simple select
*
* @param string Table name to select from
* @param string What to order by
* @param string Where statement
* @param string Columns to select
* @return result Result of query
*/
function select($table, $orderby = '', $where = '', $cols = '*', $limit = '',$groupby='') {
$groupby = !empty($groupby) ? "GROUP BY $groupby" : '';
$orderby = !empty($orderby) ? "ORDER BY $orderby" : '';
$where = !empty($where) ? "WHERE $where" : '';
$limit = !empty($limit) ? "limit $limit" : '';
/*echo "SELECT $cols FROM $table $where $groupby $orderby $limit";
exit;*/
return $this->query("SELECT $cols FROM $table $where $groupby $orderby $limit ");
//echo $table;
//exit;/
}
/**
* Performs an insert query
*
* @param string Table name to query
* @param array Associative array of Column => Value to insert
* @return result Result of query
*/
function insert($table, $data) {
if (!is_array($data))
return false;
foreach ($data as $col => $value)
$data[$col] = $this->escape($value);
$cols = array_keys($data);
$vals = array_values($data);
//echo "INSERT INTO $table (".implode(',', $cols).") VALUES (".implode(',', $vals).")";
//exit;
$this->query("INSERT INTO $table (".implode(',', $cols).") VALUES (".implode(',', $vals).")");
//exit;
return mysql_insert_id();
}
/**
* Updates a row
*
* @param string Table name to query
* @param array Associtive array of columns to update
* @param string Where clause
* @return result Result of query
*/
function update($table, $data, $where) {
if (!is_array($data))
return false;
foreach ($data as $col => $value) {
$vals[] = $col.' = '.$this->escape($value);
}
//echo "UPDATE $table SET ".implode(',', $vals)." WHERE $where";
return $this->query("UPDATE $table SET ".implode(',', $vals)." WHERE $where");
}
/**
* Delete a single row
*
* @param string Table name to query
* @param string The column to match against
* @param string Value to match against column
* @return result Result of query
*/
function delete($table, $where) {
return $this->query("DELETE FROM $table WHERE $where");
}
/**
* Get results of query
*
* @param string Return as object or array
* @return result Result of query
*/
function get($type = 'object') {
$type = $type == 'object' ? 'mysql_fetch_object' : 'mysql_fetch_assoc';
if (is_resource($this->last_query)) {
while($rows = $type($this->last_query))
$results[] = $rows;
}
else $this->error();
return (!empty($results)) ? $results : null;
}
/**
* Get first result of query
*
* @param string Return as object or array
* @return result Result of query
*/
function get_first($type = 'object') {
$type = $type == 'object' ? 'mysql_fetch_object' : 'mysql_fetch_assoc';
if (is_resource($this->last_query))
return $type($this->last_query);
else $this->error();
}
/**
* Escape strings
*
* @param mixed String to escape
* @return string Escaped string, ready for SQL insertion
*/
function escape($data) {
switch(gettype($data)) {
case 'string':
$data = "'".mysql_real_escape_string($data)."'";
break;
case 'boolean':
$data = (int) $data;
break;
case 'double':
$data = sprintf('%F', $data);
break;
default:
$data = ($data === null) ? 'null' : $data;
}
return (string) $data;
}
/**
* Show simple error messages to help aid development process
*
* @param string Custom error message to show
* @return death Error page
*/
function error($msg = '') {
if ($this->show_errors === true) {
$error = '<h1>Error!</h1>';
if (!empty($msg))
$error .= "$msg<br />";
if (mysql_error())
$error .= '<b>MySQL Error:</b> '.mysql_error().'<br />';
if (isset($this->last_sql))
$error .= '<b>SQL Statement:</b> '.$this->last_sql;
die($error);
}
}
function numRecord()
{
return mysql_num_rows($this->last_query);
}
function lastid()
{
return mysql_insert_id();
}
function getConnection()
{
return $this->conn ;
}
}
?>
Toevoeging op 08/12/2010 16:56:52:
ik heb er niet zo heeeel veel verstand van (geen eigenlijk)
maar het moet toch wel gewoon heel invoudig zijn.? moet ik niet alles met pagination weghalen ?
groetjes
Lees wat ik heb gepost (en lees ook de andere reacties).
Je hoeft dus geen pagination...
Nick cortez op 08/12/2010 13:31:29:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
?>
class PS_Pagination {
var $php_self;
var $rows_per_page; //Number of records to display per page
var $total_rows; //Total number of rows returned by the query
var $links_per_page; //Number of links to display per page
var $sql;
var $debug = false;
var $conn;
var $page;
var $max_pages;
var $offset;
?>
Bij var $rows_per_page definieer je hoeveel rows hij moet laten zien.
Dus in de code waar mee je hem uitleest zeg je gewoon dat hij 99999999999 rows mag laten zien? :)
Karl Karl op 08/12/2010 15:38:42:
Gebruik geen die, toon geen errors en hij wilt juist maar een paar boten per pagina. Niet alle boten, zoals jij doet.
Mitchel V op 08/12/2010 15:30:43:
Klopt het dat dit een script is voor pagina nummers waarbij je je boten DB hebt geselecteerd?
Waarom doe je dan niet gewoon zo:
want je pagina nummering heb je dan niet nodig.
Waarom doe je dan niet gewoon zo:
want je pagina nummering heb je dan niet nodig.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$sql = "SELECT naam FROM boten LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
$sql = "SELECT naam FROM boten LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 0){
echo 'Er zijn geen boten gevonden!<br>';
}else{
while($row = mysql_fetch_assoc($query)){
echo $row['naam'].'<br>';
}
}
?>
Gebruik geen die, toon geen errors en hij wilt juist maar een paar boten per pagina. Niet alle boten, zoals jij doet.
$sql = "SELECT naam FROM boten LIMIT 5";
zo? :P
@Karl Karl
Nick cortez op 08/12/2010 16:53:03:
dus niet net zoals nu maar 5 boten per pagina, gewoon alle boten op 1 pagina.
Dus was mijn code toch goed (buiten de DIE om te controleren).
Gewijzigd op 09/12/2010 07:57:01 door Mitchel V