PHP 视频多页无法正常工作


PHP videos multiple pages doesn't work correctly

我正在做一个简单的视频博客网站项目,我试图每页只显示 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>") ; }   
        } ?>

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

  1. 不要使用mysql_* .该系列函数现已弃用,并且在 PHP 的未来版本中将放弃支持。为了延长代码寿命,请考虑使用 MySQLi 或 PDO。
  2. 使用 is_numeric() 检查字符串是否具有数值。对于如此简单的任务,使用 preg_match() 是非常繁重的负载。
  3. 尽量避免在MySQL查询中使用SELECT *。您很少需要表中的所有内容,因此获取行的所有字段效率非常低(尤其是在您没有以最佳方式使用索引的情况下)。

话虽如此,我花了一些时间按照我上面宣扬的实践重写了你的代码。下面是修改后的代码,下面是对错误解释:


更新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()实际上忽略了您的所有分页,但正如我所说,很难诊断,因为我们看不到您的所有代码!