From 13626f1556884806dbcc387b7268975de2c0aac4 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Wed, 1 Oct 2008 14:04:03 +0000 Subject: Added PHP5 iterator to it_dbi: Allows foreach (new T_User...) --- it_dbi.class | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/it_dbi.class b/it_dbi.class index 153fdd6..356930e 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -143,7 +143,8 @@ function createclass($p) if (substr($classname, 0, 4) != 'PMA_') # It is designed behaviour that an error is generated if this class already exists! { - $code = "class $classname extends it_dbi + $interface = function_exists("interface_exists") && interface_exists("Iterator", false) ? "implements Iterator" : ""; + $code = "class $classname extends it_dbi $interface { function $classname(/* $query ... */) { @@ -677,6 +678,42 @@ function _get_field_info() return $result; } +# +# Implement PHP 5 Iterator interface to make foreach work +# Example: foreach (new T_User('firstname' => "foo") as $foouser) { ... } +# +function current() +{ + return $this; +} + +function key() +{ + return $this->_key; +} + +function next() +{ + $this->iterate(); +} + +function rewind() +{ + if (!$this->_result) # Object without query used in foreach + $this->select(); + + # Only rewind if not already at start and results present + if (!$this->_nofetch && mysql_num_rows($this->_result)) + mysql_data_seek($this->_result, 0); + + $this->iterate(); +} + +function valid() +{ + return (bool)$this->_data; +} + } /* End class it_dbi */ ?> -- cgit v1.2.3