【问题标题】:PHP videos multiple pages doesn't work correctlyPHP 视频多个页面无法正常工作
【发布时间】:2016-01-21 21:06:16
【问题描述】:

我正在做一个简单的视频博客网站项目,我试图每页只显示 20 个视频缩略图,我在索引中编写了这段代码,将视频分成多个页面,然后对它们进行分页......问题是它在无限页中每页显示相同的第一个视频的缩略图 20 次。

我真的需要有关此代码的帮助

<?php

   require_once ('db.php') ;
   require_once ('VideosApi.php') ;
   $count = mysql_query('SELECT COUNT(id) AS numb FROM videos ORDER BY id');  
   $array = mysql_fetch_assoc($count);
   $number = $array['numb'];  
   mysql_free_result($count);  
   $PerPage = 20;
   $nbPage = ceil(abs($number/$PerPage));
   if(isset($_GET['page']) && $_GET['page'] > 0 && $_GET['page'] <= $nbPage && preg_match('#^[0-9]+$#',$_GET['page'])){ $cPage = $_GET['page']; }
   else{ $cPage = 1; }  

   $Query = mysql_query('SELECT * FROM Videos ORDER BY id LIMIT '.(($cPage-1) * $PerPage).','.$PerPage);  

              $videos = Videos_Get() ;
           if ($videos == Null)
             die ('problem');

        $vcount = @count ($videos) ;

           if ($vcount == 0)
             die('no videos') ;



        For ($i = 0; $i < $vcount; $i++)
          {
        $video = $videos [$i];

        if ($video->time > 3600)
            $duration = gmdate("H:i:s",$video->time);
        else
            $duration = gmdate("i:s",$video->time);




        while($Rows = mysql_fetch_assoc($Query)){ 

          echo ( "<div class=\"video\">
                <a href=\"video.php?id=$video->id\"><img src=\"$video->img\"></a><span class=\"class-video-name\">$video->name</span>
                <div class=\"class-video-footer\">
                <span class=\"class-video-duration\">$duration</span>
                </div>
         </div>") ; }   

        } ?>

【问题讨论】:

    标签: php video


    【解决方案1】:

    在我们得到正确答案之前的一些提示:

    1. 不要使用mysql_*。现在不推荐使用该系列函数,并且在未来的 PHP 版本中将不再提供支持。为延长代码寿命,请考虑使用MySQLiPDO
    2. 使用is_numeric() 检查字符串是否有数值。对于这样一个简单的任务,使用preg_match() 是非常繁重的。
    3. 尽量避免在 MySQL 查询中使用 SELECT *。您很少需要表中的所有内容,因此获取行的所有字段是very inefficient(尤其是在您没有以最佳方式使用索引的情况下)。

    话虽如此,我已经花了一些时间按照我上面所宣扬的做法重写了您的代码。下面是修改后的代码,下面是错误的解释:


    更新db.php如下:

    <?php
    
    $db = new PDO( 'mysql:dbname=DATABASE_NAME;host=127.0.0.1', 'DATABASE_USER', 'DATABASE_PASSWORD' );
    
    ?>
    

    现在是你的主文件:

    <?php
    
    require_once 'db.php';
    require_once 'VideosApi.php';
    
    $count = $db->query( 'SELECT COUNT(id) AS total FROM videos' )->fetchObject();
    $number = $count->total;
    
    $perPage = 20;
    $pages   = ceil( $number / $perPage );
    
    $page    = ( isset($_GET['page']) ) ? $_GET['page'] : 1;
    $page    = ( $page < 1 || $page > $pages || !is_numeric($page) ) ? 1 : $page;
    
    $offset  = ($page - 1) * $perPage;
    $query   = $db->query( 'SELECT id, img, name, time FROM videos ORDER BY id LIMIT '.$offset.','.$perPage );
    
    if( $query->rowCount() < 1 )
    {
        die( 'No videos' );
    }
    
    $html = '';
    
    while( $video = $query->fetchObject() )
    {
        $duration = ($video->time > 3600) ? gmdate('H:i:s', $video->time) : gmdate('i:s', $video->time);
    
        $html.= '<div class="video">';
        $html.= "\n\t".'<a href=\"video.php?id='.$video->id.'">';
        $html.= "\n\t\t".'<img src="'.$video->img.'" />';
        $html.= "\n\t".'</a>';
        $html.= "\n\t".'<span class="class-video-name">'.$video->name.'</span>';
        $html.= "\n\t".'<div class="class-video-footer">';
        $html.= "\n\t\t".'<span class="class-video-duration">'.$duration.'</span>';
        $html.= "\n\t".'</div>';
        $html.= "\n".'</div>';
    }
    
    echo $html;
    
    ?>
    

    您的原始代码的问题有点难以确定,因为您没有提供VideosApi.php 的内容,我假设您在此处定义Videos_Get()。无论如何,我怀疑正在发生的事情是,Videos_Get() 实际上忽略了您的所有分页,但正如我所说,很难诊断,因为我们看不到您的所有代码!

    【讨论】:

    • 你的代码很棒..它运行良好,看起来也很专业我希望有一天我能像你一样编码..非常感谢:D
    • 如果上面的代码有帮助,请考虑投票并接受它作为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多