note 89053 added to class.iterator
| From: | nullhility at gmail dot com | Date: | Thu, 19 Feb 2009 15:13:23 +0000 |
| Subject: | note 89053 added to class.iterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150859@lists.php.net to get a copy of this message | ||
Because the purpose of the above example is to illustrate the foreach statement's call sequence
I made my own example of the Iterator interface just to give other user's some idea of possible
solutions that might benefit from the interface.
<?php
class MysqlResult implements Iterator
{
private $result;
private $position;
private $row_data;
public function __construct ($result)
{
$this->result = $result;
$this->position = 0;
}
public function current ()
{
return $this->row_data;
}
public function key ()
{
return $this->position;
}
public function next ()
{
$this->position++;
$this->row_data = mysql_fetch_assoc($this->result);
}
public function rewind ()
{
$this->_position = 0;
mysql_data_seek($this->result, 0);
/* The initial call to valid requires that data
pre-exists in $this->row_data
*/
$this->row_data = mysql_fetch_assoc($this->result);
}
public function valid ()
{
return (boolean) $this->row_data;
}
}
$link = mysql_connect("localhost", "user");
mysql_select_db('example_db', $link);
$result = new MysqlResult(mysql_query("SELECT * FROM
example_tbl"));
foreach ($result as $pos => $row) {
var_dump($pos, $row);
}
?>
----
Server IP: 117.55.229.1
Probable Submitter: 124.177.95.8
----
Manual Page -- http://www.php.net/manual/en/class.iterator.php
Edit -- https://master.php.net/note/edit/89053
Del: integrated -- https://master.php.net/note/delete/89053/integrated
Del: useless -- https://master.php.net/note/delete/89053/useless
Del: bad code -- https://master.php.net/note/delete/89053/bad+code
Del: spam -- https://master.php.net/note/delete/89053/spam
Del: non-english -- https://master.php.net/note/delete/89053/non-english
Del: in docs -- https://master.php.net/note/delete/89053/in+docs
Del: other reasons-- https://master.php.net/note/delete/89053
Reject -- https://master.php.net/note/reject/89053
Search -- https://master.php.net/manage/user-notes.php