首页 > 解决方案 > 在页脚中首先处理 HTML

问题描述

我有一个问题在我的页脚中,有部分 Html 和 PHP,然后又是 HTML。问题是在网站上,整个 HTML 都显示在 PHP 之前。你可以在下面的照片中看到它。

谢谢,JS

<footer class="row align-items-center block bg-light border-t-o">
   <!-- Footer Titel -->
   <div class="col-lg text-left footer-content">
      <h1 class="sponsor">Sponsoren:</h1>
   </div>
   <!-- Sponsorenbilder -->
   <?php
      $random1 = rand(1,4);
      $random2 = rand(1,4);
      $random3 = rand(1,4);
      $random4 = rand(1,4);
      echo "
      <div class='col-md text-center footer-content' style='order: " . $random1 . ";'>
          <img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 1'></img>
      </div>
      <div class='col-md text-center footer-content' style='order: " . $random2 . ";'>
        <img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 2'></img>
      </div>
      <div class='col-md text-center footer-content' style='order: " . $random3 . ";'>
        <img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 3'></img>
      </div>
      <div class='col-md text-center footer-content' style='order: " . $random4 . ";'>
        <img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 4'></img>
      </div>
      ";
      ?>
   <!-- Impressum Button -->
   <div class="col-lg text-right footer-content">
      <h1 class="impressum impressum-style"><a class="impressum-style" href="impressum.html">Impressum</a></h1>
   </div>
</footer>

在此处输入图像描述

标签: phphtmlposition

解决方案


如果我理解正确,问题不在于 PHP 实际上是第二个加载,而是 PHP 生成的实际代码块与代码流的位置不同。

这是因为给定order样式的列实际上放置没有顺序样式的列之后。

为了解决这个问题,您可以使用更多的 PHP 来确保第三块代码的顺序高于其他所有代码块。

<?php

  function getHighest($list_of_numbers){
    $highest = 0;

    foreach($list_of_numbers as $list_item){
      if ($list_item > $highest){
        $highest = $list_item;
      }
    }

    return $highest;
  }

  $highest = getHighest([$random1, $random2, $random3, $random4]) + 1;
  echo "<div class='col-lg text-right footer-content' style='order: ${highest}'>" 
  ?>

推荐阅读