Ver Mensaje Individual
  #10 (permalink)  
Antiguo 02/04/2012, 12:30
Maru77
 
Fecha de Ingreso: agosto-2003
Mensajes: 524
Antigüedad: 20 años, 8 meses
Puntos: 5
Respuesta: problema con foreach

Esta es la clase con la que trabajo.

Código PHP:
<?php 

# includes
include_once("conf.inc.php"); // aca conecta a la base
include_once(INCLUDES_PATH."messages.php");

class 
RecordSet{
    var 
$conection;
    var 
$fields = array();
    var 
$rs = array();
    var 
$position 0;
    var 
$bof false# flag Begin of File
    
var $eof false# flag End of File
    
    # constructor method of the class. Instanciate a conection object as a class atribute
    
function RecordSet($conn){
        
$this->conection = &$conn;
    }
    
    
# main method, to open, execute and populate a rs with the sql return
    
function open($sql){
        switch(
$this->conection->type){
            case 
"mysql":
                
$this->conection->open_conn();
                
$this->conection->executeQuery($sql);
                
$this->conection->close_conn();
                
# creating the array with the fields names
                
$n_fields mysql_num_fields($this->conection->result);
                for(
$i=0;$i<$n_fields;$i++){
                    
$this->fields[] = mysql_field_name($this->conection->result,$i);
                }
                
                
# creating the current recordset with the current register
                
$temp = array();
                while(
$row mysql_fetch_array($this->conection->result)){
                    for(
$i=0;$i<$n_fields;$i++){
                        
$temp[$this->fields[$i]] = $row[$i];
                        
                    }
                    
# inserting new array into the main array
                    
$this->rs[] = $temp;
                }
                if(
sizeof($this->rs) > 0){
                    
$this->bof true;
                    
$this->eof false;
                    
$this->position 0;
                }else{
                    
$this->bof true;
                    
$this->eof true;
                    
$this->position 0;
                }
                break;
            case 
"mssql":
                
# not implemented yet    
                
break;
            case 
"access":
                
# not implemented yet
                
$this->conection->open_conn();
                
$this->conection->executeQuery($sql);
                
$this->conection->close_conn();
                
# creating the array with the fields names
                
$n_fields odbc_num_fields($this->conection->result);
                for(
$i=0;$i<$n_fields;$i++){
                    
$this->fields[] = odbc_field_name($this->conection->result,$i);
                }
                
                
# creating the current recordset with the current register
                
$temp = array();
                while(
$row odbc_fetch_array($this->conection->result)){
                    for(
$i=0;$i<$n_fields;$i++){
                        
$temp[$this->fields[$i]] = $row[$i];
                        
                    }
                    
# inserting new array into the main array
                    
$this->rs[] = $temp;
                }
                if(
sizeof($this->rs) > 0){
                    
$this->bof true;
                    
$this->eof false;
                    
$this->position 0;
                }else{
                    
$this->bof true;
                    
$this->eof true;
                    
$this->position 0;
                }
                break;
            
# if you have another type of db, just create a new case here.
        
}    
    }
    
# yo
    
function close(){
        
$this->fields = array();
        
$this->rs = array();
        
$this->position 0;
        
$this->bof false;
        
$this->eof false;
    }
    function 
field($campo){
        if(
is_integer($campo)){
            return 
$this->rs[$this->position][$this->fields[$campo]];
        }else{
            return 
$this->rs[$this->position][$campo];
        }
    }
    function 
next(){    
        if (
$this->position >= sizeof($this->rs)){
            
# end of file, so we don´t do anything
            
PrintMessage("Warning : You have reached the end of file. The method next() will stop now.");
        }else{
            
$this->position++;
            if (
$this->bof){
                
$this->bof false;
            }
            if (
$this->position >= sizeof($this->rs)){
                
# end of file, so we set the eof flag
                
$this->eof true;
            }
        }
    }
    function 
previous(){
        if(
$this->bof){
            
# if flag bof is set, don´t do anything
        
}else{
            
$this->position--;
            if (
$this->eof){
                
$this->eof false;
            }
            if(
$this->position 0){
                
$this->bof true;
                
$this->position 0;
            }
        }
    }
    function 
first(){
        
$this->bof true;
        
$this->eof false;
        
$this->position 0;
    }
    function 
last(){
        
$this->eof true;
        
$this->bof false;
        
$this->position sizeof($this->rs) - 1;
    }
    function 
count_registers(){
        return 
sizeof($this->rs);
    }
    
#desde acá lo cambié
    
function get_record_by_ID($table$id$fields "*"){
        
$sql "SELECT $fields FROM $table WHERE id = '$id'";
        
/*$result = $this->sqlordie($sql);
        return mysql_fetch_assoc($result);*/
        
$this->open($sql);
        for(
$j=0;$j<sizeof($this->fields);$j++){
            
$campo=$this->fields[$j];
            
$record->$campo $this->field($j);
        }
        
$this->close();
        return 
$record;
    }
    function 
get_object_list($sql,$open=true){
        if(
$open)
            
$this->open($sql);
        
$object=array();
        for(
$i=0;$i<sizeof($this->rs);$i++){
            for(
$j=0;$j<sizeof($this->fields);$j++){
                
$campo=$this->fields[$j];
                
$object[$i]->$campo $this->rs[$i][$campo];
            }
        }
        if(
$open)
            
$this->close();
        return 
$object;
    }
    function 
get_object($sql){
        
$this->open($sql);
        foreach(
$this->fields as $campo){
            
$record->$campo $this->rs[0][$campo];    
        }
        
$this->close();
        return 
$record;
    }
    function 
login($sql){
        
$this->open($sql);
        foreach(
$this->fields as $campo){
            
$record->$campo $this->rs[0][$campo];    
        }
        
$this->close();
        return !
$this->eof?$record:false;
    }

}
?>

Este es el código con el que se recorre la tabla y deberia devolver los registros, pero solo lee 1 y lo repite 5 veces. En la tabla tengo cargados 2 registros. Además probé con otras tablas y hace lo mismo.

Código PHP:
<?php

$conn 
= new conection();
$rs = new RecordSet($conn);

$sql="SELECT * FROM `novedades`";

$novedades $rs->get_object($sql); 

foreach(
$novedades as $clave=>$valor){

echo 
$novedades->titulo;
echo 
$novedades->texto;

}

?>
__________________
Saludos!!!
Maru.-