Welcome Guest, Not a member yet? Register   Sign In
How to use case statement instead of if statement in codeigniter
#1

I have a code below which seems not to be working so I would want to try case statement instead
Code:
 <td>
      <?php $is_author = $message->user_id == $this->session->userdata('id'); ?>
      <?php if($is_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 else: ?>
      <?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
      <?php endif; ?>
  </td>


I need help to solve the if statement or change it to case statement if it can work pliz.
Reply
#2

You would get more help by specifying what the problem is.

CodeIgniter is written in PHP, so look here for a switch case control structure.
A good decision is based on knowledge and not on numbers. - Plato

Reply
#3

(10-28-2016, 04:17 AM)salain Wrote: You would get more help by specifying what the problem is.

CodeIgniter is written in PHP, so look here for  a switch case control structure.

I'm trying to show edit and delete buttons if the message->user_id is the same as the user logged in (which means he's the author) and approve button if the logged in user is not the author of the message
Reply
#4

Hey,

Still, not saying what the problem is ?
What do you expect? and what do you get ?

If you always get the the code in the else statement when you are sure the id's  are the same then it maybe because one is not set properly
print or echo the ids to see if they are set correctly.

The way you get the id from the session is depreciated Look here

Also you don't need the quote a the end in your anchor
PHP Code:
echo anchor('admin/messages/edit/'.$message->id'Edit''class="btn btn-primary"'); 

I hope this helps
A good decision is based on knowledge and not on numbers. - Plato

Reply
#5

Using switch:

PHP Code:
<td>
<?
php 
  $role 
= ($message->user_id == $this->session->id) ? 'author' 'visitor';
 
 switch ($role) {
    case 
'author' :
 
     echo anchor('admin/messages/edit/'.$message->id'Edit''class="btn btn-primary"');
     
 echo anchor('admin/messages/delete/'.$message->id'Delete''class="btn btn-danger"');
 
     break;
    case 
'visitor' :
     
 echo anchor('admin/messages/approve/'.$message->id'Approve''class="btn btn-success"');
 
     break;
    default:
     
 //what to do if the role is neither 'author' nor 'visitor'?
 
 }
?>
</td> 
Reply
#6

(10-28-2016, 05:43 AM)Wouter60 Wrote: Using switch:

PHP Code:
<td>
<?
php 
  $role 
= ($message->user_id == $this->session->id) ? 'author' 'visitor';
 
 switch ($role) {
    case 
'author' :
 
     echo anchor('admin/messages/edit/'.$message->id'Edit''class="btn btn-primary"');
     
 echo anchor('admin/messages/delete/'.$message->id'Delete''class="btn btn-danger"');
 
     break;
    case 
'visitor' :
     
 echo anchor('admin/messages/approve/'.$message->id'Approve''class="btn btn-success"');
 
     break;
    default:
     
 //what to do if the role is neither 'author' nor 'visitor'?
 
 }
?>
</td> 
thnks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB