views:

54

answers:

1

I have a foreach and then a foreach inside a foreach. The first foreach seems to work fine and I don't get any php errors. However in each subsequent foreach loop I get a Message: Undefined property: stdClass::$filename error (replace filename with every single identifier). Can someone help me out and tell me what I'm doing wrong? Thanks

    if($query = $this->db->get()){

        foreach ($query->result() as $row)
        {
            $textable = $row->text_table;
        }

        $this->db->distinct('filename');
        $this->db->select('location');
        $this->db->from('TranslateData');

        if($query1 = $this->db->get()){

            foreach($query1->result() as $row1){

                $this->db->select('string_id');
                $this->db->select($textable);
                $this->db->where('filename', $row1->filename);
                $this->db->from('TranslateData');

                $query2 = $this->db->get();

                $xml = '<?xml version="1.0" encoding="utf-8" ?>'.PHP_EOL;
                $xml .= '<data>'.PHP_EOL;

                foreach($query2 as $row2){

                    $xml .= '<item>'.PHP_EOL;
                    $xml .= '   <name>'.$row2->string_id.'</name>'.PHP_EOL;
                    $xml .= '   <copy><![CDATA['.$row2->$textable.']]></copy>'.PHP_EOL;
                    $xml .= '</item>'.PHP_EOL;

                }

                $xml .= '</data>'.PHP_EOL;

                $path = str_replace('/', '', $row1->location);

                $this->save_xml($path, $row1->filename, $xml);

            }
        }
    }   
+1  A: 

You have shown two ways of calling a resultset. I hope that is fine.

foreach($query1->result() as $row1)
foreach($query2 as $row2)

I believe, your DB library resultset method (examples above) could be returning reference to same array whenever you iterate it. So, when you called it within an existing iteration, it lost original $row1 reference. which means, $row1 == $row2 - which caused the undefined error.

Try print_r or as @RobertPitt suggested. place it at the first line after the second foreach.

thevikas