I’ve got this project that I’m working on in, where a user can register and be assigned an ID. That user then has the ability to post blogs or comment, these blogs or comments get assigned an authorID same as the users userID.
I need a way to get their name posted below these blogs or messages instead of their ID-which it currently does.
My blog controller handles the blog posting (commenting is similar) with the exception of a add blog function:
function blogToevoegen() {
$this->form_validation->set_rules('blogtitel', 'blogtitel', 'min_length[3]|required|xss_clean|callback_username_check');
$this->form_validation->set_rules('blogtekst', 'blogtekst', 'min_length[2]|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('view_page', $this->view_data);
} else {
$data = array(
'id' => $this->input->post('id'),
'blogtitel' => $this->input->post('blogtitel'),
'blogtekst' => $this->input->post('blogtekst'),
'auteur_id' => $this->session->userdata('gebruiker_id'),
);
$this->db->set('datum', 'NOW()', FALSE);
$this->db->insert('blogs', $data);
redirect('blog/eigenblogs');
}
}
A function that handles the displaying of the blog post (it also gets the comments linked to a blog)
function comments() {
$this->db->where('id', $this->uri->segment(3));
$this->db->select('blogtitel, blogtekst, auteur_id, datum');
$data['query2'] = $this->db->get('blogs');
$this->db->where('boodschap_id', $this->uri->segment(3));
$data['query'] = $this->db->get('comments');
$data ["titel"] = "PXL - Comment";
$data ["pagina"] = "content_comment";
$this->load->view("view_page", $data);
}
The view code that displays these blogs (left the code for the comments out)
<?php foreach ($query2->result() as $rij): ?>
<div class="post">
<h2><?= $rij->blogtitel ?></h2>
<p><?= $rij->blogtekst ?></p>
<p class="meta">Posted by <a href="#"><?= $rij->auteur_id ?></a> on <?= $rij->datum ?>
</p>
</div>
<?php endforeach; ?>
<hr>
I need a way to change the auteur_id to a name (made out of a combination the first + last name).
What you need here is called SQL Join. Find more about it in the CI manual:
Here is an example of what you need:
This will get you all the blog posts and would query for the user in the users table based on the author_id.