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

图片转代码服务由CSDN问答提供 功能建议

我的PHP代码现在有点问题。 首先,我有一个MySQL数据库,我存储了一些数据,我正在检索这些数据并使用PHP在页面上显示它。 这是代码,所以我可以更好地描述我的问题:

 &lt;?php 
 $ servername =“localhost”; 
 $ username =“root”; 
 $  password =“”; 
 $ dbname =“my_database”; 
 
 //创建连接
 $ conn = new mysqli($ servername,$ username,$ password,$ dbname); 
 //检查连接\  nif($ conn-&gt; connect_error){
die(“连接失败:”。$ conn-&gt; connect_error); 
} 
 
 $ sql =“SELECT * FROM sample”; 
 $ result = $  conn-&gt; query($ sql); 
 
if($ result-&gt; num_rows&gt; 0){
 
 
 //输出每行的数据
 while($ row = $ result-&gt  ; fetch_assoc()){
 echo“&lt; span class ='infoTitle'&gt; Now:&lt; / span&gt;”。$ row [“now”]。“&lt; br&gt;”。
“&lt; span  class ='infoTitle'&gt;状态:&lt; / span&gt;“。$ row [”status“]。”&lt; br&gt;“; 
 
if(trim($ row [”now“] == 1))  {echo“是”;} if(trim($ row [“now”] == 0)){echo“”;} 
if($ row [“status”] == 3){echo“Open”;}  if($ row [“status”] == 2){echo“Closed”;} 
 
} 
 
} 
 
 
其他{
 echo'&lt; h2 class =“noInfo”  &GT;哎呦! 看起来目前没有可用的信息。&lt; / h2&gt;'; 
} 
 
 $ conn-&gt; close(); 
?&gt;  
   
 
 

所以我遇到的问题是条件语句。 我有隐藏在php页面中的输入信息来自哪里,它会将隐藏字段的值输入MySQL。 那么我的浏览器当前显示的代码是这样的:

 现在:1 
Status:3 
YesOpen 
   
  
 

但是我想让它做的是这个

 现在:是
状态:打开
   \  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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>";
    
    打赏 评论
  • dtf54486 2015-07-18 19:15

    You've got few errors in Your code:

    • trim($row["now"] == 1) - it should be trim($row["now"]) == 1 and so on (otherwise You execute trim on boolean value since $row["now"] == 1 will be evaluated first)
    • echo is called just at the start of the loop and THEN 'status' and 'now' are checked and displayed and it has no effect to first echo since in next iteration new row is read and unformatted values put into 'first echo'

    Just change while loop to something like that:

    while ($row = $result->fetch_assoc()) {
        if (intval($row["now"]) == 1) {
            $now = "Yes";
        } else {
            $now = "";
        } 
    
        if ($row["status"] == 3) {
            $status = "Open";
        } else if($row["status"] == 2) {
            $status = "Closed";
        } else {
            $status = '';
        }
    
        echo "<span class='infoTitle'>Now:</span>".$now."<br><span class='infoTitle'>Status:</span> ".$status."<br>";
    }
    

    Then should be fine.

    打赏 评论

相关推荐 更多相似问题