1

I wasn't sure how to title my question, I'm basically asking if anyone knows a better way to approach this. I'm using this function below to show which option is pulled from the DB, I'm wondering if there is a more compact way or better way to do this other than elseif each ID

function access_option($access)
{
    if ($access == 0)
    {
        echo '<option value="0" selected>Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 1)
    {
        echo '<option value="0">Member</option>
        <option value="1" selected>Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 2)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2" selected>Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 3)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3" selected>Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 4)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4" selected>Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 5)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5" selected>Senior Administrator</option>';
    }
}

Is this the best way to do it?

3 Answers 3

4

You can do it with less repetition using ternary operators:

function access_option($access)
{
    echo '<option value="0"'.($access == 0 ? ' selected' : '').'>Member</option>';
    echo '<option value="1"'.($access == 1 ? ' selected' : '').'>Streamer</option>';
    echo '<option value="2"'.($access == 2 ? ' selected' : '').'>Moderator</option>';
    echo '<option value="3"'.($access == 3 ? ' selected' : '').'>Manager</option>';
    echo '<option value="4"'.($access == 4 ? ' selected' : '').'>Administrator</option>';
    echo '<option value="5"'.($access == 5 ? ' selected' : '').'>Senior Administrator</option>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's helpful and what I was looking for
Glad I could help!
3

You can loop over the values, which are held in an array, and set the selected value dynamically. This method makes it easy to add/remove options in the future by adjusting the array -

function access_option($access)
{
    $options = ["Member", "Streamer", "Moderator", "Manager", "Administrator", "Senior Administrator"];
    foreach($options as $key=>$value)
    {
        echo '<option value="'.$key.'"'.($key==$access ? ' selected' : '').'>'.$value.'</option>';
    }
}

1 Comment

i like this solution
0

Ternary operators will reduce the size of it as mentioned as another answer. You can also use the switch statement. Similar to if and elseif but easier and will reduce your code.

Example:

<?php
function access_option($access)
{
 switch ($access) {
    case 0: echo "access equals 0";
    case 1: echo "access equals 1";
    case 2: echo "access equals 2";
 }
}
?>

Comments

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.