Welcome Guest, Not a member yet? Register   Sign In
Trying to pass variable from 1 Function to Another to Put in Array within same Model
#1

[eluser]jshultz[/eluser]
Ok, that sounds really confusing. What I'm trying to do is this. I've got a function that uploads/resizes photos to the server. It stores the paths in the DB. I need to attach the id of the business to the row of photos.

Here's what I have so far:

Code:
function get_bus_id() {
        $userid = $this->tank_auth->get_user_id();
        $this->db->select('b.id');
        $this->db->from ('business AS b');
        $this->db->where ('b.userid', $userid);
        return $this->db->get();
    }

That get's the id of the business. Then, I have my upload function which is below:

Code:
/* Uploads images to the site and adds to the database. */
    function do_upload() {
        
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 2000
        );
        
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $image_data = $this->upload->data();
        
        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->gallery_path . '/thumbs',
            'maintain_ratio' => true,
            'width' => 150,
            'height' => 100
        );
        
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        
        $upload = $this->upload->data();
        $bus_id = $this->get_bus_id();
        
        $data = array(
            'userid' => $this->tank_auth->get_user_id(),
            'thumb' => $this->gallery_path . '/thumbs/' . $upload['file_name'],
            'fullsize' => $upload['full_path'],
            'busid'=> $bus_id['query'],
        );
        
        echo var_dump($bus_id);
        
        $this->db->insert('photos', $data);
    }

The problem I'm getting is the following:

Fatal error: Cannot use object of type CI_DB_mysql_result as array in /home/welcomet/public_html/application/models/gallery_model.php on line 48

I've tried all sorts of ways to get the value over, but my limited knowledge keeps getting in the way. Any help would be really appreciated.
#2

[eluser]zimco[/eluser]
Looks like you wanted to return an array from your model but instead you have returned an object then tried to use it as an array. I usually do something like this in my model if i want to return an array:
Code:
$query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result_array();
    }
#3

[eluser]jshultz[/eluser]
Ok, I tried that but I'm getting an array to string conversion message.

I should only retrieve on record from the get_bus_id function. One account can only have one business associated with it at this time.
#4

[eluser]jshultz[/eluser]
Ok, I tweaked it I think, I'm getting this error now:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: id

Filename: models/gallery_model.php

Line Number: 48


Line 48 is
Code:
'busid'=> $bus_id['id'],

my get_bus_id function now looks like this:

Code:
function get_bus_id() {
        $userid = $this->tank_auth->get_user_id();
        $this->db->select('b.id');
        $this->db->from ('business AS b');
        $this->db->where ('b.userid', $userid);
        $query = $this->db->get();
            if ($query->num_rows() > 0) {
            return $query->result_array();
    }

and I changed the do_upload() function so that it says this:
Code:
$bus_id = $this->get_bus_id();
#5

[eluser]vitoco[/eluser]
Try this code, and a question...what happens if the query doesn't return a row ??
Code:
function get_bus_id() {
        $userid = $this->tank_auth->get_user_id();
        $this->db->select('b.id');
        $this->db->from ('business AS b');
        $this->db->where ('b.userid', $userid);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
        {
            // RESULT ARRAY RETURN A MULTIDIMENSIONAL ARRAY e.g. ARRAY OF DB RECORDS
            // ( ROWS ), SO IT DOENS'T FIT
            //return $query->result_array();
            // THE CORRECT METHOD IS row_array(), THAT RETURN THE FIRST ROW OF THE
            // RECORDSET
            $row = $query->row_array();
            echo '<pre>'.print_r( $row , true ).'</pre>';
            return $row ;
        }
        // WHAT HAPPEN IF THE SELECT DOESN'T RETURN A ROW ??
        else
        {
            ;// ???
        }
    }
#6

[eluser]jshultz[/eluser]
I'm still receiving this though:

Severity: Notice

Message: Undefined index: id

Filename: models/gallery_model.php

Line Number: 48

i've checked my sql query and it should work. when i run the query manually i get the expected result.

This line should run the function: $bus_id = $this->get_bus_id();

and this line should take the value and store it in the array: 'busid'=> $bus_id['id'],

I've tried it without ['id'] and i got the same results.
#7

[eluser]vitoco[/eluser]
the code was edited and i've added a print_r to see the array data. try again please
#8

[eluser]D.Phil[/eluser]
Line 48 should be:
Code:
'busid'=> $bus_id[0]['id'],

or you could modify the get bus is function to look like this:

Code:
function get_bus_id() {
    $userid = $this->tank_auth->get_user_id();
    $this->db->select('b.id');
    $this->db->from ('business AS b');
    $this->db->where ('b.userid', $userid);
    $this->db->limit(1);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $row = $query->row();
        return $row->id();
    } else return false;
}
What that will do is limit the number of records returned to one.
#9

[eluser]jshultz[/eluser]
Array
(
[id] => 5
)
#10

[eluser]vitoco[/eluser]
so it's working...now you must change
Code:
$data = array(
            'userid' => $this->tank_auth->get_user_id(),
            'thumb' => $this->gallery_path . '/thumbs/' . $upload['file_name'],
            'fullsize' => $upload['full_path'],
            'busid'=> $bus_id['query'],
        );

to
Code:
$data = array(
            'userid' => $this->tank_auth->get_user_id(),
            'thumb' => $this->gallery_path . '/thumbs/' . $upload['file_name'],
            'fullsize' => $upload['full_path'],
            'busid'=> $bus_id['id'],
        );

and remove ( or comment to use it later ) the print_r statement




Theme © iAndrew 2016 - Forum software by © MyBB