dpbvpgvrhwxen3222 2013-05-21 09:44
浏览 36
已采纳

如何在Codeigniter中为我的公司表添加徽标路径?

I have an form for adding companies to my database. I've added a field called Logo. I want to use this field so the user can upload an Logo for their company. I am using the Codeigniter upload class in my form.

My company table looks like this:

Companies
---------
id
companyname
address
postalcode
country
email
website
logo

My form looks liek this:

<tr>
<td><?= form_label('Bedrijfsnaam:');?></td>
<td><?= form_input('Bedrijfsnaam');?><small> (Spaties niet toegestaan)</small></td>
</tr>

<tr>
<td><?= form_label('Adres:');?></td>
<td><?= form_input('Adres');?></td>
</tr>

<tr>
<td><?= form_label('Postcode:');?></td>
<td><?= form_input('Postcode');?></td>
</tr>

<tr>
<td><?= form_label('Plaats:');?></td>
<td><?= form_input('Plaats');?></td>
</tr>

<tr>
<td><?= form_label('Telefoonnummer:');?></td>
<td><?= form_input('Telefoonnummer');?></td>
</tr>

<tr>
<td><?= form_label('Website:');?></td>
<td><?= form_input('Website');?></td>
</tr>

<tr>
<td><?= form_label('Email:');?></td>
<td><?= form_input('Email');?></td>
</tr>

<tr>
<td><?= form_label('Profiel:');?></td>
<td><?= form_textarea('Profiel');?></td>
</tr>

<tr>
<td><?= form_label('Categorieen:'); ?></td>
<td><?= form_dropdown('categorieen', $opties); ?></td>
</tr>

<tr>
<td><?= form_label('Logo:'); ?></td>
<td><input type="file" name="logo" size="20" /></td>
</tr>

<tr>
<td><?= form_submit('submit', 'Opslaan');?> <?= form_reset('reset', 'Reset');?></td>
</tr>

My model looks like this:

    $data1 = array( 
        'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'), 
        'Postcode' => $this->input->post('Postcode'), 
        'Plaats' => $this->input->post('Plaats'), 
        'Telefoonnummer' => $this->input->post('Telefoonnummer'), 
        'Email' => $this->input->post('Email'), 
        'Website' => $this->input->post('Website'), 
        'Profiel' => $this->input->post('Profiel'), 
        'Adres' => $this->input->post('Adres'), 
        'logo' => $this->input->post('logo') 
    ); 
    $this->db->insert('bedrijven',$data1); 

I know this won't work but I can't figure out how to do this.

I have another uploadform but that's only for uploading images, not for the logo.

What's the best way to do this using the upload class?


EDIT:

I get an empty field when I add a new company.

My controller:

    function addbedrijven()
    {
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '';
        $config['max_height']  = '';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);

        if($this->upload->do_upload('logo')){

        $logo_image_data = $this->upload->data();

        }
        $this->members_model->addbedrijf();
        redirect('members/index');
    }

My Model:

    function addbedrijf() 
    { 
        $logo_image_data = $this->upload->data();

        $data1 = array( 
            'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'), 
            'Postcode' => $this->input->post('Postcode'), 
            'Plaats' => $this->input->post('Plaats'), 
            'Telefoonnummer' => $this->input->post('Telefoonnummer'), 
            'Email' => $this->input->post('Email'), 
            'Website' => $this->input->post('Website'), 
            'Profiel' => $this->input->post('Profiel'), 
            'Adres' => $this->input->post('Adres'), 
            'logo' => $logo_image_data['full_path'] 
        ); 
        $this->db->insert('bedrijven',$data1); 
    }
  • 写回答

1条回答 默认 最新

  • douwen3973 2013-05-21 10:15
    关注

    you can use the codeigniter upload class within the same form

          $config_logo_image = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => UPLOAD_PATH,
            'max_size' => 2000,
            );
        $this->load->library('upload', $config_logo_image );                    
        if($this->upload->do_upload('logo_image')){
    
        $logo_image_data = $this->upload->data();
    
        }
    

    And through this data in the your $data array

        $data1 = array( 
        'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'), 
        'Postcode' => $this->input->post('Postcode'), 
        'Plaats' => $this->input->post('Plaats'), 
        'Telefoonnummer' => $this->input->post('Telefoonnummer'), 
        'Email' => $this->input->post('Email'), 
        'Website' => $this->input->post('Website'), 
        'Profiel' => $this->input->post('Profiel'), 
        'Adres' => $this->input->post('Adres'), 
        'logo' => $logo_image_data ['full_path']  // it is full path of image 
    ); 
    $this->db->insert('bedrijven',$data1); 
    

    'logo' =>$logo_image_data ['file_name'] //only file name

    Hope it makes sense

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法