Welcome Guest, Not a member yet? Register   Sign In
help with a custom field_data()
#1

[eluser]dexter21[/eluser]
hi , iam trying to get all fields names and values from a table . i've made a custom query that return an array of elements.if i print $campo->valor, and $campo->nombre i obtain the results but if i call to function in a view it said that campo->nombre and campo->valor dont exisxt;

basicly i use following function as:
Code:
$sql = "select * from news";
$sql  .= " where nCodnoticia = 1";
registro=$this->db->query2($sql,$this->tabla);



$registro->campos[0]->type ->ok it print it
$registro->campos[0]->valor ->it give me an error, dont exist


any could help me plz ?


following function:

Code:
function query2($sql,$tabla)
    {

        $query = $this->query($sql);


        if ($query->num_rows() > 0)
        {
            $objeto = null;
            $objeto->campos = $query->field_data();

            $valores = $query->row();
            
            foreach($objeto->campos as $campo)
            {    
            
               echo $campo->name."<br>";
               echo $campo->type."<br>";
               echo $campo->max_length."<br>";
               echo $campo->primary_key."<br>";
                              

                foreach($valores as $nombre=>$valor)
                {
                    if($nombre == $campo->name)
                    {    
                    
                           $campo->valor = $valor;
                           $campo->nombre = $this->traduceCampo($tabla,$campo->name);


                            echo "campo->valor:".$campo->valor."<br>";
                            echo "campo->nombre:".$campo->nombre."<br>";
                                                
                           break;  
                     }
                 }
                
           }
        }
        else
        {
            $objeto = null;
            $objeto->campos = $query->field_data();
            foreach($objeto->campos as $campo)
            {
                   $campo->valor = null;
                   $campo->nombre = $campo->name;
            }
        }
        //echo var_dump($objeto);      
       return $objeto;
    }
#2

[eluser]gtech[/eluser]
[strike]can you do us a favour and put your code in [ code ] [/ code ] brackets so the code gets indented properly and is readable.. cheers.[/strike] thank you.
#3

[eluser]gtech[/eluser]
[strike]Ok I am a bit confused. you have put your query2 function in the database drivers code rather than a model, or have I missed a trick here? I didn't think it was good practice to do this..

have you read the bit in the documentation about views, models and controllers?
[/strike]
see fix below.
#4

[eluser]dexter21[/eluser]
yes the query2 is in db_driver.php and i call it from a helper . the problem that i have is that inside de
Code:
if ($query->num_rows() > 0)

}
the object have the right values , $objeto->campos->valor has value, but after the close of
if , when i return $objeto, there $objeto->campos->valor is null. I dont know why this field reset.

any help?
#5

[eluser]gtech[/eluser]
code fix: (note change in first loop, and variables added to return object)
Code:
function query2($sql,$tabla)
{
    $query = $this->query($sql);
    if ($query->num_rows() > 0)
    {
        $objeto = null;
        $objeto->campos = $query->field_data();

        $valores = $query->row();

        // use $key=>$campo so we can add to objecto using key
        foreach($objeto->campos as $key=>$campo)
        {        
            echo $campo->name."<br>";
            echo $campo->type."<br>";
            echo $campo->max_length."<br>";
            echo $campo->primary_key."<br>";
                              

            foreach($valores as $nombre=>$valor)
            {
                if($nombre == $campo->name)
                {
                    $nombre = $this->traduceCampo($tabla,$campo->name);

                    // you need to add your data to the return objecto
                    // as you were working with a copy of the object.
                    $objecto->campos[$key]->valor = $valor;
                    $objecto->campos[$key]->nombre = $nombre;
                    break;  
                }
            }    
        }
    } else {
        $objeto = null;
        $objeto->campos = $query->field_data();
        // again fix code in loop so that $campo gets added to $objecto.
        foreach($objeto->campos as $key=>$campo)
        {
            $objecto->campos[$key]->valor = null;
            $objecto->campos[$key]->nombre = $campo->name;
        }
    }      
    return $objeto;
}

>I have not tested this! But you were not adding valor & nombre to $objecto.

>I seriously would move your code from db_driver to a model!

>The db_driver code is core to CodeIgniter and may change when a new CodeIgniter version is released.

>You can call a model from a helper.




Theme © iAndrew 2016 - Forum software by © MyBB