php - How to upload an image on CodeIgniter and display it on another page -


i still learning codeigniter framework , completed news application tutorial, lets create news item title , text story. how can user able upload image news item, , display each image when viewing corresponding news item.

ps. sorry if made mistakes, first question

create.php

<h2>create news item</h2>  <?php echo validation_errors(); ?>  <?php echo form_open('news/create') ?>  <?php echo form_open_multipart('upload/do_upload');?>  <input type="file" name="userfile" size="20" />      <label for="title">title</label>     <input type="input" name="title" /><br />      <label for="text">text</label>     <textarea name="text"></textarea><br />      <input style="margin-left: 18%; margin-right: 20%;" type="submit" name="submit" value="publish" />  </form> 

news_model.php

    <?php class news_model extends ci_model {      public function __construct()     {         $this->load->database();     }  public function get_news($slug = false) {     if ($slug === false)     {         $this->db->order_by("id", "desc");         $query = $this->db->get('news');           return $query->result_array();     }      $query = $this->db->get_where('news', array('slug' => $slug));     return $query->row_array(); } public function set_news() {     $this->load->helper('url');      $slug = url_title($this->input->post('title'), 'dash', true);      $data = array(         'title' => $this->input->post('title'),         'slug' => $slug,         'text' => $this->input->post('text')     );      return $this->db->insert('news', $data); } } 

news.php -controller

<?php class news extends ci_controller {      public function __construct()     {         parent::__construct();         $this->load->model('news_model');     }      public function index(){         $data['news'] = $this->news_model->get_news();         $data['title'] = 'news archive';         $this->load->view('templates/header', $data);         $this->load->view('templates/rightpane', $data);         $this->load->view('news/index', $data);         $this->load->view('templates/footer');     }   public function view($slug) {     $data['news_item'] = $this->news_model->get_news($slug);              if (empty($data['news_item']))     {         show_404();     }      $data['title'] = $data['news_item']['title'];      $this->load->view('templates/header', $data);     $this->load->view('templates/rightpane', $data);     $this->load->view('news/view', $data);     $this->load->view('templates/footer'); } public function create() {     $this->load->helper('form');     $this->load->library('form_validation');      $data['title'] = 'create news item';      $this->form_validation->set_rules('title', 'title', 'required');     $this->form_validation->set_rules('text', 'text', 'required');      if ($this->form_validation->run() === false)     {         $this->load->view('templates/header', $data);         $this->load->view('templates/rightpane', $data);         $this->load->view('news/create');         $this->load->view('templates/footer');      }     else     {         $this->news_model->set_news();         $this->load->view('news/success');     } } } 

view.php

<?php echo '<h2>'.$news_item['title'].'</h2>'; echo $news_item['text'];` 

index.php

<?php foreach ($news $news_item): ?>     <div style="margin-left: 18%; margin-right: 20%;">     <h2 ><?php echo $news_item['title'] ?></h2>     <div id="main" >         <?php echo $news_item['text'] ?>     </div>     <p ><a href="news/<?php echo $news_item['slug'] ?>">view article</a></p>     </div> <?php endforeach ?> 

take @ upload class docs :

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

hint : you'll save file server , save url in database, can display url inside img tag.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -