PHP使用if语句回显

时间:2014-04-28 10:02:25

标签: php

我试图根据if语句的结果回显HTML代码的两个不同部分,这些结果再次从数据库中获取。结果的数量不明确,它将根据用户输入而变化。除了回显HTML之外,它一切正常。这是代码:

<?php  
        $query = mysql_query("SELECT dt, soba1, soba2, soba3, soba4, soba5, soba6,     soba7, soba8, soba9, soba10 FROM calendar_table WHERE (dt BETWEEN '$dateFrom' AND '$dateTo')"); 
        while($row = mysql_fetch_assoc($query))
        {
            $date = $row['dt']; 
            $soba1 = $row['soba1'];
            $soba2 = $row['soba2'];
            $soba3 = $row['soba3'];
            $soba4 = $row['soba4'];
            $soba5 = $row['soba5'];
            $soba6 = $row['soba6'];
            $soba7 = $row['soba7'];
            $soba8 = $row['soba8'];
            $soba9 = $row['soba9'];
            $soba10 = $row['soba10'];

           echo " 
             <div class='column'> 
                <p class='dateBox'> $date </p>
                ". 
                    ( ($soba1 != NULL) ?
                      echo "<p class='status'> <input type='text' name='persons'> </p>"
                    :
                      echo "<p class='status'> $soba1 </p>"
                    ."  
                <p class='status'> $soba2 </p>
                <p class='status'> $soba3 </p>
                <p class='status'> $soba4 </p>
                <p class='status'> $soba5 </p>
                <p class='status'> $soba6 </p>
                <p class='status'> $soba7 </p>
                <p class='status'> $soba8 </p>
                <p class='status'> $soba9 </p>
                <p class='status'> $soba10 </p>
              </div>"; 
            }; 
     ?> 

有关如何以不同方式书写或修改以使其有效的任何建议。我得到的错误是解析错误:语法错误,意外&#39; =&#39;在第137行的C:\ xampp \ htdocs \ plazahotel \ cro \ confirm.php

3 个答案:

答案 0 :(得分:1)

从三元内删除echo来电。您还缺少结束),因此它变为:

       echo " 
         <div class='column'> 
            <p class='dateBox'> $date </p>
            ". 
                (($soba1 != NULL) ?
                  "<p class='status'> <input type='text' name='persons'> </p>"
                :
                  "<p class='status'> $soba1 </p>")
                ."  
            <p class='status'> $soba2 </p>
            <p class='status'> $soba3 </p>
            <p class='status'> $soba4 </p>
            <p class='status'> $soba5 </p>
            <p class='status'> $soba6 </p>
            <p class='status'> $soba7 </p>
            <p class='status'> $soba8 </p>
            <p class='status'> $soba9 </p>
            <p class='status'> $soba10 </p>
          </div>"; 

答案 1 :(得分:1)

您只能使用以下语法:

 echo ($variableIsTrue ? 'Output A' : 'Output B');

但是你已经有了一个回声(从DIV开始)。您语法错误。删除不需要的回音并添加)

        <p class='dateBox'> $date </p>
            ". 
                ( ($soba1 != NULL) ?
                 "<p class='status'> <input type='text' name='persons'> </p>"
                :
                  "<p class='status'> $soba1 </p>"
                ) //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ADD!!!!!!!!!!!!!!!!!!!!!!!!!!
                ."  
            <p class='status'> $soba2 </p>

答案 2 :(得分:0)

您不能嵌套echo,为了便于阅读,请尝试:

<?php
echo "<div class='column'> 
      <p class='dateBox'> $date </p>";
if($soba1 != NULL){
      echo "<p class='status'> <input type='text' name='persons'> </p>";
}
else
{
      echo "<p class='status'> $soba1 </p> 
            <p class='status'> $soba2 </p>
            <p class='status'> $soba3 </p>
            <p class='status'> $soba4 </p>
            <p class='status'> $soba5 </p>
            <p class='status'> $soba6 </p>
            <p class='status'> $soba7 </p>
            <p class='status'> $soba8 </p>
            <p class='status'> $soba9 </p>
            <p class='status'> $soba10 </p>
          </div>";
}