Wednesday 15 April 2015

php - Codeigniter: showing post thumbnail with every post -



php - Codeigniter: showing post thumbnail with every post -

i trying setup demo blog , want every post have thumbnail. @ blog first: my demo blog. can create new blog post here creating new post , show them in blog. have gallery page can upload images, here: gallery page. want new post page have image upload alternative , should add together single image every post publish new post page so: example. how can that? have tried many times failed show image every post.

model post: class post extends ci_model { function get_posts($num=20, $start=0) { $this->db->select()->from('posts')->where('active',1)->order_by('date_added', 'desc')->limit(20,0); $query = $this->db->get(); homecoming $query->result_array(); } function get_posts_count() { $this->db->select('postid')->from('posts')->where('active', 1); $query = $this->db->get(); homecoming $query->num_rows(); } function get_post($postid) { $this->db->select()->from('posts')->where(array('active'=>1, 'postid'=> '$postid'))->order_by('date_added', 'desc'); $query = $this->db->get(); homecoming $query->first_row('array'); } function insert_post($data) { $this->db->insert('posts', $data); homecoming $this->db->insert_id(); } }

controller post: class posts extends ci_controller { function __construct() { parent::__construct(); $this->load->model('post'); $this->load->helper('form'); } function index($start=0) { $data['posts']=$this->post->get_posts(5, $start); $this->load->library('pagination'); $config['base_url'] = base_url() . 'posts/index/'; $config['total_rows'] = $this->post->get_posts_count(); $config['per_page'] = 5; $this->pagination->initialize($config); $data['pages'] = $this->pagination->create_links(); $this->load->view('header'); $this->load->view('post_index',$data); $this->load->view('footer'); } function post($postid) { $data['post']= $this->post->get_post($postid); $this->load->view('post',$data); } function new_post() { if ($_post) { $data=array( 'title'=> $_post['title'], 'post' => $_post['post'], 'active'=> 1 ); $this->post->insert_post($data); redirect(base_url().'posts/'); } else { $this->load->view('new_post'); } } }

index page view: blog posts shown:

<?php if (!isset($posts)) { ?> <p>there no post in database</p> <?php } else { foreach ($posts $row) { ?> <h2 class="title"><a class="link_title" href="<?php echo base_url()?>posts/post/<?echo $row['postid']?>"><?php echo $row['title']?></a><a class="edit" href="<?php echo base_url()?>posts/editpost/<?echo $row['postid']?>">edit</a> / <a class="delete" href="<?php echo base_url()?>posts/<?echo $row['postid']?>">delete</a></h2> <p><?php echo substr(strip_tags($row['post']),0,200) . ".."?></p> <p><a href="<?php echo base_url()?>/posts/post/<? echo $row['postid']?>">read more</a></p> <hr> <?php } } ?> <?php echo $pages; ?>

this new post page view want image upload alternative available:

<form action="<?php echo base_url()?>posts/new_post" method="post"> <p>title: <input name="title" type="text"></p> <p>description: <br><textarea name="post" cols="50" rows="30"></textarea></p> <p><input type="submit" value="add post"></p> </form>

i have codes upload image galley page. sharing if helps any.

gallery model: class gallery_model extends ci_model { var $gallery_path; var $gallery_path_url; function __construct() { parent::__construct(); $this->gallery_path = './images'; $this->gallery_path_url = base_url() . 'images/'; } function do_upload() { $config = array( 'allowed_types' => 'jpg|png|jpeg', 'upload_path' => $this->gallery_path ); $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' => 75 ); $this->load->library('image_lib', $config); $this->image_lib->resize(); } function get_images() { $files = scandir($this->gallery_path); $files = array_diff($files, array('.', '..', 'thumbs')); $images = array(); foreach ($files $file) { $images[] = array( 'url' => $this->gallery_path_url . $file, 'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file ); } homecoming $images; } }

gallery controller: class gallery extends ci_controller { function index() { $this->load->model('gallery_model'); if ($this->input->post('upload')) { $this->gallery_model->do_upload(); } $data['images'] = $this->gallery_model->get_images(); $this->load->view('header'); $this->load->view('gallery', $data); $this->load->view('footer'); } }

gallery view:

<div class="gallery"> <?php if (isset($images) && count($images)): foreach ($images $image): ?> <div class="thumbs"> <a href="<?php echo $image['url']; ?>"> <img src="<?php echo $image['thumb_url']; ?>" alt=""> </a> </div> <?php endforeach; else: ?> <div class="blank_gallery">please upload pictures here</div> <?php endif; ?> </div> <div class="upload"> <?php echo form_open_multipart('gallery'); echo form_upload('userfile'); echo form_submit('upload', 'upload'); echo form_close(); ?>

how can gallery page codes used new post page page every post has thumbnail .

i learning php. don't know know how figure out. please help me.

maybe didn't understand problem, can figure need plugin:

[http://jquery.malsup.com/form][1] [example @ : http://jquery.malsup.com/form/progress.html][2]

this help upload pic first edit/save article, because "gallery page" save , upload images , need link these 2 pages ajax image plugin.

php codeigniter

No comments:

Post a Comment