我正在创建一个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
答案 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)
$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);
//...
}