开发者

Trying to pass variable from 1 Function to Another to Put in Array within same Model

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: Notic开发者_运维知识库e

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.


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.


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()


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();
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜