duanjia7607
2018-04-30 18:28
浏览 16
已采纳

无论如何把它放在一个函数中,所以不使用重复的代码

This is my code for a project I am working on, is there anyway to shorten this code or even put it in a function so it is not this much repetitive code. I'm learning programming, and I was unable to find a solution to fix it. Is it a while loop or something I am meant to use?

$sth=$conn->prepare("SELECT * FROM players WHERE pid = {$steamprofile['steamid']}");
    $sth->execute();
    $result = $sth->fetch();
    if($result['rank'] == 11){
        echo "<button style='background-color: #ad0521; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Godfather</button>";
    }
    if($result['rank'] == 10){
        echo "<button style='background-color: #999900; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Mob Boss</button>";
    }
    if($result['rank'] == 9){
        echo "<button style='background-color: #798488; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Under Boss</button>";
    }
    if($result['rank'] == 8){
        echo "<button style='background-color: #397be5; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Warlord</button>";
    }
    if($result['rank'] == 7){
        echo "<button style='background-color: #6f9de8; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Turf Captain</button>";
    }
    if($result['rank'] == 6){
        echo "<button style='background-color: #c1722c; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Hitman</button>";
    }
    if($result['rank'] == 5){
        echo "<button style='background-color: #ffbf00; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Henchman</button>";
    }
    if($result['rank'] == 4){
        echo "<button style='background-color: #008c5f; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Loan Shark</button>";
    }
    if($result['rank'] == 3){
        echo "<button style='background-color: #12c98c; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Enforcer</button>";
    }
    if($result['rank'] == 2){
        echo "<button style='background-color: #6a0e91; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Butcher</button>";
    }
    if($result['rank'] == 1){
        echo "<button style='background-color: #b94ae8; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Scout</button>";
    }
    if($result['rank'] == 0){
        echo "<button style='background-color: #595959; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>Guest</button>";
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpl57372 2018-04-30 18:36
    已采纳

    You can have an array which holds the various attributes for each rank (I've used only colour and the label), then just use these in the one echo statement...

    $rankTypes = [ 0 => ["color" => "#595959", "name" => "Guest"],
                   1 => ["color" => "#b94ae8", "name" => "Scout"]
    ];
    echo "<button style='background-color: {$rankTypes[$result['rank']]['color']}; width: 100%; color: white; border: 0px solid black; font-family: myFirstFont; font-size: 18px;' disabled>{$rankTypes[$result['rank']]['name']}</button>";
    

    You keep on adding the extra rank types into the array.

    点赞 评论

相关推荐 更多相似问题