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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀