如何以更短的方式编写许多类似的if语句?

时间:2016-06-15 06:49:44

标签: php mysql if-statement

有没有办法把这一串if语句编写得更短?

if($data==name){
    $sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
    $q = $pdo->prepare($sql1);
    $q->execute(array($name));
}
if($data==age){
    $sql2 = "UPDATE people set age = ? WHERE id = '$id'";
    $q = $pdo->prepare($sql2);
    $q->execute(array($age));
}
if($data==color1){
    $sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
    $q = $pdo->prepare($sql3);
    $q->execute(array($color));
}
if($data==color2){
    $sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
    $q = $pdo->prepare($sql4);
    $q->execute(array($color));
}
if($data==color3){
    $sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
    $q = $pdo->prepare($sql5);
    $q->execute(array($color));
}

我试图制作一个功能

function data($info, $information, $name){
        if($data==$info){
             $sql = "UPDATE people set $information = ? WHERE id = '$id'";
             $q = $pdo->prepare($sql);
             $q->execute(array($name));
        }
}

data(name, firstname, $name);

但这不是这样的,我没有得到结果。也许是因为sql请求中的变量?

3 个答案:

答案 0 :(得分:2)

Php开关,来自docs:

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}
// same as
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

应该是你想要的。 http://php.net/manual/en/control-structures.switch.php

答案 1 :(得分:1)

  <?php

      switch($data){
          case "name":
              $field = "firstName";
          break;
          case "age":
              $field = "age";
          break;
          case "color1":
              $field = "paint1";
          break;
          case "color2":
              $field = "paint2";
          break;
          case "color3":
              $field = "paint3";
          break;
          default:
             echo 'Invalid selection';
             exit;
          break;
      }
      $sql = "UPDATE people set $field = ? WHERE id = '$id'";
      $q = $pdo->prepare($sql);
      $q->execute(array($name));

答案 2 :(得分:1)

尝试使用if elseif,因为如果您为每个人使用if语句需要很长时间。所以当第一个条件为真时表示不需要检查另一个if条件,所以使用if elseif语句。或使用开关盒

        if($data==name){
            $sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
            $q = $pdo->prepare($sql1);
            $q->execute(array($name));
        }
        else if($data==age){
            $sql2 = "UPDATE people set age = ? WHERE id = '$id'";
            $q = $pdo->prepare($sql2);
            $q->execute(array($age));
        }
        else if($data==color1){
            $sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
            $q = $pdo->prepare($sql3);
            $q->execute(array($color));
        }
        else if($data==color2){
            $sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
            $q = $pdo->prepare($sql4);
            $q->execute(array($color));
        }
        else if($data==color3){
            $sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
            $q = $pdo->prepare($sql5);
            $q->execute(array($color));
        }
        else
        {

          echo "nothing";
        }


      (OR)


        switch ($data) {
            case name:
                $sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
                $q = $pdo->prepare($sql1);
                $q->execute(array($name));
                break;
            case age:
               $sql2 = "UPDATE people set age = ? WHERE id = '$id'";
               $q = $pdo->prepare($sql2);
               $q->execute(array($age));
                break;
            case color1:
                $sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
                $q = $pdo->prepare($sql3);
                $q->execute(array($color));
                break;
            case color2:
                $sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
               $q = $pdo->prepare($sql4);
               $q->execute(array($color));
                break;  
            case color3:
                $sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
               $q = $pdo->prepare($sql5);
              $q->execute(array($color));
                break;  
        }