我有以下情况:如果用户发布了消息,则其他用户可以批准该消息,但不能批准作者,该作者应该只能编辑或删除它。
以下if语句应该适用于该语句,但它不起作用。
<td>
<?php $author = array(
$message->user_id == $this->session->userdata('id')
); ?>
<?php if($author): ?>
<?php echo anchor('admin/messages/edit/'.$message->id.'', 'Edit', 'class="btn btn-primary"'); ?>
<?php echo anchor('admin/messages/delete/'.$message->id.'', 'Delete', 'class="btn btn-danger"'); ?>
<?php endif; ?>
<?php if(!$author): ?>
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
<?php endif; ?>
</td>
下面的代码是我的CONTROLLER中的 approve METHOD,它应该在将数据传递给MODEL之前检查条件
public function approve($id)
{
$this->db->select('*');
$this->db->from('approval');
$this->db->join('messages', 'messages.id = approval.sms_id');
$query = $this->db->get();
$first_approval = $query->row('first_approval');
$second_approval = $query->row('second_approval');
$third_approval = $query->row('third_approval');
if ($first_approval == null) {
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$approval_data = array(
'approvals' => 'one'
);
$this->Message_model->some_approve($id, $approval_data);
$this->Message_model->approve($id, $data);
//Activity array
$data = array(
'resource_id' => '',
'type' => 'message',
'action' => 'approved',
'user_id' => $this->session->userdata('user_id'),
'message' => 'Message was approved'
);
//Insert Activity
$this->Activity_model->add($data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send');
//Redirect
redirect('admin/messages');
} elseif ($second_approval == null && $this->session->userdata('user_id') != $first_approval) {
$data = array(
'second_approval' => $this->session->userdata('user_id')
);
$approval_data = array(
'approvals' => 'two'
);
$this->Message_model->some_approve($id, $approval_data);
$this->Message_model->approve($id, $data);
//Activity array
$data = array(
'resource_id' => '',
'type' => 'message',
'action' => 'approved',
'user_id' => $this->session->userdata('user_id'),
'message' => 'Message was approved'
);
//Insert Activity
$this->Activity_model->add($data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send');
//Redirect
redirect('admin/messages');
} elseif ($third_approval == null && $this->session->userdata('user_id') != $second_approval && $this->session->userdata('user_id') != $first_approval) {
$data = array(
'third_approval' => $this->session->userdata('user_id')
);
$approval_data = array(
'status' => 'Approved',
'approvals' => 'three'
);
$this->Message_model->approve($id, $data);
$this->Message_model->some_approve($id, $approval_data);
//Activity array
$data = array(
'resource_id' => '',
'type' => 'message',
'action' => 'approved',
'user_id' => $this->session->userdata('user_id'),
'message' => 'Message was approved'
);
//Insert Activity
$this->Activity_model->add($data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send and the message has passed');
//Redirect
redirect('admin/messages');
} elseif ($third_approval == $this->session->userdata('user_id') || $this->session->userdata('user_id') == $second_approval || $this->session->userdata('user_id') == $first_approval) {
//isset Message
$this->session->set_flashdata('success', 'You already have send an approval');
//Redirect
redirect('admin/messages');
} else {
$approval_data = array(
'status' => 'Approved',
'approvals' => 'three'
);
$this->Message_model->some_approve($id, $approval_data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send and the message has already been send');
//Redirect
redirect('admin/messages');
}
}
以下方法正在运作
public function approve($id, $data) {
$this->db->where('sms_id', $id);
$this->db->update('approval',$data);
}
public function some_approve($id, $approval_data)
{
$this->db->where('id', $id);
$this->db->update('messages',$approval_data);
}
我的控制器和按钮上的if条件是我需要帮助的条件,因为它们没有显示任何错误,但是它们不能正常工作
答案 0 :(得分:0)
首先,你的if条件是错误的:
它应该是这样的:
<?php if($message->user_id && $message->user_id == $this->session->userdata('id')): ?>
HTML Data
<?php else:?>
HTML Data