php echo中的div id不起作用

时间:2017-06-08 16:38:06

标签: php

对于学校项目,我需要建立一个网站。我想在PHP中使用echo创建一个div但不知何故它不起作用。 这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <link rel="icon" type="image/png" href="./images/favicon-32x32.png" 
    sizes="32x32" />
    <link rel="icon" type="image/png" href="./images/favicon-16x16.png" 
    sizes="16x16" />
    <title>MLB: Major League Baseball</title>
    <link href="css folder/MLBstylesheet.css" rel="stylesheet" 
    type="text/css"/>
</head> 
<body>
    <div id="container">
        <div id="titel">    

           <img class="MLBTitel"  src="./images/MLBtitel.jpg" alt="MLBTitel" >
           <div id="titeltekst">
               MAJOR LEAGUE BASEBALL
               <br> 
           </div>

           <nav>
              <ul>
                 <li><a class= "menu" href="index.html">Home</a></li>
                 <li><a class= "menu" href="spelers.php">Spelers</a></li>
                 <li><a class= "menu" href="teams.php">Teams</a></li>
                 <li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
                 <li><a class= "menu" href="contact.html">Contact</a></li>
             </ul>
         </nav>

         <br><br>       
     </div>
     <div id="spelerstabel">
        <?php
        $servername = "localhost";
        $username = "id1419279_root";
        $password = "nivardenjoey";
        $dbname = "id1419279_mlb";


        $conn = new mysqli($localhost, $username, $password, $dbname);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "SELECT foto, voornaam, achternaam, team FROM spelers";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {

            while($row = $result->fetch_assoc()) {
                echo "<div id='spelersfoto'> <img class='spelersfoto' src=' " . 
                $row["foto"]. "'/> - Name: " . $row["voornaam"]. " " . $row["achternaam"]. " 
                - Team: " .$row["team"]. " </div>";


            }
        } else {
            echo "0 results";
        }
        $conn->close();
        ?>
    </div>
</div>
</body>
</html>

有人能帮助我吗?

*我在这里复制了我的整个代码,这可能更容易

2 个答案:

答案 0 :(得分:1)

我删除了src=' "参数中的空格。此外,请尝试使用唯一的ID。如果您需要为每个div应用一些css,请改为使用类:

$i = 0;
while($row = $result->fetch_assoc()) {
    $i++;
    echo "<div id='spelersfoto-'".$i."><img class='spelersfoto' src='" . $row["foto"]. "'/> - Name: " . $row["voornaam"]. " " . $row["achternaam"]. " - Team: " .$row["team"]. " </div>";
}

答案 1 :(得分:0)

NivardJ,

试试这个:

$cntr = 1;
while($row = $result->fetch_assoc()) {
    echo "<div id='spelersfoto-$cntr'> 
              <img class='spelersfoto' src='" . $row["foto"]. "'/>
              - Name: " . $row["voornaam"]. " " . $row["achternaam"]. " 
              - Team: " .$row["team"]. " 
          </div>";
    $cntr++;
}

希望这有帮助!

最佳,

-Rush