【问题标题】:Database text is printing twice in a PHP while loop数据库文本在 PHP while 循环中打印两次
【发布时间】:2016-03-24 20:57:39
【问题描述】:

我有包含以下列(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 时显示。我做错了什么?

【问题讨论】:

  • 你在html中的属性应该用引号括起来。
  • @NiekdeGooijer 是否必须将它们封装在引号中?我问的原因是因为它可以在没有它的情况下工作,或者它只是旧网络浏览器的好习惯?
  • 是的。它是 xml/html 标准。浏览器会解析它并尝试猜测结果。

标签: php html mysql css


【解决方案1】:

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

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

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

【讨论】:

  • 很好的答案!差点忘了 Switch 语句!
  • 这是最好的解决方案之一。
【解决方案2】:

你的脚本

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

elseif($id == 3)

【讨论】:

    【解决方案3】:

    也许你想要一个

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

    【讨论】:

      【解决方案4】:

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

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

      如果你执行上面的sn-p,它会同时打印“id is 1”和“id is something else”。这是因为 $id=1 与 if ($id == 2)else 部分匹配。

      逻辑应该是:

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

      【讨论】:

        【解决方案5】:

        如果结果中包含所有三个 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
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-08
          • 2015-06-12
          • 1970-01-01
          相关资源
          最近更新 更多