Im trying to do so that only the author of the post can edit and delete it from the list where it is shown.
this is my view:
<?php if (($this->session->userdata('logged_in')==TRUE)) { ?>
<a href="<?php echo site_url("edit/$data[id_post]")?>" type="button">edit</a>
<a href="<?php echo site_url("delete/$data[id_post]")?>" type="button">delete</a>
<?php } ?>
The above code makes sure that only logged in users can actually edit the post, however they can edit all posts, and not only their own. In my /create i have made a hidden field, so that my posts are saved with the id of the author, so that relationship works just fine. However my question is, how do i make sure that authors only can edit their own posts?
Thanks!
Update:
Here is the Contoller
public function index()
{
if (($this->session->userdata('id')!=NULL))
{
$data['view'] = $this->home_model->list_data();
$created_by_sql = $this->db->query('SELECT id FROM user JOIN post ON user.id=post.id_user');
if ($created_by_sql->num_rows() > 0)
{
foreach ($created_by_sql ->result() as $row)
{
$user_id = $row->id;
}
}
$data["user_id"] = $user_id;
$this->load->view('home_table',$data);
Here is model
public function list_data()
{
$this->db->select('*');
$this->db->from('post as p');
$this->db->join('user as u', 'p.id_user = u.id');
$this->db->order_by("p.id_post", "desc");
$query = $this->db->get();
return $query->result_array();
}
And this is my View
<?php foreach($view as $data): ?>
<?php if (!empty($data['source'])) { ?>
<div class="sumber">anda<?php echo $data['source'];?></div>
<?php } ?>
<?php if ($this->session->userdata('logged_in')==TRUE AND $user_id == $this->session->userdata('id')) { ?>
<a href="<?php echo site_url("edit/$data[id_post]")?>" type="button">edit</a>
<a href="<?php echo site_url("delete/$data[id_post]")?>" type="button">delete</a>
<?php } else { echo 'id user not same with iduser_Login';} ?>`
<?php endforeach; ?>