在CodeIgniter中获取数据

时间:2019-03-09 17:23:15

标签: php mysql database codeigniter fetch

我需要帮助来从数据库中获取数据并在视图中显示它们。 我的数据库名称是目录和表 usernames ,其中包括:id,标题,正文,子句,用户名,平台,性别,年龄和created_at。 我使用foreach来显示数据库中的所有帖子,但是现在我想显示基于 platform 的特定帖子。 我该怎么办?

这是我用来显示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')中显示所有包含平台电报的行。 如前所述,我的数据库的结构如下:

  

目录(数据库名称)

     

用户名(表名)

     

-> id-> title-> body-> slug-> username-> platform-> gender-> age-> created_at

0 个答案:

没有答案