0

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 ':'

Jason Paddle
  • 1,095
  • 1
  • 19
  • 37
  • Nothing more follows `PHP Parse error: syntax error, unexpected ':'` ? – Kuya Sep 07 '15 at 12:49
  • No, there is error on both rows `$prev_button` and `$next_button` at the end .. where is `':'';`... – Jason Paddle Sep 07 '15 at 12:56
  • Remove `:''` from the end of those two lines. OR is the colon supposed to part of the button? If so, then change `':'';` to `:';` on those two lines. – Kuya Sep 07 '15 at 13:03
  • Why `duplicate`? The thread Is not about this error @John. Please read the question.... Kuya i've removed them and again just blank space where must be video.. – Jason Paddle Sep 07 '15 at 14:00
  • I don't know, but there is an up and a reopen vote. Many times the reviewers also don't agree, and sometimes you have bad luck, imho this was the case now. BTW, you also can to vote for reopening your question, so we will need already 3 votes yet. – peterh Sep 07 '15 at 14:31
  • @peterh, thank's! I've already vote for reopen and already has 3 votes. – Jason Paddle Sep 08 '15 at 04:39

1 Answers1

1

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

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Kuya
  • 7,280
  • 4
  • 19
  • 31
  • Thank's for the answer. I get that part `Your "first" and "previous" sql queries should include and video_cat_id = :video_cat_id...` but I didn't get next part. – Jason Paddle Sep 07 '15 at 11:52
  • see my updated answer – Kuya Sep 07 '15 at 11:56
  • @JasonPaddle Do you need more help? – Kuya Sep 07 '15 at 12:03
  • please see my updated question – Jason Paddle Sep 07 '15 at 12:39
  • In this line `$result->bindParam(':video_cat_id', $video_cat_id, PDO::PARAM_INT);` ... Where did you declare the value for `$video_cat_id` ? – Kuya Sep 07 '15 at 12:42
  • In here: `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']; ...` You can see it in original question. – Jason Paddle Sep 07 '15 at 12:43
  • This is `pretty url`.. not the HMTL page. I make them via .htaccess. – Jason Paddle Sep 07 '15 at 14:02
  • I guess I got too much coding yesterday and after resting a little bit I saw what stupid mistake I've made.. this `and video_cat_id = :video_cat_id` should be `and vcat = :video_cat_id` since in database the row for category is `vcat` not `video_cat_id`.. Thank's for the help! Now everything work just fine! – Jason Paddle Sep 08 '15 at 04:57