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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog