IF输出中的IF / ELSEIF语句

时间:2017-12-11 01:13:50

标签: php

有人可以解释一下如何在$ output中添加if / elseif statemnts吗?

谢谢!

以下是代码:

        $output .= "  
                 <tr>  
                     <td class='signal-list'><a href data-toggle='modal' data-target='#showSignal$id'>$id</a></td>
                      <td class='signal-list'>$date_added</td>
                      <td class='signal-list'>$pr</td>
                      <td>$ac</td>
                      <td class='signal-list'>$entr</td>
                      <td>$dir</td>
                      <td>$clo</td>
                      <td>$p_l</td>
                 </tr>  
            ";  
       }  
       $output .= '</table>';  
  }  
  echo $output;  

我想在$ output

中添加此代码
  if($status == 'Profit') {

  echo "<td class='signal-list'><p class='label label-success'>PROFIT</p></td>";

    } elseif($status == 'Loss') {

  echo "<td class='signal-list'><p class='label label-danger'>&nbsp; LOSS &nbsp;</p></td>";

    } else {

 echo "<td class='signal-list'><p class='label label-primary'>&nbsp; LIVE &nbsp;</p></td>";

   }   

2 个答案:

答案 0 :(得分:1)

    $output .= "  
             <tr>  
                 <td class='signal-list'><a href data-toggle='modal' data-target='#showSignal$id'>$id</a></td>
                  <td class='signal-list'>$date_added</td>
                  <td class='signal-list'>$pr</td>
                  <td>$ac</td>
                  <td class='signal-list'>$entr</td>
                  <td>$dir</td>
                  <td>$clo</td>
                  <td>$p_l</td>
                  ";
if($status == 'Profit') {
    $output .= "<td class='signal-list'><p class='label label-success'>PROFIT</p></td>";
}
elseif($status == 'Loss') {
    $output .= "<td class='signal-list'><p class='label label-danger'>&nbsp; LOSS &nbsp;</p></td>";
} else {
    $output .= "<td class='signal-list'><p class='label label-primary'>&nbsp; LIVE &nbsp;</p></td>";
}                 
$output .= "
             </tr>  
        ";

$output .= '</table>';  
echo $output;

答案 1 :(得分:1)

这取决于您是否希望其他单元格位于现有单元格之前或之后,但您需要做的只是在<tr>添加之外取$output,然后更改echo 1}}使用$output附加到.=变量。

以下是展示之后添加内容的示例,但如果您希望事先加入Existing cellsNew cells,则可以轻松交换<?php // Start the table and table row - note that you do not use `.=` here $output = '<table><tr>'; // Existing cells $output .= " <td class='signal-list'><a href data-toggle='modal' data-target='#showSignal$id'>$id</a></td> <td class='signal-list'>$date_added</td> <td class='signal-list'>$pr</td> <td>$ac</td> <td class='signal-list'>$entr</td> <td>$dir</td> <td>$clo</td> <td>$p_l</td> "; // New cells if($status == 'Profit') { $output .= "<td class='signal-list'><p class='label label-success'>PROFIT</p></td>"; } elseif($status == 'Loss') { $output .= "<td class='signal-list'><p class='label label-danger'>&nbsp; LOSS &nbsp;</p></td>"; } else { $output .= "<td class='signal-list'><p class='label label-primary'>&nbsp; LIVE &nbsp;</p></td>"; } // End the table row and table itself $output .= '</tr></table>'; echo $output; 和{{1}}。结构仍然是正确的。

{{1}}

希望这有帮助! :)