0

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
  • I don't know what are you trying to do but trim($row["now"] == 1) is wrong syntax Commented Jul 18, 2015 at 18:43
  • remove this line echo "<span class='infoTitle'>Now:</span>".$row["now"]."<br>". "<span class='infoTitle'>Status:</span> ".$row["status"]."<br>"; and try you are getting 1 and 3 from this line of code Commented Jul 18, 2015 at 19:26

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>";

2 Comments

BigScar and adam.pilacki both of you were on the money! thanks a lot I appreciate it !!
Thanks Adam for catching the Trim() syntax problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.