如何在PHP中基于视图显示和排序两个表(电影/系列)?

fykwrbwg  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(123)

我有一个名为“趋势”的部分,我想同时显示系列和电影(两个不同的表,没有****连接),但我想根据谁获得更多的视图来排序。我已经做了2个foreach循环,但它首先显示电影,然后是系列;问题是当一个连续剧有1000次观看,而一部电影有800次观看时,它会先播放这部电影,因为第一次迭代是为了流行电影。2有什么逻辑来解决这个问题吗?

<div class="trending" id="trending">
  
  <div class="trending-wrapper">
  <?php foreach($trendingMovies as $trendingMovie) : ?>
    <!-- Movie Start -->
    <a href="<?= base_url('movies/movie/') . $trendingMovie->movie_id ?>" class="trending_item">
      <div class="trending_item_poster">
        <img src="<?= $trendingMovie->movie_poster_large ?>" alt="<?= $trendingMovie->movie_name ?>">
      </div>
    </a>
    <!-- Movie End -->
    <?php endforeach; ?>

    <?php foreach($trendingSeries as $trendingSerie) : ?>
    <!-- Serie Start -->
    <a href="<?= base_url('series/serie/') . $trendingSerie->serie_id ?>" class="trending_tem">
      <div class="trending_item_poster">
        <img src="<?= $trendingSerie->serie_poster_large ?>" alt="<?= $trendingSerie->serie_name ?>">
      </div>
    </a>
    <!-- Serie End -->
    <?php endforeach; ?>
  </div>
 
</div>
uqzxnwby

uqzxnwby1#

使用JQuery(;

<div class="trending" id="trending">
  
  <div class="trending-wrapper">
  <?php foreach($trendingMovies as $trendingMovie) : ?>
    <div class="col-md-12 foot" data-position="<?= $trendingMovie->views?>">
        <!-- Movie Start -->
        <a href="<?= base_url('movies/movie/') . $trendingMovie->movie_id ?>" class="trending_item">
          <div class="trending_item_poster">
            <img src="<?= $trendingMovie->movie_poster_large ?>" alt="<?= $trendingMovie->movie_name ?>">
          </div>
        </a>
    </div>
    <!-- Movie End -->
    <?php endforeach; ?>

    <?php foreach($trendingSeries as $trendingSerie) : ?>
        <div class="col-md-12 foot" data-position="<?= $trendingSerie->views?>">    
            <!-- Serie Start -->
            <a href="<?= base_url('series/serie/') . $trendingSerie->serie_id ?>" class="trending_tem">
              <div class="trending_item_poster">
                <img src="<?= $trendingSerie->serie_poster_large ?>" alt="<?= $trendingSerie->serie_name ?>">
              </div>
            </a>
        </div>
    <!-- Serie End -->
    <?php endforeach; ?>
  </div>
 
</div>
<script>
    $("#trending div.foot").sort(function(a, b) {
      return $(a).data("position") - $(b).data("position");
    }).appendTo("#trending");
</script>

vanilla javascript的另一个示例

<ul id="list">
         <li class="index" data-index="1">1 </li>
         <li class="index" data-index="5">5</li>
         <li class="index" data-index="2">2</li>
         <li class="index" data-index="3">3</li>
         <li class="index" data-index="4">4 </li>
     </ul>
       
   
     <script>
         function comparator(a, b) {
             if (a.dataset.index < b.dataset.index)
                 return -1;
             if (a.dataset.index > b.dataset.index)
                 return 1;
             return 0;
         }
           
         // Function to sort Data
        
         var indexes = document.querySelectorAll("[data-index]");
         var indexesArray = Array.from(indexes);
         let sorted = indexesArray.sort(comparator);
         sorted.forEach(e =>
             document.querySelector("#list").appendChild(e));
         
     </script>

相关问题