CodeIgniter Forums
function in model return null - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: function in model return null (/thread-8791.html)



function in model return null - El Forum - 05-31-2008

[eluser]dexter21[/eluser]
Hi, i have a function in a model for making form fields of a sql statement :
Code:
function query2($sql,$tabla)
    {
        $query = $this->db->query($sql);

        if ($query->num_rows() > 0)
        {
            $objeto = null;
            $objeto->campos = $query->field_data();
            
            $valores = $query->row();
            
            foreach($objeto->campos as $key=>$campo)
            {
            
                foreach($valores as $nombre=>$valor)
                   {
                
                       if($nombre == $campo->name)
                       {

                           $campo->valor = $valor;
                        $campo->nombre=$campo->name;
                        
                        
                        echo "campo->nombre:".$campo->nombre."<br>";                                            
                        echo "campo->valor:".$campo->valor."<br>";
                    
                           break;  
                        
                       }
                    
                   }

           }
        }
        else
        {
            $objeto = null;
            $objeto->campos = $query->field_data();
            foreach($objeto->campos as $campo)
            {
                   $campo->valor = null;
                   $campo->nombre = $this->traduceCampo($tabla,$campo->name);
            }
        }

        
              
       return $objeto;
    }

the echo inside if ,print nombre and valor rightly, but after close the if those values are null, and of course the values of the return are also null.

i get $objeto values from a view with:
Code:
foreach($registro->campos as $campo)
    {
        
            echo "campo->type:".$campo->type."<br>";
            echo "campo->name:".$campo->name."<br>";
            echo "campo->nombre:".$campo->nombre."<br>";
            echo "campo->valor:".$campo->valor."<br>";
    
    }


//i get error
//Message: Undefined property: nombre
//Message: Undefined property: valor

Anyone could help me plz


many thanks


function in model return null - El Forum - 05-31-2008

[eluser]gtech[/eluser]
hello I gave the answer in the last post, with the code fix if you scroll to the bottom of the thread... you are not adding the variables to the return object.

[url="http://ellislab.com/forums/viewthread/80834/"]http://ellislab.com/forums/viewthread/80834/[/url] <-- has the fix that you can copy and paste into your code

did the fix not work for you?


function in model return null - El Forum - 05-31-2008

[eluser]gtech[/eluser]
I have just updated the fix, to fix the second loop as well.
see link in above reply. you just need to convert the code to go in the model


function in model return null - El Forum - 06-01-2008

[eluser]dexter21[/eluser]
hi, same problem , the most strange is that i've copied project to local, and it run ok, but in remote server continue the problem. i've lost about what could be the problem.