CodeIgniter Forums
Checking if a user has submitted an approval with if statements - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Checking if a user has submitted an approval with if statements (/showthread.php?tid=66501)



Checking if a user has submitted an approval with if statements - Bright - 10-27-2016

Is a user has composed a message, other users be able to should approve the message but the message Author should only delete and edit not approve. The below if statement is supposed to work for that but its not 
Code:
    <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>
The code below is the *approve* METHOD in my CONTROLLER which is supposed to check the **condition** before passing data to the MODEL 

Code:
    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');

    }

}
The METHODS below are working 
Code:
    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);
    }

The if conditions in my controller and on the buttons are the ones which I need help on because the are not showing any error but they are not working as they should


RE: Checking if a user has submitted an approval with if statements - InsiteFX - 10-27-2016

Please use code tags to wrap your code in, the way it is now makes it very hard for someone to read it.

[ code]
// your code here!
[ /code]

Please remove the space in the code tags.


RE: Checking if a user has submitted an approval with if statements - Bright - 10-28-2016

(10-27-2016, 11:28 AM)InsiteFX Wrote: Please use code tags to wrap your code in, the way it is now makes it very hard for someone to read it.

[ code]
// your code here!
[ /code]

Please remove the space in the code tags.

I have edited, I think it's now clear


RE: Checking if a user has submitted an approval with if statements - Wouter60 - 10-28-2016

Don't use the <?php opening tag right after you closed a previous code statement with ?>
Just write the statements inside one opening and one closing tag until you switch to html in your view.

Example:
PHP Code:
<div>
<?
php
$message 
'PHP is great!';
echo 
$message;
$message .= 'And CodeIgniter is great too!';
echo 
$message;
?>
</div>