首页 > 解决方案 > 使用 CodeIgniter 从数据库中获取数据

问题描述

我需要帮助从数据库中获取数据并在视图中显示它们。我的数据库名称是目录和表用户名,包括:id、title、body、slug、username、platform、gender、age 和 created_at。我使用 foreach 显示数据库中的所有帖子,但我现在想显示基于平台的特定帖子。我该怎么做?

这是我用来显示数据库中按 id 排序的所有用户名的代码:

<div class="col-md-6 mb-4">
    <div class="card">

    <div class="card-body" style="height: 130px;">
      <h5 class="card-title"><?php echo $username_data['title']; ?></h5>
      <p class="card-text"><?php echo word_limiter($username_data['body'], 24); ?></p>
  </div>
    <div class="card-footer text-muted">
        <div class="row">
            <div class="col-6">
                <small>Created at: <?php echo $username_data['created_at'];?></small><br> 
          <strong>Is <?php echo $username_data['gender'];?></strong>
            </div>
            <div class="col-6 text-right">
                <a href="<?php echo site_url('usernames/' . $username_data['slug']); ?>" class="btn btn-primary text-right">View full message</a>
            </div>
        </div>
  </div>
</div>
</div>

这是我的模型:

<?php
class Usernames extends CI_Controller{
    public function index(){
        $data['title'] = 'Lastest Posts';

        $data['posts'] = $this->post_model->get_usernames();

        $this->load->view('templates/header');
        $this->load->view('usernames/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($slug = NULL){ 
        $data['post'] = $this->post_model->get_usernames($slug);
        $query = $this->db->get('usernames');

        if(empty($data['post'])){
            show_404();
        }

        $data['title'] = $data['post']['title'];

        $this->load->view('templates/header');
        $this->load->view('usernames/view', $data);
        $this->load->view('templates/footer');

    }

    public function create(){
        $data['title'] = 'Create Post';

        $this->form_validation->set_rules('title','Title','required');
        $this->form_validation->set_rules('body','Message','required');
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('age','Age','required');
        $this->form_validation->set_rules('gender','Gender','required');
        $this->form_validation->set_rules('platform','Platform','required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('usernames/create', $data);
            $this->load->view('templates/footer');
        } else {
            $this->post_model->create_username();
            redirect('usernames');
        }   
    }
    public function snapchat(){
        $data['title'] = 'Lastest Posts';

        $data['posts'] = $this->post_model->get_usernames();



        $this->load->view('templates/header');
        $this->load->view('usernames/snapchat', $data);
        $this->load->view('templates/footer');
    }
}

模型:

<?php
class Post_model extends CI_Model{
    public function __construct(){
        $this->load->database();
    }
    public function get_usernames($slug = FALSE){
        if($slug === FALSE){
            $this->db->order_by('usernames.id', 'DESC');
            $query = $this->db->get('usernames');
            return $query->result_array();
        }

        $query = $this->db->get_where('usernames', array('slug' => $slug));
        return $query->row_array();
    }

    public function create_username(){
        $slug_link = substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes(16))), 0, 16);
        $slug = url_title($this->input->post('title').'-'.$slug_link);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'platform' => $this->input->post('platform'),
            'age' => $this->input->post('age'),
            'gender' => $this->input->post('gender'),
            'username' => $this->input->post('username')
        );

        return $this->db->insert('usernames', $data);
    }
}

一切正常,但我真的不知道如何获取这些数据。我需要在这个特定页面('telegram.php')中显示所有包含来自平台的 Telegram 的行。如前所述,我的数据库结构如下:

目录(数据库名称)

用户名(表名)

->id->title->body->slug->user

标签: phpcodeigniterfetch

解决方案


它似乎缺少它的构造函数 $this->load->model('post_model');


推荐阅读