Welcome Guest, Not a member yet? Register   Sign In
"You must submit a valid result object" returned even though I'm passing an object.
#1

[eluser]Josh Kendall[/eluser]
Hi, I hope someone can offer some advice. I'm trying to use the DB Utility to export a result as a CSV file. Problem is, I need to update part of the result before export. After about an hour of converting the result_array() to an object (since I couldn't update the object) I'm now being told "You must submit a valid result object", even though I'm submitting an object that looks exactly like the object $query->result() produces.

Here is the results of printing a "true" result object:

Code:
Array
(
    [0] => stdClass Object
        (
            [type] => staff
            [lastname] => Member1
            [firstname] => Staff
            [email] => [email protected]
            [status] =>
            [training] => 4
        )

    [1] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Basic
            [email] => [email protected]
            [status] => 0:0:0
            [training] => 4
        )

    [2] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Another Basic
            [email] => [email protected]
            [status] => 0:0:0
            [training] => 4
        )

)

and here is my object after making the changes and converting it from a result array to an object:

Code:
Array
(
    [0] => stdClass Object
        (
            [type] => staff
            [lastname] => Member1
            [firstname] => Staff
            [email] => [email protected]
            [status] =>
            [training] => 4
        )

    [1] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Basic
            [email] => [email protected]
            [status] => Elesson: 0, Page: 0
            [training] => 4
        )

    [2] => stdClass Object
        (
            [type] => user
            [lastname] => User
            [firstname] => Another Basic
            [email] => [email protected]
            [status] => Elesson: 0, Page: 0
            [training] => 4
        )

)

And here is the code I'm using in my controller:

Code:
// ------ EXPORT FUNCTIONS
        function export($file)
        {

            $data = explode('::', $file);
            $file = $data[1].'-'.time();
            $type = $data[0];
            $this->load->dbutil();
            if($type == 'user_progress'){
                $query = $this->db->query("SELECT * FROM `x_training_".$this->session->userdata('training')."_answers`");
            } else if($type == 'activity_export'){
                $query = $this->db->query("SELECT `type`, `lastname`, `firstname`, `email`, `status`, `training` FROM `x_users` where `type`!='admin' order by `training` ASC, `type` ASC");
            }
            $result = $query->result_array();
            $count = count($result);
            $i = 0;
            while($i < $count){
                if(ereg(':', $result[$i]['status'])){
                    $ex = explode(':', $result[$i]['status']);
                    $result[$i]['status'] = 'Elesson: '.$ex[1].', Page: '.$ex[2];
                }
                $i++;
            }

            $i = 0;
            $object = array();
            while($i < $count){
                $object[$i] = $this->arr2obj($result[$i]);
                $i++;
            }

            $csv = $this->dbutil->csv_from_result($object);
            $this->load->helper('download');
            if(force_download($file.'.xls', $csv)){
                redirect($_SERVER['HTTP_REFERER']);
            }
            
        }

and this is the function that converts the array to an object:

Code:
function arr2obj($arg_array) {
            $tmp = new stdClass; // start off a new (empty) object
            foreach ($arg_array as $key => $value) {
                if (is_array($value)) { // if its multi-dimentional, keep going :)
                    $tmp->$key = arr2obj($value);
                } else {
                    if (is_numeric($key)) { // can't do it with numbers :(
                        die("Cannot turn numeric arrays into objects!");
                    }
                    $tmp->$key = $value;
                }
            }
            return $tmp; // return the object!
        }

Any help would be appreciated.


Messages In This Thread
"You must submit a valid result object" returned even though I'm passing an object. - by El Forum - 02-04-2008, 11:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB