tbare Posted December 6, 2007 Share Posted December 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2007 Share Posted December 7, 2007 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; } Quote Link to comment Share on other sites More sharing options...
tbare Posted December 7, 2007 Author Share Posted December 7, 2007 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! Quote Link to comment Share on other sites More sharing options...
btherl Posted December 7, 2007 Share Posted December 7, 2007 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). Quote Link to comment Share on other sites More sharing options...
tbare Posted December 7, 2007 Author Share Posted December 7, 2007 makes sense, but (again, sorry for the ignorance here... i'm trying to learn), what steps would i have to repeat? 1-4 after step 4, and then call the foreach statement? Quote Link to comment Share on other sites More sharing options...
tbare Posted December 8, 2007 Author Share Posted December 8, 2007 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! Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted December 8, 2007 Share Posted December 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
tbare Posted December 8, 2007 Author Share Posted December 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted December 8, 2007 Share Posted December 8, 2007 Worked fine for me (apart from errors about missing files!) : Page: http://jacksonmj.co.uk/phpfreaks/humor_video.php Source: http://jacksonmj.co.uk/phpfreaks/humor_video.txt Did you copy my code in correctly? Quote Link to comment Share on other sites More sharing options...
tbare Posted December 8, 2007 Author Share Posted December 8, 2007 copied and pasted... page:http://www.wannafork.com/humor_video2.php source: http://www.wannafork.com/text_files/source.php Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted December 8, 2007 Share Posted December 8, 2007 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("/'/", "'" , $filez); $title = preg_replace("/'/", "'" , $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. Quote Link to comment Share on other sites More sharing options...
tbare Posted December 8, 2007 Author Share Posted December 8, 2007 hmmm.. once again... when i click on page 2, i lose all pagination, and it just goes to showing them all again... Quote Link to comment Share on other sites More sharing options...
tbare Posted December 8, 2007 Author Share Posted December 8, 2007 ok figured out the problem.. (don't hate me) it was going back to humor_video.php instead of humor_video2.php thanks! works fine now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.