dta38159 2015-07-18 18:31
浏览 53
已采纳

如何使用PHP回显具有条件语句的变量?

Im having a bit of an issue right now with my PHP code. So first of all, I have a MySQL database where I have some data stored, and I'm retrieving that data and displaying it on a page using PHP. Here's the code so I can better describe my problem:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM sample";
$result = $conn->query($sql);

if ($result->num_rows > 0) {


 // output data of each row
 while($row = $result->fetch_assoc()) {
     echo "<span class='infoTitle'>Now:</span>".$row["now"]."<br>".
"<span class='infoTitle'>Status:</span> ".$row["status"]."<br>";

if (trim($row["now"] == 1)) {echo "Yes";} if(trim($row["now"] == 0)) {echo "";}
if ($row["status"] == 3) {echo "Open";} if($row["status"] == 2) {echo "Closed";}

}

}


 else {
     echo '<h2 class="noInfo">Whoops! Looks like there is currently no information available.</h2>';
}

$conn->close();
?>  

So the problem I am having is with the Conditional Statements. I have input hidden in the php page where this information is coming from that will input the values of the hidden field into MySQL. So what the code is currently displaying in my browser is something like this:

Now: 1
Status: 3
YesOpen

But what I'm trying to get it to do is this

Now: Yes
Status: Open
  • 写回答

2条回答 默认 最新

  • dougou7008 2015-07-18 19:06
    关注

    Does this make sense to you. I've established the values before I try to output the lines

    // output data of each row
     while($row = $result->fetch_assoc()) {
    $firstvar = "";
    $secondvar = "";
    
    if (trim($row['now']) == 1)
        {$firstvar = "Yes";}
    if(trim($row['now']) == 0)
        {$firstvar = "";}
    if ($row['status'] == 3)
        {$secondvar = "Open";}
    if($row['status'] == 2)
        {$secondvar = "Closed";}
         echo "<span class='infoTitle'>Now: </span>" . $firstvar . "<br>";
         echo "<span class='infoTitle'>Status: </span> " . $secondvar . "<br>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集