获取结果时如何选择所需的图标?

时间:2016-01-12 13:14:19

标签: php performance if-statement

我正在创建一个Q& A网站,像SO一样。现在我需要在每个答案旁边打印一个“检查”图标 (以指定接受的答案)。现在有一些条件:

  • 当前用户是否已登录?
  • 当前用户是问题的作者吗?
  • 有没有接受的答案?

以及其他一些案例..!

其实我实现了这一点。但我使用了很多嵌套的if语句来做到这一点,在我看来真的没有优化。现在我想知道有没有更好的算法来编写这段代码?

$results = $stm->fetchAll(PDO::FETCH_ASSOC);       // Fetching all answers

foreach ($results as $result){

    if ($_SESSION['id'] == $author_id){            // User is logged
        if($result['id'] == $AcceptedAnswerId){    // There is accepted answer
            $AcceptAnswerIcon = "<a href='#'>
                                    <i style='color: green;' class='fa fa-check'></i>
                                 </a>";
        } else {                                   // There isn't accepted answer
            $AcceptAnswerIcon = "<a href='#'>
                                    <i style='color: gray' class='fa fa-check'></i>
                                 </a>";
        }
    } 

    elseif ($_SESSION['id'] !== $author_id) {      // User isn't logged
        if($result['id'] == $AcceptedAnswerId){    // There isn accepted answer
            $AcceptAnswerIcon = "<i style='color: green;' class='fa fa-check'></i>";
        } else {                                   // There isn't accepted answer
            $AcceptAnswerIcon = null;
        }
    }

    else {
        $AcceptAnswerIcon = null;
    }


// ... I will use $AcceptAnswerIcon in here and mix it with answer structure


} // End of "foreach" block             

3 个答案:

答案 0 :(得分:0)

更新:

首先,因为你只找到1个正确的答案,在找到(或不找到)后,它会跳出循环,保持简单:

foreach($results as $result)
{
  $foundCorrectAnswer = ($result['id'] == $AcceptedAnswerId);
  if($foundCorrectAnswer)
  {
    break;
  }
}

毕竟,有3个可能的输出(我的第一个错误的东西是我只看到2个),所以这里是:

if($isLogged)
{
  $color = $foundCorrectAnswer ? 'green' : 'gray';
  $AcceptAnswerIcon = "<a href='#'><i style='color: {$color};' class='fa fa-check'></i></a>";
}
else if($foundCorrectAnswer)
{
  $AcceptAnswerIcon = "<i style='color: green;' class='fa fa-check'></i>";
}

如此完整的代码:

$isLogged = $_SESSION['id'] == $author_id;
$results = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $result)
{
  $foundCorrectAnswer = ($result['id'] == $AcceptedAnswerId);
  if($foundCorrectAnswer)
  {
    break;
  }
}

if($isLogged)
{
  $color = $foundCorrectAnswer ? 'green' : 'gray';
  $AcceptAnswerIcon = "<a href='#'><i style='color: {$color};' class='fa fa-check'></i></a>";
}
else if($foundCorrectAnswer)
{
  $AcceptAnswerIcon = "<i style='color: green;' class='fa fa-check'></i>";
}

答案 1 :(得分:0)

更好地使用面向对象的PHP以便更好地处理...

class Author{

    //funtion will return true if author
    function is_author(){

    }



    //function will return true if it is accepted.
    function is_accepted(){

    }


}


$author = new Author();

现在使用此条件减少更多if和else ...

if ( $author->is_author() ) {
    //do something

    $AcceptAnswerIcon = "<a href='#'>
                                <i style='". if($author->is_accepted()) { ."color: green;".} else { ."color:gray;' class='fa fa-check'></i>
                             </a>";


} else {


    if($result['id'] == $AcceptedAnswerId){    // There isn accepted answer
        $AcceptAnswerIcon = "<i style='color: green;' class='fa fa-check'></i>";
    } else {                                   // There isn't accepted answer
        $AcceptAnswerIcon = null;
    }


}

答案 2 :(得分:0)

说明

  • 在你的逻辑 if ... elseif ... else 中,最后的 else 没有意义,因为用户只能被记录。
  • 为了使代码更紧凑,您可以将在不同场合使用的字符串(和其他)元素放入变量或常量(定义)。
    这会影响您的代码更小,也可能是(?)更快。
    在这里,我使用&lt; a href = ...&gt; 字符串创建了一个模板,其中 {var} 标记被变量替换在运行时。
  • 另一方面,当你冒险让人难以理解时,我不建议强制使用你的代码。
  • 下面你会找到一个更紧凑的代码变体。
    我使用三元条件 $ result =(condition)? truevalue:falsevalue ,它通常使代码更紧凑(但见上文)。

代码段

$results = $stm->fetchAll(PDO::FETCH_ASSOC);       // Fetching all answers
define ('template','<a href="#">'.PHP_EOL.'   <i style="color: {color};" class="fa fa-check"></i>'.PHP_EOL.'</a>');

foreach ($results as $result){
    $has_acceptedanswers = ($result['id'] == $AcceptedAnswerId);
    if ($_SESSION['id'] == $author_id){           // User is logged
        $template = template;
        $color = ($has_acceptedanswers) ? 'green' : 'gray';
    } else {                                      // User is not logged
        $template = ($has_acceptedanswers) ? template : false;
        $color = 'green';
    }
    $AcceptAnswerIcon = ($template === false) ? null : str_replace('{color}',$color,$template);

    //...
}
相关问题