I need help fetching data from database and display them in view.
My DB name is directory and table usernames that includes:id,title,body,slug,username,platform,gender,age and created_at.
I used foreach to display all posts from database, but i want to display now a specific post based on platform.
How can i do it?
This is the code i use to display all usernames from the database ordered by 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>
This is my model:
<?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');
}
}
Model:
<?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);
}
}
Everything is working fine, but i don’t know really how to fetch those data.
I need that in this specific page (‘telegram.php’), show all rows that includes Telegram from platform.
As mentioned before, my database is structured like this:
directory(DB Name)
usernames(table name)
->id->title->body->slug->user
2
Answers
Add the method, to your Model and call it to fetch the corresponding data in the corresponding method in your controller.
It seems to be missing its constructor $this->load->model(‘post_model’);