Jump to content

[SOLVED] help w/ pagination w/o mysql db...


tbare

Recommended Posts

Ok... dunno how easy this is going to be, but i would like to set up a pagination for my website where all of the data is being called from a separate php file...

 

page that calls the file:

<table border="0" cellspacing="10" cellpadding="0" align="center">

<?php include("text_files/humor_video.php"); ?>

</table>

 

where humor video contains:

 

<?php

//new file
$file = "getamac-misprint.avi";
$title = "Mac Ad: Misprint";
$thumb = "getamac-misprint_tn.png";
$thumb_width = "200";
$thumb_height = "150";
$size = filesize("files/video/$file");

print "<tr><td>\n";
print "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='files/video/thumbs/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
print "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
print "<a href='files/video/$file'>download file</a> | file size: ";
include("text_files/filesize.php");
print "$size\n";
print "</td></tr>\n";


//new file
$file = "long_shot.avi";
$title = "Long Shot";
$thumb = "long_shot_tn.png";
$thumb_width = "200";
$thumb_height = "161";
$size = filesize("files/video/$file");

print "<tr><td>\n";
print "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='files/video/thumbs/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
print "<td><a href='humor_video_play.php?file=$file&title=$title'>$title : Not funny, but a heckuvah shot!</a><br>\n";
print "<a href='files/video/$file'>download file</a> | file size: ";
include("text_files/filesize.php");
print "$size\n";
print "</td></tr>\n";
?>

but a LOT more files (about 40 or so)... i would like to (eventually) be able to set it up so the user can specify how many to show on a page, but for now, i would like to show, say 10 per page... (i'll figure the rest out later).

 

I honestly have no idea where to start on this, so any direction would be greatly appreciated.

Link to comment
Share on other sites

Your program design makes this very difficult.  But I can think of a few ways to do it.

 

One way is to set a variable $num_files = 0 at the top of your include.  After displaying each new file, you increment $num_files by 1.  And around each file, you check

 

if ($num_files >= $offset && $num_files < $offset + $limit) {
  # display file
}

 

If you're willing to do some rewriting of your program, you can put all the html for each file in an array instead of printing them immediately.  Then you can just use

 

$display_array = array_slice($full_array, $offset, $limit);
foreach ($display_array as $html_output) {
  print $html_output;
}

Link to comment
Share on other sites

i'm always willing to re-write code if it makes it cleaner, faster, and of course, easier to read :)

 

however, i'm not very familiar w/ arrays. what would be the best way to put what i have there in an array? (i'm currently in the process of adding video duration using ffmpeg into the loop as well, but i can figure out how to add to)...actually... you helped me with that conversion, too :)

 

any more help would be greatly appreciated again!

Link to comment
Share on other sites

Here's a suggestion for how to represent your files so they're easier to deal with:

 

//Step 1 - make the file into a single array, so it can be dealt with as one unit
$file_data = array(
  'filename' => "getamac-misprint.avi",  // Note change from $file to $filename
  'title' => "Mac Ad: Misprint",
  'thumb' => "getamac-misprint_tn.png",
  'thumb_width' => "200",
  'thumb_height' => "150",
  'size' => filesize("files/video/{$file_data['filename']}"), // <-- Note change here
);

// Step 2 - Make a reusable function which can display ANY file with a single call

function display_file($file_data) {
  extract($file_data); // Convert array variables to full variables

  print "<tr><td>\n";
  print "<a href='humor_video_play.php?file=$filename&title=$title'>\n<img border='0' src='files/video/thumbs/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  print "<td><a href='humor_video_play.php?file=$filename&title=$title'>$title</a><br>\n";
  print "<a href='files/video/$filename'>download file</a> | file size: ";
  include("text_files/filesize.php");
  print "$size\n";
  print "</td></tr>\n";
}

// Step 3 - demonstrate re-usable function
display_file($file_data);

// Step 4 - Add this file's data to an array of file datas
$all_files[] = $file_data;

// Example - Iterate over full array (assuming you added many more files), displaying each file
foreach ($all_files as $file_data) {
  display_file($file_data);
}

 

The basic idea is to get all the file data as one unit so you can deal with it easily, and also to stop repeating your display code for each file (since it's identical each time).

Link to comment
Share on other sites

well, i got the array all set up and working just fine... i saw the tutorial on setting up pagination, but that's for a sql database... how would i set it up w/o the sql database?

 

http://www.wannafork.com/text_files/source.php is the source of the page i'm wanting to set up....

 

any help here would be greatly appreciated, either via your own help, or a link (everything i've found has to do w/ a sql database...

 

Thanks again!

Link to comment
Share on other sites

Try replacing

 

foreach($files as $file){
  print(printLinkRow($file));
}

with

function printPageNav($page,$lastPage)
{
   $nav = "<div style='text-align:center;'>";
   if ($page <= 1){
      $nav .= "First page   Previous page   ";
   } else {
      $nav .= "<a href='humor_video.php?p=1'>First page</a>  ";
      $nav .= "<a href='humor_video.php?p=".($page-1)."'>Previous page</a>   ";
   }
   if ($page >= $lastPage){
      $nav .= "Next page   Last page";
   } else {
      $nav .= "<a href='humor_video.php?p=".($page+1)."'>Next page</a>   ";
      $nav .= "<a href='humor_video.php?p=$lastPage'>Last page</a>";
   }
   $nav .= "</div>";
   return $nav;
}

$files_pages = ceil(count($files)/10);
$page = 1;
if (isset($_GET['p']) && is_numeric($_GET['p'])){
   $page=$_GET['p'];
}
if ($page > $files_pages){
   $page = $files_pages;
}
if ($page < 1){
   $page = 1;
}
$display_files = array_slice($files, ($page-1)*10, 10);

print(printPageNav($page,$files_pages));
print('<table>');
foreach($display_files as $file){
  print(printLinkRow($file));
}
print('</table>');
print(printPageNav($page,$files_pages));

 

Don't forget to put the duration and filesize for all your files (using the filesize function to automatically get the filesize). Also, browsers should by default display the thumbnail with its actual dimensions, so explicitly setting the height and width is probably unnecessary.

 

And you should really be using '&' instead of '&' in your link URLs.

Link to comment
Share on other sites

well, that almost worked... only problem is when i click "Next page" or "Last page" it goes on to display all the files again... (although, the first page did show the first 10 and that's it...)...

 

any other ideas? i'll look @ the code later... i have to go christmas shopping <sarcasm>HURRAY!!</sarcasm> now...

 

also, i'm looking more for a pagination that shows all available pages (almost exactly like this site does) where, if there's more than say, 7 pages, it shows something like:

First page ... [3] 4 5 6 7 ... 15

 

dunno how easy that is to do, but that's what i'm looking for..

 

Thanks again for the help :)

 

-T

Link to comment
Share on other sites

Try the following (this is the code from printLinkRow function definition downards):

 

function printLinkRow($file){
  $file_path = "files/video";
  $thumb_path = "$file_path/thumbs";
  $filez = $file['file'];
  $title = $file['title'];
  $thumb = $file['thumb'];

  $size = filesize("$file_path/$filez");
  include("text_files/filesize.php");

  list($thumb_width, $thumb_height) = getimagesize("$thumb_path/$thumb");
  include("text_files/video_info.php");

  $filez = preg_replace("/'/", "&#39;" , $filez);
  $title = preg_replace("/'/", "&#39;" , $title);
  $alt = $title;

  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$filez&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt='$alt'></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$filez&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$filez'>download file</a> | file size: ";
  $message .= "$size<br>\n";
  $message .= "duration : ";
  $message .= "$duration<br>\n";
  $message .= "</td></tr>\n";
  return($message);
}

function addToNav($addPage)
{
   global $page;
   global $nav;
   if ($addPage != $page){
      $nav .= "<a href='humor_video.php?p=".$addPage."'>".$addPage."</a>  ";
   }else{
      $nav .= "[".$addPage."]  ";
   }
}

function printPageNav()
{
   global $page;
   global $files_pages;
   global $nav;
   global $sidePages;
   $nav = "<div style='text-align:center;'>";
   $excerpt_start = $page-$sidePages;
   $excerpt_end = $page+$sidePages;
   if ($excerpt_start < 2){
      $excerpt_start = 2;
   }
   if ($excerpt_end > ($files_pages-1)){
      $excerpt_end = $files_pages-1;
   }
   addToNav(1);
   if ($excerpt_start != 2){
      $nav .= "...  ";
   }
   for ($i = $excerpt_start; $i <= $excerpt_end; $i++){
      addToNav($i);
   }
   if ($excerpt_end != ($files_pages-1)){
      $nav .= "...  ";
   }
   addToNav($files_pages);
   $nav .= "</div>";
   return $nav;
}

$sidePages = 4;
$itemsPerPage = 10;
$page = 1;
if (isset($_GET['p']) && is_numeric($_GET['p'])){
   $page=$_GET['p'];
}
$files_pages = ceil(count($files)/$itemsPerPage);
if ($page > $files_pages){
   $page = $files_pages;
}
if ($page < 1){
   $page = 1;
}
$display_files = array_slice($files, ($page-1)*$itemsPerPage, $itemsPerPage);
print(printPageNav());
print('<table>');
foreach($display_files as $file){
  print(printLinkRow($file));
}
print('</table>');
print(printPageNav());
?>  

 

$sidePages is the number of page numbers that you want to display on either side of the selected page in the navigation

$itemsPerPage is the number of items to be shown on each page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.