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

时间:2015-12-18 07:46:25

标签: php html mysql css

我有数据库表,其中包含以下列(id,title,image,text)。到目前为止,我只有3行:

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

这是我的代码:

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

我遇到的问题是else语句中的h2标签出于某种原因打印第一个标题和第三个标题。即使第一个标题已经在第一个if语句中打印出来,它仍然显示$ id必须至少为3.我做错了什么?

5 个答案:

答案 0 :(得分:5)

如果我们快速查看您的代码

            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
                }
            }

如果我理解正确,你希望你的最后一个只为ID 3执行。在这种情况下你需要另一个IF,或者更好,看一下PHP的switch语句(http://php.net/manual/en/control-structures.switch.php)。 / p>

答案 1 :(得分:1)

你编写

脚本

if $id == 2他们显示secImg else显示thirdImg。所以if $id != 2,thirdImg显示。所以if $id == 1 thirdImg也显示了。你应该使用

elseif($id == 3)

答案 2 :(得分:1)

也许你想要一个

else if ($id == 2) 

而不是

if ($id == 2)

因为如果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>";
                }

如果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>";
                }

如果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>";
                }

答案 3 :(得分:1)

您以错误的方式使用if条件。看看这个逻辑:

$id = 1;

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

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

如果你执行上面的代码片段,它将同时打印“id is 1”和“id is else”。这是因为$id=1与if else的{​​{1}}部分匹配。

逻辑应该是:

($id == 2)

答案 4 :(得分:0)

如果结果中包含所有三个ID,您也可以尝试:

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
    }
}