Codeigniter:显示每个帖子的缩略图


Codeigniter: showing post thumbnail with every 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();

        return $query->result_array();
    }
    function get_posts_count() {
            $this->db->select('postID')->from('posts')->where('active', 1);
            $query = $this->db->get();
            return $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();
            return $query->first_row('array');
        }
    function insert_post($data) {
            $this->db->insert('posts', $data);
            return $this->db->insert_id();
        }
 }

岗位负责人:

 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');
        }
    }
 }

索引页面视图:显示所有博客文章的位置:

    <?php 
        if (!isset($posts)) {
     ?>
     <p>there is currently no post in the database</p>
     <?php 
    } else {
        foreach ($posts as $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; ?>

这是一个新的后页视图,我想在那里可以使用图像上传选项:

  <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>

我也有一些代码上传图片到厨房页面。如果有帮助的话,我会分享这个。

画廊模型:

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 as $file) {
                $images[] = array(
                    'url' => $this->gallery_path_url . $file,
                    'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
                    );
            }
            return $images;
        }
  }

画廊控制器:

 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');
    }
 }

画廊视图:

  <div class="gallery">
<?php if (isset($images) && count($images)): 
        foreach ($images as $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 some 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();
?>

如何将此库页面代码与新文章页面页面一起使用,以便每个文章都有缩略图。

我正在学习php。不知道该怎么想。请帮帮我。

也许我不理解你的问题,但我可以认为你需要这个插件:

[http://jquery.malsup.com/form][1]
[Example at : http://jquery.malsup.com/form/progress.html][2]

这将帮助你先上传图片,然后编辑/保存文章,因为你的"图库页面"只会保存和上传图像,你需要用这个ajax图像插件链接这两个页面。