dongtang4954 2015-09-07 11:07
浏览 43
已采纳

从“结束”或“第一”值[重复]之后的数据库元素的按钮上一次和下一次加载

This question already has an answer here:

I show one video per page from category and with buttons next/previous user can navigate to next or previous id in database. The problem is that when I am on the first video of that category the button previous is still there and I can go one id previous which is another category. Same is with button next. When user is on last video of that category the button for next is still there and can click on it which will load next id from database ( next category ).

 if(isset($_GET['video_id']) && is_numeric($_GET['video_id']) && isset($_GET['video_cat_id']) && is_numeric($_GET['video_cat_id'])){

    $video_id = $_GET['video_id'];
    $video_cat_id = $_GET['video_cat_id'];    

// get next picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id > :video_id ORDER BY video_id ASC LIMIT 1');
if($result){
    $result->execute(array(':video_id' => $video_id));
    //$result->execute(array(':video_cat_id' => $video_cat_id));
    if (($row = $result->fetch()) !== FALSE) {
       $next_id = $row['video_id'];
    }
}

// get previous picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id < :video_id ORDER BY video_id DESC LIMIT 1');
if($result){
    $result->execute(array(':video_id' => $video_id));
    //$result->execute(array(':video_cat_id' => $video_cat_id));
    if (($row = $result->fetch()) !== FALSE) {
       $prev_id = $row['video_id'];
    }
}

  $result = $pdo->prepare("SELECT * FROM video WHERE video_id= :video_id LIMIT 1");
  if ($result->execute(array(':video_id'=>$_GET['video_id']))) 
  {       

   $prev_button = (isset($prev_id) && $prev_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
   $next_button = (isset($next_id) && $next_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
   if($row = $result->fetch())
   {
           // video goes here

What I saw is that in the query SELECT * FROM video WHERE video_id= :video_id LIMIT 1 I didn't select the category.

What I try is to make the query like this

$result = $pdo->prepare("SELECT * FROM video WHERE video_id= :video_id and video_cat_id = :video_cat_id LIMIT 1");
$result->bindParam(':video_id', $_GET['video_id'], PDO::PARAM_INT);
$result->bindParam(':video_cat_id', $video_cat_id, PDO::PARAM_INT);   
if ($result->execute())    
{
          // video

But is loading me blank page. What can be the actual problem here?

UPDATE:

I make this

// get next picture id
$result = $pdo->prepare('SELECT video_id FROM video WHERE video_id > :video_id and video_cat_id = :video_cat_id ORDER BY video_id ASC LIMIT 1');
if($result){
   $result->bindParam(':video_id', $_GET['video_id'], PDO::PARAM_INT);
   $result->bindParam(':video_cat_id', $video_cat_id, PDO::PARAM_INT);
    $result->execute();
    if (($row = $result->fetch()) !== FALSE) {
       $next_id = $row['video_id'];
    }
}

same for previous picture - added video_cat_id in query. Then this:

  $prev_button = '<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
  $next_button = '<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
  if(isset($prev_id) && $prev_id > 0) { echo $prev_button; }
  if(isset($next_id) && $next_id > 0) { echo $next_button; } 
  if($row = $result->fetch())
  {

I've got the error:

PHP Parse error: syntax error, unexpected ':'

</div>
  • 写回答

1条回答 默认 最新

  • duaevb1511 2015-09-07 11:41
    关注

    Your "first" and "previous" sql queries should include and video_cat_id = :video_cat_id that way you won't be able to go back or forward into another category.

    Then, this code is not an "if" statement...

    $prev_button = (isset($prev_id) && $prev_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.html"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
    $next_button = (isset($next_id) && $next_id>0)?'<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.html"><i class="fa fa-arrow-right fa-3x"></i></a>':'';      
    if($row = $result->fetch())
    {
           // video goes here
    

    Define what your button should look like and the parameters it should use. like this...

    $prev_button = '<a href="vcat-'.$video_cat_id.'/video-'.$prev_id.'.php"><i class="fa fa-arrow-left fa-3x"></i></a>':'';
    $next_button = '<a href="vcat-'.$video_cat_id.'/video-'.$next_id.'.php"><i class="fa fa-arrow-right fa-3x"></i></a>':'';
    

    Then use an if statement to decide IF you should show them or not. Basically...
    If there is a previous video in the category - show the button.
    If there is a next video in the category - show the button.

    if(isset($prev_id) && $prev_id > 0) { echo $prev_button; }
    if(isset($next_id) && $next_id > 0) { echo $next_button; }     
    if($row = $result->fetch())
    {
           // video goes here
    

    Hope this helps.

    Happy Coding!

    Update:

    I completely missed this... Your buttons are redirecting to an .html page. An .html or.htm page cannot process php. You will need to change your pages to .php and change the code for $prev_button = and $next_button = to point to a .php page as I did above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题