Take two:
[www-register-wizard.git] / database / DB_result.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Database Result Class\r
20  *\r
21  * This is the platform-independent result class.\r
22  * This class will not be called directly. Rather, the adapter\r
23  * class for the specific database will extend and instantiate it.\r
24  *\r
25  * @category    Database\r
26  * @author              ExpressionEngine Dev Team\r
27  * @link                http://codeigniter.com/user_guide/database/\r
28  */\r
29 class CI_DB_result {\r
30 \r
31         var $conn_id            = NULL;\r
32         var $result_id          = NULL;\r
33         var $result_array       = array();\r
34         var $result_object      = array();\r
35         var $current_row        = 0;\r
36         var $num_rows           = 0;\r
37         var $row_data           = NULL;\r
38 \r
39 \r
40         /**\r
41          * Query result.  Acts as a wrapper function for the following functions.\r
42          *\r
43          * @access      public\r
44          * @param       string  can be "object" or "array"\r
45          * @return      mixed   either a result object or array \r
46          */     \r
47         function result($type = 'object')\r
48         {       \r
49                 return ($type == 'object') ? $this->result_object() : $this->result_array();\r
50         }\r
51 \r
52         // --------------------------------------------------------------------\r
53 \r
54         /**\r
55          * Query result.  "object" version.\r
56          *\r
57          * @access      public\r
58          * @return      object\r
59          */     \r
60         function result_object()\r
61         {\r
62                 if (count($this->result_object) > 0)\r
63                 {\r
64                         return $this->result_object;\r
65                 }\r
66                 \r
67                 // In the event that query caching is on the result_id variable \r
68                 // will return FALSE since there isn't a valid SQL resource so \r
69                 // we'll simply return an empty array.\r
70                 if ($this->result_id === FALSE OR $this->num_rows() == 0)\r
71                 {\r
72                         return array();\r
73                 }\r
74 \r
75                 $this->_data_seek(0);\r
76                 while ($row = $this->_fetch_object())\r
77                 {\r
78                         $this->result_object[] = $row;\r
79                 }\r
80                 \r
81                 return $this->result_object;\r
82         }\r
83         \r
84         // --------------------------------------------------------------------\r
85 \r
86         /**\r
87          * Query result.  "array" version.\r
88          *\r
89          * @access      public\r
90          * @return      array\r
91          */     \r
92         function result_array()\r
93         {\r
94                 if (count($this->result_array) > 0)\r
95                 {\r
96                         return $this->result_array;\r
97                 }\r
98 \r
99                 // In the event that query caching is on the result_id variable \r
100                 // will return FALSE since there isn't a valid SQL resource so \r
101                 // we'll simply return an empty array.\r
102                 if ($this->result_id === FALSE OR $this->num_rows() == 0)\r
103                 {\r
104                         return array();\r
105                 }\r
106 \r
107                 $this->_data_seek(0);\r
108                 while ($row = $this->_fetch_assoc())\r
109                 {\r
110                         $this->result_array[] = $row;\r
111                 }\r
112                 \r
113                 return $this->result_array;\r
114         }\r
115 \r
116         // --------------------------------------------------------------------\r
117 \r
118         /**\r
119          * Query result.  Acts as a wrapper function for the following functions.\r
120          *\r
121          * @access      public\r
122          * @param       string\r
123          * @param       string  can be "object" or "array"\r
124          * @return      mixed   either a result object or array \r
125          */     \r
126         function row($n = 0, $type = 'object')\r
127         {\r
128                 if ( ! is_numeric($n))\r
129                 {\r
130                         // We cache the row data for subsequent uses\r
131                         if ( ! is_array($this->row_data))\r
132                         {\r
133                                 $this->row_data = $this->row_array(0);\r
134                         }\r
135                 \r
136                         // array_key_exists() instead of isset() to allow for MySQL NULL values\r
137                         if (array_key_exists($n, $this->row_data))\r
138                         {\r
139                                 return $this->row_data[$n];\r
140                         }\r
141                         // reset the $n variable if the result was not achieved                 \r
142                         $n = 0;\r
143                 }\r
144                 \r
145                 return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);\r
146         }\r
147 \r
148         // --------------------------------------------------------------------\r
149 \r
150         /**\r
151          * Assigns an item into a particular column slot\r
152          *\r
153          * @access      public\r
154          * @return      object\r
155          */     \r
156         function set_row($key, $value = NULL)\r
157         {\r
158                 // We cache the row data for subsequent uses\r
159                 if ( ! is_array($this->row_data))\r
160                 {\r
161                         $this->row_data = $this->row_array(0);\r
162                 }\r
163         \r
164                 if (is_array($key))\r
165                 {\r
166                         foreach ($key as $k => $v)\r
167                         {\r
168                                 $this->row_data[$k] = $v;\r
169                         }\r
170                         \r
171                         return;\r
172                 }\r
173         \r
174                 if ($key != '' AND ! is_null($value))\r
175                 {\r
176                         $this->row_data[$key] = $value;\r
177                 }\r
178         }\r
179 \r
180         // --------------------------------------------------------------------\r
181 \r
182         /**\r
183          * Returns a single result row - object version\r
184          *\r
185          * @access      public\r
186          * @return      object\r
187          */     \r
188         function row_object($n = 0)\r
189         {\r
190                 $result = $this->result_object();\r
191                 \r
192                 if (count($result) == 0)\r
193                 {\r
194                         return $result;\r
195                 }\r
196 \r
197                 if ($n != $this->current_row AND isset($result[$n]))\r
198                 {\r
199                         $this->current_row = $n;\r
200                 }\r
201 \r
202                 return $result[$this->current_row];\r
203         }\r
204 \r
205         // --------------------------------------------------------------------\r
206 \r
207         /**\r
208          * Returns a single result row - array version\r
209          *\r
210          * @access      public\r
211          * @return      array\r
212          */     \r
213         function row_array($n = 0)\r
214         {\r
215                 $result = $this->result_array();\r
216 \r
217                 if (count($result) == 0)\r
218                 {\r
219                         return $result;\r
220                 }\r
221                         \r
222                 if ($n != $this->current_row AND isset($result[$n]))\r
223                 {\r
224                         $this->current_row = $n;\r
225                 }\r
226                 \r
227                 return $result[$this->current_row];\r
228         }\r
229 \r
230                 \r
231         // --------------------------------------------------------------------\r
232 \r
233         /**\r
234          * Returns the "first" row\r
235          *\r
236          * @access      public\r
237          * @return      object\r
238          */     \r
239         function first_row($type = 'object')\r
240         {\r
241                 $result = $this->result($type);\r
242 \r
243                 if (count($result) == 0)\r
244                 {\r
245                         return $result;\r
246                 }\r
247                 return $result[0];\r
248         }\r
249         \r
250         // --------------------------------------------------------------------\r
251 \r
252         /**\r
253          * Returns the "last" row\r
254          *\r
255          * @access      public\r
256          * @return      object\r
257          */     \r
258         function last_row($type = 'object')\r
259         {\r
260                 $result = $this->result($type);\r
261 \r
262                 if (count($result) == 0)\r
263                 {\r
264                         return $result;\r
265                 }\r
266                 return $result[count($result) -1];\r
267         }       \r
268 \r
269         // --------------------------------------------------------------------\r
270 \r
271         /**\r
272          * Returns the "next" row\r
273          *\r
274          * @access      public\r
275          * @return      object\r
276          */     \r
277         function next_row($type = 'object')\r
278         {\r
279                 $result = $this->result($type);\r
280 \r
281                 if (count($result) == 0)\r
282                 {\r
283                         return $result;\r
284                 }\r
285 \r
286                 if (isset($result[$this->current_row + 1]))\r
287                 {\r
288                         ++$this->current_row;\r
289                 }\r
290                                 \r
291                 return $result[$this->current_row];\r
292         }\r
293         \r
294         // --------------------------------------------------------------------\r
295 \r
296         /**\r
297          * Returns the "previous" row\r
298          *\r
299          * @access      public\r
300          * @return      object\r
301          */     \r
302         function previous_row($type = 'object')\r
303         {\r
304                 $result = $this->result($type);\r
305 \r
306                 if (count($result) == 0)\r
307                 {\r
308                         return $result;\r
309                 }\r
310 \r
311                 if (isset($result[$this->current_row - 1]))\r
312                 {\r
313                         --$this->current_row;\r
314                 }\r
315                 return $result[$this->current_row];\r
316         }\r
317 \r
318         // --------------------------------------------------------------------\r
319 \r
320         /**\r
321          * The following functions are normally overloaded by the identically named\r
322          * methods in the platform-specific driver -- except when query caching\r
323          * is used.  When caching is enabled we do not load the other driver.\r
324          * These functions are primarily here to prevent undefined function errors\r
325          * when a cached result object is in use.  They are not otherwise fully\r
326          * operational due to the unavailability of the database resource IDs with\r
327          * cached results.\r
328          */\r
329         function num_rows() { return $this->num_rows; }\r
330         function num_fields() { return 0; }\r
331         function list_fields() { return array(); }\r
332         function field_data() { return array(); }       \r
333         function free_result() { return TRUE; }\r
334         function _data_seek() { return TRUE; }\r
335         function _fetch_assoc() { return array(); }     \r
336         function _fetch_object() { return array(); }\r
337         \r
338 }\r
339 // END DB_result class\r
340 \r
341 /* End of file DB_result.php */\r
342 /* Location: ./system/database/DB_result.php */