如何通过PHP

时间:2019-01-14 19:01:45

标签: php html sql

我正在尝试在我的足球俱乐部网站项目上制作足球卡。如何用较少的代码行制作所有卡?

我使用带有w3.css框架的html为一张播放器制作了一张卡,并将php包含在我的html代码中,如下所示:

  <div class="w3-card w3-light-gray w3-animate-zoom w3-tooltip w3-hover-opacity">
    <div class="w3-text-indigo w3-display-topleft w3-xxlarge" style="width:20%;height:20%">
      <?php
        $sql = "SELECT id FROM men WHERE id='1';";
        $result = mysqli_query($conn,$sql);
        $resultCheck = mysqli_num_rows($result);

        if ($resultCheck > 0){
          while ($row = mysqli_fetch_assoc($result)) {
            echo $row['id']."<br>";
          }
        }
      ?></div>
    <div class="w3-display-topright w3-text-dark-gray w3-large" style="width:20%;height:20%;padding:10px">
      <img src="flags/spain.svg" alt="Spain">
    </div>
    <img src="players/kepa.png" alt="Kepa Arrizabalaga" style="width:100%">
    <p class="w3-text w3-text-dark-gray w3-large">
      <?php
      $sql = "SELECT name, age, country FROM men WHERE id='1';";
      $result = mysqli_query($conn,$sql);
      $resultCheck = mysqli_num_rows($result);

      if ($resultCheck > 0){
        while ($row = mysqli_fetch_assoc($result)) {
          echo $row['name']."<br>"."Age: ".$row['age']."<br>"."Country: ".$row['country'];
        }
      }
    ?></p>
  </div>

它可以正常工作,但是对于每张卡重复这些步骤会使我的代码变得很长。 我已经不得不在一张卡中重复两次php和sql代码,这对我来说似乎是不必要的,但不知道如何使其工作。

1 个答案:

答案 0 :(得分:0)

也许

<?php
$sql = "SELECT * FROM men ORDER BY id;";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0){
    while ($row = mysqli_fetch_assoc($result)) {
?>
        <div class="w3-card w3-light-gray w3-animate-zoom w3-tooltip w3-hover-opacity">
            <div class="w3-text-indigo w3-display-topleft w3-xxlarge" style="width:20%;height:20%">
                <?php echo $row['id']; ?>
            </div>
            <div class="w3-display-topright w3-text-dark-gray w3-large" style="width:20%;height:20%;padding:10px">
                <img src="flags/spain.svg" alt="Spain">
            </div>
            <img src="players/kepa.png" alt="Kepa Arrizabalaga" style="width:100%">
            <p class="w3-text w3-text-dark-gray w3-large">
                <?php echo $row['name']."<br>"."Age: ".$row['age']."<br>"."Country: ".$row['country']; ?>
            </p>
        </div>

<?php
    }
}
?>