我坚持这个项目。我已经搞了一个星期了。我总是得到图像更新的错误。我希望你能帮助我。
以下是代码:
控制器:
function update_post(){
$this->load->model('membership_model');
$this->membership_model->update_post();
}
模型
function update_post(){
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
$post_id = $this->uri->segment(3);
$data['result'] = $this->db->query("SELECT * FROM post WHERE post_id=$post_id");
$this->load->view('template/member-header');
$this->load->view('edit_post',$data);
$this->load->view('template/footer');
}
else
{
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
//resize
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = true;
$config['width'] = 250;
$config['height'] = 200;
$config['new_image'] = './upload/'.$file_name;
$this->load->library('image_lib',$config);
$this->image_lib->resize();
//resize
$data = array(
'post_title' => htmlentities($this->input->post('title')),
'post_content' => htmlentities($this->input->post('content')),
'post_image' => $file_name
);
$this->db->update('post', $data);
$this->load->view('template/member-header');
$this->load->view('edit_success');
$this->load->view('template/footer');
}
}
查看
<?php
if(isset($error)){
echo "<span class='red'>".$error."</span>";
}
else{
}
?>
<?php
$post_id = $this->uri->segment(3);
// get single data on database
if ($result->num_rows() > 0){
$row = $result->row();
$image_path = base_url('upload/'.$row->post_image);
echo "<br /><br /><center><img src='$image_path' border='5'/></center>";
$title = $row->post_title;
$content = $row->post_content;
echo form_open_multipart('site/update_post/'.$post_id);
echo "<li><h4>Image:</h4> ".form_upload('userfile','','required')."</li>";
echo "<li><h4>Title:</h4> ".form_input('title',$title,'required')."</li>";
echo "<li><h4>Content:</h4> ".form_textarea('content',$content,'required')."</li>";
echo "<li>".form_submit('submit','Post').form_reset('reset','Reset')."</li>";
echo form_close();
}
?>
任何帮助都会非常感激。谢谢
答案 0 :(得分:0)
根据您的错误评论。请参阅:Multiple Results vs Single Result
单个结果的标准查询
$result = $this->db->query("YOUR QUERY");
$row = $result->row();
echo $row->post_title;
包含多个结果的标准查询
$result = $this->db->query("YOUR QUERY");
if ($result->num_rows() > 0)
{
foreach ($result->result() as $row)
{
echo $row->post_title;
echo $row->post_content;
}
}