PHP视频多个页面无法正常工作
我正在做一个简单的vlog网站项目,我试图每页只显示20个视频缩略图,我在索引中编写了这段代码,将视频分成多个页面,然后分页。。。问题在于,对于无限多个页面,它每页显示同一首视频的缩略图20次 我真的需要这个代码的帮助PHP视频多个页面无法正常工作,php,video,Php,Video,我正在做一个简单的vlog网站项目,我试图每页只显示20个视频缩略图,我在索引中编写了这段代码,将视频分成多个页面,然后分页。。。问题在于,对于无限多个页面,它每页显示同一首视频的缩略图20次 我真的需要这个代码的帮助 <?php require_once ('db.php') ; require_once ('VideosApi.php') ; $count = mysql_query('SELECT COUNT(id) AS numb FROM videos ORD
<?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>") ; }
} ?>
在我们找到正确答案之前,有几个提示: 不要使用mysql。这一系列函数现在已被弃用,在PHP的未来版本中将不再支持。对于代码长寿,考虑使用或。 用于检查字符串是否具有数值。对于这样一个简单的任务,使用preg_match是非常繁重的。 尽量避免在MySQL查询中使用SELECT*。您很少需要表中的所有内容,因此获取行的所有字段尤其是在未优化使用索引的情况下。 话虽如此,我还是花了一些时间按照我上面所讲的实践重写了您的代码。下面是修改后的代码,下面是错误原因的解释: 更新db.php如下: 现在查看主文件:
<?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\u Get。无论如何,我怀疑正在发生的是,视频实际上忽略了所有的分页,但正如我所说的,很难诊断,因为我们看不到所有的代码 你的代码很棒。。它工作得很好,看起来也很专业。我希望有一天我能像你一样编写代码。。非常感谢:如果上面的代码有帮助,请考虑投票并接受它作为正确的答案。
<?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;
?>