Reputation: 21
I am trying to echo out the user level depending on whether the user level is either 1 or 5 based on SQL data results. Here:
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
So I need a sort of if
statement to select the user level from $rrows['id']
then if that selected data is 1 echo out "Network" and if it is "5" echo out "Administrator". How can this be done?
Upvotes: 1
Views: 95
Reputation: 327
I wouldn't do it with a if statement. I'd do it like that:
$levels = array(1 => "Network", 5 => "Administrator");
echo $levels[$rrows['user_level']];
That way, if you want to add other levels, you just add a value to the array and that's it.
Upvotes: 1
Reputation: 8268
I wonder how you got that far without an if statement but anyway, you also could do this right in you sql query. I always try to outsource as much logic to mysql as possible for a balanced load distribution.
SELECT level, IF(level = 1, 'Network', 'Administrator') as level FROM table
I think you can nest if statements to have more options.
Adapted from this answer: 'IF' in 'SELECT' statement - choose output value based on column values
Upvotes: 1
Reputation: 6369
Seems like you need something like that:
$user_levels = array('Network','role2','role3','role4','Administrator');
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $users_levels[(int)$rrows['id']-1]; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
The reason I am using -1 in the array is because the array is 0 based and your roles start with 1. If you need to use the 'user_level' simply replace the row with
<td><?php echo $users_levels[(int)$rrows['user_level']-1]; ?></td>
Hope this helps!
Upvotes: 2