Php 试图将变量从一个函数传递到另一个函数,以放入同一模型内的数组中
好的,听起来真的很混乱。我想做的就是这样。我有一个将照片上传/调整大小到服务器的功能。它将路径存储在数据库中。我需要将企业id附加到照片行 以下是我目前掌握的情况:Php 试图将变量从一个函数传递到另一个函数,以放入同一模型内的数组中,php,codeigniter,Php,Codeigniter,好的,听起来真的很混乱。我想做的就是这样。我有一个将照片上传/调整大小到服务器的功能。它将路径存储在数据库中。我需要将企业id附加到照片行 以下是我目前掌握的情况: function get_bus_id() { $userid = $this->tank_auth->get_user_id(); $this->db->select('b.id'); $this->db->from ('business AS b'); $thi
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
我尝试了各种方法来获得价值,但我有限的知识一直在阻碍我。非常感谢您的帮助。不知道如何在不提交“答案”的情况下向您提问
第48行是什么?哪个文件是gallery_model.php?从声音上看,它可能是一个尚未初始化的数组键,也可能是查询数据库时出现的问题。问题是,如果找不到业务,则函数不会返回任何内容。因此,
$bus_id
中没有任何内容(甚至不是数组)。您可能应该让get\u bus\u id()
函数返回false,如下所示:
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
然后,在调用
get_bus_id()
之后,您可以检查$bus_id==false
并可能返回false以表示来自do_upload()
的错误。好的,我已经让它工作了。这是完整的工作代码:
/* 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);
}
//从数据库获取业务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();
}
}
整个文件是gallery_model.php。第48行是'busid'=>$bus_id['id'],$bus_id=$this->get_bus_id();是从一个表中获取行id的内容。然后,我从该函数中获取应该返回的内容,并使用'busid'=>$bus_id['id']将其放入一个新数组中;我只是想告诉你一个新来的人,一旦你达到50个代表点,你就有能力在问题/答案中添加评论。正如我所想的那样$总线id['id']尚未初始化。我猜您已经设置了PHP,以便显示警告?您可能需要为生产环境更改该设置。为了防止通知,请尝试将$bus_id['id']=0;在你做作业之前。有关更多信息和讨论,请查看乔纳森·库恩:谢谢你的提示。这就是我需要知道的:)get_bus_id()函数的其余部分是什么?在我看来,查询没有返回任何结果。另外,您是否验证了get_user_id()函数是否正常工作?