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

时间:2018-04-30 18:28:22

标签: php pdo

这是我正在处理的项目的代码,无论如何都要缩短这个代码,或者甚至把它放在一个函数中,所以它不是那么多重复的代码。我正在学习编程,我无法找到解决方案来解决它。它是一个while循环还是我打算使用的东西?

$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 个答案:

答案 0 :(得分:4)

你可以有一个数组,其中包含每个等级的各种属性(我只使用了颜色和标签),然后只在一个echo语句中使用它们......

$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>";

您继续在阵列中添加额外的排名类型。