views:

22

answers:

3

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:

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
          $query->row_array();
            }

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

/* 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['id'],
        );

        echo var_dump($bus_id);

        $this->db->insert('photos', $data);
    }

The problem I’m getting is the following:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: id

Filename: models/gallery_model.php

Line Number: 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.

A: 

not sure how to ask you a question without submitting an "answer"...

what's in line 48? which file is gallery_model.php? from the sounds of it, it could be an array key that hasn't been initialized or an issue in querying the database.

Jan Kuboschek
the whole file is gallery_model.php. Line 48 is 'busid'=> $bus_id['id'],$bus_id = $this->get_bus_id(); is what is getting the row id from one table. Then, I'm taking what is supposed to be returned from that function and placing it into a new array using 'busid'=>$bus_id['id'];
Jason Shultz
Just an FYI from someone who is also somewhat new, once you reach 50 rep points you have the ability to add a comment to questions/answers.
Jonathan Kuhn
as i thought. $bus_id['id'] has not been initialized. i'm guessing that you have PHP set up so that warnings are displayed? you may want to change that setting for your production environment. in order to prevent the notice, try setting $bus_id['id'] = 0; before you make your assignment.for more information and discussion, check out http://www.dmxzone.com/go?13811
Jan Kuboschek
jonathan kuhn: thanks for the tip. that's what i needed to know :)
Jan Kuboschek
A: 

The problem is that if the business is not found, your function is not returning anything. As a result, $bus_id has nothing in it (its not even an array). You should probably have your get_bus_id() function return false like so:

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }

then after you call get_bus_id() you can check if $bus_id == false and maybe return false to denote an error from do_upload()

Jonathan Kuhn
A: 

Ok, I have it working. This is the complete and working 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();

        /*
        TABLE STRUCTURE =============
            id, the row ID
            photoname
            thumb
            fullsize
            busid
            userid
        */



        $data = array(
            'id'        => 0 , // I GUESS IS AUTO_INCREMENT
            'photoname'    => '',
            'thumb'        => $this->gallery_path . '/thumbs/' . $upload['file_name'],
            'fullsize'    => $upload['full_path'],
            'busid'=> $bus_id['id'],
            'userid' => $this->tank_auth->get_user_id(),
        );

        // CHECK THE DATA CREATED FOR INSERT

        $this->db->insert('photos', $data);
    }

// Get Business ID from DB

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
              return $query->row_array();
            }
        }
Jason Shultz