尝试将变量从1个函数传递给另一个函数以在同一模型中放入数组

时间:2010-05-24 19:08:51

标签: php codeigniter

好的,这听起来真的很混乱。我想要做的就是这个。我有一个功能可以将照片上传/调整大小到服务器。它将路径存储在DB中。我需要将业务的ID附加到照片行。

这是我到目前为止所拥有的:

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

这就是业务的重要性。然后,我的上传功能如下:

/* 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);
    }

我遇到的问题如下:

遇到PHP错误

严重性:注意

消息:未定义索引:id

文件名:models / gallery_model.php

行号:48

我已经尝试了各种各样的方法来获得价值,但我的有限知识不断阻碍。任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:0)

不确定如何在不提交“回答”的情况下问你一个问题...

第48行是什么?哪个文件是gallery_model.php?从它的声音来看,它可能是一个尚未初始化的数组键或查询数据库时的问题。

答案 1 :(得分:0)

问题是,如果找不到业务,您的功能不会返回任何内容。结果,$bus_id中没有任何东西(它甚至不是数组)。您可能应该让get_bus_id()函数返回false,如下所示:

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

然后在您致电get_bus_id()后,您可以检查$bus_id == false是否可能返回false以表示来自do_upload()的错误

答案 2 :(得分:0)

好的,我有它的工作。这是完整且有效的代码:

/* 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);
    }

//从DB获取商家ID

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