resultset = $resultset;
$this->index = 0;
$this->max_index = (mysql_num_rows($resultset) - 1);
}
/*** méthodes ArrayAccess ***/
function offsetExists($offset){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
if($offset >= $this->index && $offset <= $this->max_index)
return true;
return false;
}
function offsetGet($offset){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
if(mysql_data_seek($this->resultset, $offset) === true)
return mysql_fetch_array($this->resultset);
return NULL;
}
function offsetSet($offset,$value){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
throw new Exception("This resultset is in read-only mode");
}
function offsetUnset($offset){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
throw new Exception("This resultset is in read-only mode");
}
// ajoute une valeur à la fin
// de notre objet tableau
function append($value){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
throw new Exception("This resultset is in read-only mode");
}
/**
* Interface Countable
* not implemented yet
* because it gives an
* error : 'not found'
*/
function count(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
else
return ($this->max_index + 1);
}
/*** Iterator ***/
function current(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
return mysql_fetch_assoc($this->resultset);
}
function key(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
return $this->index;
}
function next(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
$this->index++;
}
function rewind(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
$this->index = 0;
}
function valid(){
if(!isset($this->resultset) || !isset($this->index) || !isset($this->max_index))
throw new Exception("No resultset");
if($this->offsetExists($this->index))
return true;
return false;
}
// permet de définir cet objet-ci comme
// étant un tableau
function getIterator(){
return $this;
}
function __destruct(){
mysql_free_result($this->resultset);
$this->resultset = NULL;
$this->index = NULL;
$this->max_index = NULL;
}
} // fin de la classe mysql_rs
/***** Exemple d'utilisation *****/
$handle = mysql_connect('localhost','root','rqhmap0m');
mysql_select_db('java',$handle);
$query = 'SELECT * FROM COFFEES';
$query = mysql_query($query) or die("Erreur à la sélection des nouvelles :: ".mysql_error());
$rs = new mysqlrs($query);
echo count($rs),'
';
foreach($rs as $rows){
foreach($rows as $key => $value){
echo $key,' = ',$value,'
';
}
echo '