-1

I'm working on a simple vlog site project and I'm trying to show only 20 video thumbnail per page and I've wrote this code in the index to divide the videos to multiple pages and then pagination them ... the problem is that it shows the same first video's thumbnail 20 times per page for infinity pages.

I really need help with this code

<?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>") ; }   

        } ?>
njasi
  • 126
  • 8
Alex36
  • 3
  • 1

1 Answers1

0

A few tips before we get to the answer proper:

  1. Don't use mysql_*. That family of functions is now deprecated and support will be dropped in future versions of PHP. For code longevity, consider using MySQLi or PDO.
  2. Use is_numeric() to check if a string has a numeric value. Using preg_match() is very load heavy for such a simple task.
  3. Try to avoid using SELECT * inside of MySQL queries. Very rarely do you need everything from the table so fetching all fields for rows is very inefficient (especially if you're not using indexes optimally).

That having been said, I've taken some time to rewrite your code following the practices I've preached above. Below is the modified code, and underneath that an explanation of what was wrong:


Update db.php as follows:

<?php

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

?>

Now for your main file:

<?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;

?>

The problem with your original code is a little hard to determine since you do not provide the contents of VideosApi.php, which is where I assume you define Videos_Get(). Anyway, what I suspect is happening, is that Videos_Get() is actually ignoring all of your pagination, but as I say, it's difficult to diagnose because we can't see all of your code!

Community
  • 1
  • 1
BenM
  • 52,573
  • 26
  • 113
  • 168
  • Your code is great .. it works well and also looks very professional I hope one day I can code just like you .. thanks a lot :D – Alex36 Jan 21 '16 at 21:42
  • If the code above helped, please consider upvoting and accepting it as the correct answer. – BenM Jan 22 '16 at 09:54