数据库文本在PHP while循环中打印两次

I have database table with the following columns (id, title, image, text). So far I only have 3 rows they are:

1  Lorem ipsum dolor      [Image-Link]  [Text is blank here]
2  [title is blank here]  [Image-Link]  [Text is blank here]
3  Mediocrem voluptaria   [Image-Link]  detraxit eleifend pr

This is my code:

HTML/PHP

<?php
        $resultSet = $db->query("SELECT * FROM table");

        if ($resultSet->num_rows != 0)
        {
             while ($rows = $resultSet->fetch_assoc())
            {
                $id = $rows["id"];


                if ($id <= 3)
                {
                    $images = $rows["image"];
                    $title = $rows["title"];
                    echo "<div id=main>";
                    if ($id == 1)
                    {

                        echo "<div id=mainImg>";
                        echo "<img src=$images>";
                        echo "<div id=mainTitle>";
                        echo "<h2>$title</h2>";
                        echo "</div>";
                        echo "</div>";

                    }
                    echo "<div id=secDiv>";
                    if ($id == 2)
                    {
                        echo "<img id=secImg src=$images>";
                    }
                    else
                    {

                        echo "<img id=thirdImg src=$images>";
                        echo "<h2>$title</h2>";
                    }
                    echo "</div>";
                    echo "</div>";
                }


            }
        }

    ?>

CSS

body{
    position: relative;
}

#main{
    position: relative;
    width: 70%;
    height: auto;
    margin: 0 auto;
}

#mainImg{
    position: absolute;
    width: 65%;
}

#mainImg img{
    width: 100%;
}

#mainTitle{
    position: absolute;
    width: 100%;
    height: 25%;
    bottom: 1.5%;
    background-color: rgba(100, 0, 0, 0.7);
}

#mainTitle h2{
    color: white;
    text-align: center;
    font-family: sans-serif;
    font-size: 150%;
    opacity: 1;
}

#secDiv{
    position: absolute;
    right: 0%;
    top: 0%;
    width: 30%;
    height: auto;
}

#secImg{
    width: 45%;
    float: left;
}



#thirdImg{
    width: 45%;
    float: right;
}

#secDiv h2{
    clear: both;
    font-size: 12px;
}

The problem that I am having is that the h2 tag in the else statement is printing the first Title and the third title for some reason. Even when the first title got printed already in the first if statement, it still shows up when $id has to be at least 3. What am I doing wrong?

If we go over your code real fast

            if ($id <= 3)
            {
                //Executes for ID: 1, 2 and 3
                if ($id == 1)
                {
                    //Executes for ID: 1
                }
                if ($id == 2)
                {
                    //Executes for ID: 2
                }
                else
                {
                    //Executes if ID !== 2
                    //So this part executes for ID: 1 and 3
                }
            }

If I understand correctly, you want your last else to execute only for the ID 3. In that case you'll need another IF, or better, take a look at PHP's switch statement (http://php.net/manual/en/control-structures.switch.php).

You script that

if $id == 2 them show secImg else show thirdImg. So if $id != 2, thirdImg shows. So also if $id == 1 thirdImg shows. You should use

elseif($id == 3)

Maybe you want a

else if ($id == 2) 

instead of

if ($id == 2)

because if Id == 1

                if ($id == 1)
                {
                    // go in here
                    echo "<div id=mainImg>";
                    echo "<img src=$images>";
                    echo "<div id=mainTitle>";
                    echo "<h2>$title</h2>";
                    echo "</div>";
                    echo "</div>";

                }
                echo "<div id=secDiv>";
                if ($id == 2)
                {
                    echo "<img id=secImg src=$images>";
                }
                else
                {
                    // go in here
                    echo "<img id=thirdImg src=$images>";
                    echo "<h2>$title</h2>";
                }

if Id == 2

                if ($id == 1)
                {
                    echo "<div id=mainImg>";
                    echo "<img src=$images>";
                    echo "<div id=mainTitle>";
                    echo "<h2>$title</h2>";
                    echo "</div>";
                    echo "</div>";

                }
                echo "<div id=secDiv>";
                if ($id == 2)
                {
                    // go in here
                    echo "<img id=secImg src=$images>";
                }
                else
                {
                    echo "<img id=thirdImg src=$images>";
                    echo "<h2>$title</h2>";
                }

if Id == 3

                if ($id == 1)
                {
                    // go in here
                    echo "<div id=mainImg>";
                    echo "<img src=$images>";
                    echo "<div id=mainTitle>";
                    echo "<h2>$title</h2>";
                    echo "</div>";
                    echo "</div>";
                }
                echo "<div id=secDiv>";
                if ($id == 2)
                {
                    echo "<img id=secImg src=$images>";
                }
                else
                {
                    // go in here
                    echo "<img id=thirdImg src=$images>";
                    echo "<h2>$title</h2>";
                }

Your are using if condition in a wrong manner. Look at this logic:

$id = 1;

if ($id == 1)
  echo "id is 1";

if ($id == 2)
  echo "id is 2";
else
  echo "id is something else";

If you execute the above snippet it will print both "id is 1" and "id is something else". This is because $id=1 matches with the else part of the if ($id == 2).

The logic should be:

if ($id == 1 )
   echo "id is 1"
else
  if ($id == 2)
    echo "id is 2"
  else
    echo "id is something else" 

If you have all three IDs in result than you can also try this:

if($id <= 3){
    if($id == 1){
        // code here for id 1
    }
    elseif($id == 2){
        // code here for id 2   
    }
    else{
        // code if id is not either 1 or 2
    }
}