I am not sure if my questions is worded correctly so please feel free to tell me to change it.
我不确定我的问题措辞是否正确,所以请随时告诉我改变它。
I am allowing for multiple images to be uploaded at once and then inserting text into the database as an html img.
我允许一次上传多个图像,然后将文本作为html img插入数据库。
Here is my code:
这是我的代码:
if ( ! $this->upload->do_upload('post_image'))
{
$this->session->set_flashdata('post_message', $this->upload->display_errors());
$this->session->set_flashdata('post_message_class', 'alert-danger');
redirect('/user/profile/'.$identity, 'refresh');
}
else
{
$uploaded = $this->upload->data();
// insert pos
if(isset($uploaded['file_name']) && $uploaded['file_name'] == 0){
$post_text = '<img src="/uploads/images/'.$uploaded['file_name'].'" width="251px" alt="'.$uploaded['raw_name'].'" /><br>'.$this->input->post('post_text');
}
else
{
foreach ($uploaded as $images){
$post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');
}
}
$query = $this->user_model->insert_user_posts($this->input->post('poster_id'), $this->input->post('profile_id'), $this->input->post('post_type'), $post_text);
$this->session->set_flashdata('post_message', 'Image has been posted!');
$this->session->set_flashdata('post_message_class', 'alert-success');
redirect('/user/profile/'.$identity, 'refresh');
}
EDIT: Here is the code to the model:
编辑:这是模型的代码:
public function insert_user_posts($poster_id, $profile_id, $post_type, $post_text)
{
// Users table.
$data = array(
'poster_id' => $poster_id,
'profile_id' => $profile_id,
'post_type' => $post_type,
'post_text' => $post_text,
'datetime' => time()
);
$this->db->insert($this->tables['users_posts'], $data);
if($this->db->affected_rows() > 0)
{
$this->set_message('upload_successful');
return TRUE;
}
else
{
return FALSE;
}
}
Also, I am using this: https://github.com/avenirer/MY_Upload
另外,我使用这个:https://github.com/avenirer/MY_Upload
If I echo
or var_dump
the $post_text
it will show the data for both images, but it is only inserting the first image. What am I doing wrong here?
如果我回显或var_dump $ post_text它将显示两个图像的数据,但它只插入第一个图像。我在这做错了什么?
1 个解决方案
#1
1
If you want to append the images together in the same $post_text
variable you have to change this line: $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');
如果你想在同一个$ post_text变量中将图像附加在一起,你必须改变这一行:$ post_text ='
'。$ this-> input-> post('post_text');
by changing the assignment operator =
to the concatenation operator .=
like so:
通过将赋值运算符=更改为连接运算符。= like like:
$post_text .= ...
$ post_text。= ...
Instead of overwriting the $post_text
variable each time you will instead add each image to the end of it. I can make a guess that your final foreach loop might look something like this:
每次你将每个图像添加到它的末尾时,而不是覆盖$ post_text变量。我可以猜测你的最终foreach循环可能看起来像这样:
$post_text = ""; foreach ($uploaded as $images){ $post_text .= '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" />'; } $post_text .= '<br>'.$this->input->post('post_text');
$ post_text =“”; foreach($上传为$ images){$ post_text。=''; } $ post_text。='
'。$ this-> input-> post('post_text');
This would create a string that contains all the images in a row followed at the end by '<br>'.$this->input->post('post_text');
. Make note that you must first instantiate the $post_text
variable to something (here I have set it to an empty string) in order to use the concatenation operator.
这将创建一个字符串,其中包含一行中的所有图像,最后是“
”。$ this-> input-> post('post_text');.请注意,您必须首先将$ post_text变量实例化为某些内容(此处我将其设置为空字符串)才能使用连接运算符。
#1
1
If you want to append the images together in the same $post_text
variable you have to change this line: $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');
如果你想在同一个$ post_text变量中将图像附加在一起,你必须改变这一行:$ post_text ='
'。$ this-> input-> post('post_text');
by changing the assignment operator =
to the concatenation operator .=
like so:
通过将赋值运算符=更改为连接运算符。= like like:
$post_text .= ...
$ post_text。= ...
Instead of overwriting the $post_text
variable each time you will instead add each image to the end of it. I can make a guess that your final foreach loop might look something like this:
每次你将每个图像添加到它的末尾时,而不是覆盖$ post_text变量。我可以猜测你的最终foreach循环可能看起来像这样:
$post_text = ""; foreach ($uploaded as $images){ $post_text .= '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" />'; } $post_text .= '<br>'.$this->input->post('post_text');
$ post_text =“”; foreach($上传为$ images){$ post_text。=''; } $ post_text。='
'。$ this-> input-> post('post_text');
This would create a string that contains all the images in a row followed at the end by '<br>'.$this->input->post('post_text');
. Make note that you must first instantiate the $post_text
variable to something (here I have set it to an empty string) in order to use the concatenation operator.
这将创建一个字符串,其中包含一行中的所有图像,最后是“
”。$ this-> input-> post('post_text');.请注意,您必须首先将$ post_text变量实例化为某些内容(此处我将其设置为空字符串)才能使用连接运算符。