1

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?

8
  • where do you want to display that? Commented Jan 25, 2014 at 3:17
  • Just because the data's coming from mysql doesn' tmean you can't use a normal if($row['id'] == 'something') { ...} type construct... Commented Jan 25, 2014 at 3:17
  • 1
    First, you did all this but can't do an if statement? And second, probly easier to use a switch-case. What's the <td><?php ?></td> for? No need for that empty PHP snippet Commented Jan 25, 2014 at 3:17
  • echo ($rrows['id']==1)?"Network":"Administrator"; Commented Jan 25, 2014 at 3:19
  • @yani want it to be displayed in the empty <td><?php ?></td> part. Commented Jan 25, 2014 at 3:20

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

6 Comments

Just tired this one, didn't show anything up. Thank you for the suggestion though!:)
No reason it shouldn't work, did you replace 'id' with 'user_level'?
@Yani Nope, i just tried it again and it says Parse error: syntax error, unexpected 'while' (T_WHILE) in {path}
seems like you have an error before the code you posted...maybe forgot to put ';', or close an if or a loop...
Yeah i found the error, but still nothing is being displayed; brings up no errors just nothing being outputted.
|
1

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

Comments

1

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.

7 Comments

Just tried this, didn't show anything up either! Hmm thanks for the suggestion though!
Just as my colleague Yani said, there's no reason this shouldn't work. What is inside $$rows['id']? Do you have another field in your database, like user_level?
$rrows['id' is the unique ID for each user, then within that database(table) there is fields including name etc, then user_level which is a number, then that number i want changing to a word depending on the number queried.
OK. Can you confirm user_level is either 1 or 5 ? If so, you should just echo $levels[$rrows['user_level']] and that would do it.
@StephaneMombuleu - You solved it! That last comment fixed it, now works as I'd like it. Thank you!:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.