Page 1 of 2
php pagination
Posted: Sun Jan 03, 2010 3:42 am
by Mysoogal
can anybody teach me how to add pagination to a php directory code that does not use mysql,
code i'm working on
Code: Select all
<?php
$path = "./dataStorage";
$dir_handle = @opendir($path) or die("Unable to open folder");
while (false !== ($file = readdir($dir_handle))) {
if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;
echo "
<a href=\"\" onclick=\"player.sendEvent('LOAD', 'dataStorageVideo/$file.flv'); return false;\" class=\"addToolTip\" title=\"<div id='ToolTipTextWrap' > File Information</div><p>dataStorage/$file</p>\"> <img class=\"thumbnail\" src=\"./dataStorage/$file\" alt=\"./dataStorage/$file\" style=\"opacity:0.4;filter:alpha(opacity=90)\" onmouseover=\"this.style.opacity=1;this.filters.alpha.opacity=100\" onmouseout=\"this.style.opacity=0.4;this.filters.alpha.opacity=91\" /> " ;
}
closedir($dir_handle);
?>

Re: php pagination
Posted: Sun Jan 03, 2010 7:14 pm
by Neo
I have written a basic code for you. You need to beautify it as required. For example, adding button images for navigation, add styles, etc...
Code: Select all
<?php
// File list pagination. Written by Neo, ROBOT.LK Labs
function getFileList ($folder){
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($folder);
// keep going until all files in directory are read
while ($file = readdir($handler)) {
sort($results, SORT_NUMERIC);
// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..')
$results[] = $file;
}
// close the handler
closedir($handler);
return $results;
}
$folder = 'C:/Windows'; // Change this to your folder
$filearray = (getFileList($folder));
$total_items = count($filearray);
$items_per_page = 10;
$total_pages = ceil($total_items/$items_per_page);
$page = intval($_GET['page']);
if((!$page) || (is_numeric($page) == false) || $page<=0 || $page > $total_pages ) {
$page = 1;
}
$start_item = $items_per_page * ($page-1);
$end_item = min($start_item + $items_per_page-1, $total_items-1);
// List 10 files
for ($i = $start_item; $i <= $end_item; $i++){
echo $filearray[$i] . "</br>";
}
echo "</br>";
echo "</br>";
// Navigation controls
if ($total_pages > 1){
if ($page == 1){
echo "< <<";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
else if ($page == $total_pages){
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo ">> >";
}
else{
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
}
?>
Re: php pagination
Posted: Sun Jan 03, 2010 11:46 pm
by Mysoogal
thank you neo, but im not sure where to add this code below i need the output formated as it was in echo " blaa blaa " in my code,
i have my setup as this
./dataStorage/$file this gets my thumbnails
./dataStorageVideo/$file.flv this get my flv
from Echo " " ;
Code: Select all
<a href="\" onclick=\"player.sendEvent('LOAD', 'dataStorageVideo/$file.flv'); return false;\" class=\"addToolTip\" title=\"<div id='ToolTipTextWrap' > File Information</div><p>dataStorage/$file</p>\"> <img class=\"thumbnail\" src=\"./dataStorage/$file\" alt=\"./dataStorage/$file\" style=\"opacity:0.4;filter:alpha(opacity=90)\" onmouseover=\"this.style.opacity=1;this.filters.alpha.opacity=100\" onmouseout=\"this.style.opacity=0.4;this.filters.alpha.opacity=91\" />
sorry i didnt add the full code, you might see my problem, i'm not sure how under your new code, to add the old code from echo " " ;
where would i first look to add my old Echo " " code so output is normal right now it only gives me list of files on my www folder,
very confusing
Re: php pagination
Posted: Sun Jan 03, 2010 11:47 pm
by Mysoogal
no idea didnt allow me to post full code
Code: Select all
<?php require('_drawrating.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Silverglow</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.thumbnail
{
float:left;
width:100px;
height:60px;
margin:2px;
}
.text_line
{
clear:both;
margin-bottom:2px;
}
</style>
<script type="text/javascript" language="javascript" src="js/behavior.js"></script>
<script type="text/javascript" language="javascript" src="js/rating.js"></script>
<link rel="stylesheet" type="text/css" href="css/rating.css" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
/* ----- Uncomment the global selector below to over-ride the default margin and padding added to all tags ----- */
*{padding: 0; margin: 0;}
/* ----- global default/initial styles ----- */
body {background-color:#fff; margin: 10px; padding: 0;}
/* ----- base default font size, type, and line height ----- */
html body{font: 62.5%/1.4em Arial, Helvetica, sans-serif;color:#333333}
html>body{font: 62.5%/1.4em Arial, Helvetica, sans-serif;color:#333333}
/* ----- add selectors here for font sizing ----- */
p, table, ul, dl {font-size: 1.2em}
caption {font-size: 1.4em}
h2 {font-size: 1.6em}
p {line-height:1.4em;margin:10px 0;}
/* ----- base links ----- */
a:link {color: #CC6633;}
a:visited {color: #CC6633;}
a:hover {color: #999966;}
a:active {color: #CC6633;}
a:focus{color: #CC6633;}
/* ----- tool tip specific styles ----- */
#theToolTip{
position: absolute;
left: -300px;
width: 200px;
border: 2px solid #C0C09F;
padding: 6px 0px 0px 10px;
background-color: #FFFFCC;
visibility: hidden;
z-index: 100;
/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
filter: progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135,strength=4);
}
#ToolTipPointer{
position:absolute;
left: -300px;
z-index: 101;
visibility: hidden;
}
#theToolTip p{
font-size: 1.1em;
color: #333333;
line-height:1.4em;
margin-right:10px;
margin-top:0;
}
#ToolTipTextWrap {
font-weight:bold;
font-size: 1.2em;
color: #592C16;
margin-right:10px;
}
</style>
<?php
session_start();
function createForm(){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center">
<tr><td colspan="2">Please eneter a nickname !</td></tr>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
</td></tr>
</table>
</form>
<?php
}
if (isset($_GET['u'])){
unset($_SESSION['nickname']);
}
// Process login info
if (isset($_POST['submitBtn'])){
$name = isset($_POST['name']) ? $_POST['name'] : "Unnamed";
$_SESSION['nickname'] = $name;
}
$nickname = isset($_SESSION['nickname']) ? $_SESSION['nickname'] : "Hidden";
?>
</head>
<body>
<div class="holder">
<div class="top_curve"></div>
<div class="left_shade1"></div>
<div class="center_content">
<div class="header">
<div style="float:left"><img src="images/logo.gif" alt="" width="37" height="36" align="left" />
<h1 style="padding-top:5px;"> mysoogalbox</h1>
</div>
<div class="navigation"><a href="#" class="nav">home</a> | <a href="#" class="nav">about</a> | <a href="#" class="nav">services</a> |<a href="#" class="nav"> upload</a></div>
</div>
<div class="focus_block">
<div class="block_left"></div>
<div class="right_content">
<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="440" height="315">
<param name="movie" value="player.swf" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="flashvars" value="file=video.flv&image=preview.jpg" />
<embed
type="application/x-shockwave-flash"
id="player2"
name="player2"
src="player.swf"
width="440"
height="315"
allowscriptaccess="always"
allowfullscreen="true"
flashvars="file=video.flv&image=preview.jpg"
/>
</object>
</div>
<br clear="all" />
</div>
<div class="left_content">
<h4>message box </h4>
<script language="javascript" type="text/javascript">
<!--
var httpObject = null;
var link = "";
var timerID = 0;
var nickName = "<?php echo $nickname; ?>";
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("result");
objDiv.innerHTML += response;
objDiv.scrollTop = objDiv.scrollHeight;
var inpObj = document.getElementById("msg");
inpObj.value = "";
inpObj.focus();
}
}
// Change the value of the outputText field
function setAll(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("result");
objDiv.innerHTML = response;
objDiv.scrollTop = objDiv.scrollHeight;
}
}
// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
link = "message.php?nick="+nickName+"&msg="+document.getElementById('msg').value;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setOutput;
httpObject.send(null);
}
}
// Implement business logic
function doReload(){
httpObject = getHTTPObject();
var randomnumber=Math.floor(Math.random()*10000);
if (httpObject != null) {
link = "message.php?all=1&rnd="+randomnumber;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setAll;
httpObject.send(null);
}
}
function UpdateTimer() {
doReload();
timerID = setTimeout("UpdateTimer()", 5000);
}
function keypressed(e){
if(e.keyCode=='13'){
doWork();
}
}
//-->
</script>
<?php
if (!isset($_SESSION['nickname']) ){
createForm();
} else {
$name = isset($_POST['name']) ? $_POST['name'] : "Unnamed";
$_SESSION['nickname'] = $name;
?>
<div id="result">
<?php
$data = file("msg.html");
foreach ($data as $line) {
echo $line;
}
?>
</div>
<div id="sender" onkeyup="keypressed(event);">
<a href="#" class="addToolTip" title="<div id='ToolTipTextWrap'>Please pick simple username</div><p>and don't forget to respect other users thank you</p>">Message Box</a> <input class="tb10" name="msg" size="30" id="msg" />
<button onclick="doWork();">Send</button>
</div>
<?php
}
?>
<br>
<h4>how many stars for bleach 251 ?</h4>
<br />
<?php echo rating_bar('bleach','7'); ?>
<br />
Fusce consectetuer blandit eros. Donec quis nisi et nulla elementum porta. Maecenas elementum pede. Ut facilisis tempor libero. Maecenas blandit, tellus nec laoreet nonummy, turpis risus tempor massa, ac dictum tellus urna consequat nibh. </div>
<div class="right_content">
<h1>Welcome to Silver Glow </h1>
<br><div class="hr"></div>
<?php
// File list pagination. Written by Neo, ROBOT.LK Labs
function getFileList ($folder){
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($folder);
// keep going until all files in directory are read
while ($file = readdir($handler)) {
sort($results, SORT_NUMERIC);
// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..')
$results[] = $file;
}
// close the handler
closedir($handler);
return $results;
}
$folder = 'C:/ProgramData/Wamp/www'; // Change this to your folder
$filearray = (getFileList($folder));
$total_items = count($filearray);
$items_per_page = 10;
$total_pages = ceil($total_items/$items_per_page);
$page = intval($_GET['page']);
if((!$page) || (is_numeric($page) == false) || $page<=0 || $page > $total_pages ) {
$page = 1;
}
$start_item = $items_per_page * ($page-1);
$end_item = min($start_item + $items_per_page-1, $total_items-1);
// List 10 files
for ($i = $start_item; $i <= $end_item; $i++){
echo $filearray[$i] . "</br>";
}
echo "</br>";
echo "</br>";
// Navigation controls
if ($total_pages > 1){
if ($page == 1){
echo "< <<";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
else if ($page == $total_pages){
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo ">> >";
}
else{
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
}
?>
</div>
</div>
<div class="right_shade1"></div>
<br clear="all" />
</div>
<div class="bottom_curve"></div>
<div class="footer">
</div>
</body>
</html>
<div class="back" style="width:800px; padding:10px 0 0 0;"><p></a></p></div>
<script language="JavaScript" type="text/JavaScript">
//Edit the informaiton between the quotes below with the path to your image.
var imagePath = "images/tooltiparrow.gif";
function addwarning(){
var thealinks = document.getElementsByTagName("a");
if (!thealinks) { return; }
for(var x=0;x!=thealinks.length;x++){
if(thealinks[x].className == "addToolTip"){
thealinks[x].setAttribute("tooltiptext",thealinks[x].title);
thealinks[x].removeAttribute("title");
thealinks[x].onmouseover=function gomouseover(){ddrivetip(this.getAttribute("tooltiptext"))};
thealinks[x].onmouseout=function gomouseout(){hideddrivetip();};
}
}
}
var offsetfromcursorX=-7; //Customize x offset of tooltip
var offsetfromcursorY=23; //Customize y offset of tooltip
var offsetdivfrompointerX=13; //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=13; //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).
document.write('<div id="theToolTip"></div>'); //write out tooltip DIV
document.write('<img id="ToolTipPointer" src="'+imagePath+'">'); //write out pointer image
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
if (ie||ns6) {
var tipobj=document.all? document.all["theToolTip"] : document.getElementById? document.getElementById("theToolTip") : "";
}
var pointerobj=document.all? document.all["ToolTipPointer"] : document.getElementById? document.getElementById("ToolTipPointer") : "";
function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!=="undefined") {tipobj.style.width=thewidth+"px";}
if (typeof thecolor!=="undefined" && thecolor!=="") {tipobj.style.backgroundColor=thecolor;}
tipobj.innerHTML=thetext;
enabletip=true;
return false;
}
}
function positiontip(e){
if (enabletip){
var nondefaultpos=false;
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;
var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000;
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px";
nondefaultpos=true;
}
else if (curX<leftedge)
{tipobj.style.left="5px";}
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px";
pointerobj.style.left=curX+offsetfromcursorX+"px";
}
//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px";
nondefaultpos=true;
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px";
pointerobj.style.top=curY+offsetfromcursorY+"px";
}
tipobj.style.visibility="visible";
if (!nondefaultpos) {pointerobj.style.visibility="visible";}
else{
pointerobj.style.visibility="hidden";}
}
}
function hideddrivetip(){
if (ns6||ie){
enabletip=false;
tipobj.style.visibility="hidden";
pointerobj.style.visibility="hidden";
tipobj.style.left="-1000px";
tipobj.style.backgroundColor='';
tipobj.style.width='300';
}
}
document.onmousemove=positiontip;
addwarning();
</script>
Re: php pagination
Posted: Mon Jan 04, 2010 12:03 am
by Neo
Find following code.
Code: Select all
// List 10 files
for ($i = $start_item; $i <= $end_item; $i++){
echo $filearray[$i] . "</br>";
}
Instead of
echo $filearray[$i] . "</br>";, put your echo code. You may have to do a change around $file with $filearray[$i].
Re: php pagination
Posted: Mon Jan 04, 2010 12:32 am
by Mysoogal
neo, many thousands of thank yous
its working great just had to change little thing under $folder so it points to the thumbs
Code: Select all
$folder = 'C:/ProgramData/Wamp/www/dataStorage'; // Change this to your folder
thank you NEO ! again
again lol i just need to find where this is coming from

Re: php pagination
Posted: Mon Jan 04, 2010 12:42 am
by Neo
That's the simple navigation buttons I have added to browse through the contents on multiple pages.
Code: Select all
// Navigation controls
if ($total_pages > 1){
if ($page == 1){
echo "< <<";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
else if ($page == $total_pages){
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo ">> >";
}
else{
echo "<a href=\"?page=1\" title=\"First Page\"><</a> ";
echo "<a href=\"?page=". ($page-1) . "\" title=\"Prev Page\"><<</a>";
echo " " . $page . " ";
echo "<a href=\"?page=". ($page+1) . "\" title=\"Next Page\">>></a> ";
echo "<a href=\"?page=". $total_pages . "\" title=\"Last Page\">></a>";
}
}
I have set items per page to 10. It seems you need to adjust it to fit your frame.
Re: php pagination
Posted: Mon Jan 04, 2010 12:56 am
by Mysoogal
thanks yes, already figured that part out
i don't have enough of videos , i added about 8 so <<>>> stays below thumbs
would look great with lots of video clips like 1000 clips the <<<>>> would stay below thumbs always i would assume. anyways not big problem its looking great and everything working
now time to hunt down remote upload script

Re: php pagination
Posted: Mon Jan 04, 2010 4:06 pm
by Mysoogal
hi again
i've got this error but the weird thing is when its in this url example.com/?page=2 it doesnt show error what could this error be
Code: Select all
Notice: Undefined index: page in C:\ProgramData\Wamp\www\index.php on line 318

Re: php pagination
Posted: Mon Jan 04, 2010 4:12 pm
by Mysoogal
always finding answers after i post
inside php.ini had to Suppress Notice warnings
Code: Select all
error_reporting = E_ALL & ~E_NOTICE
or using php
Code: Select all
<?php error_reporting (E_ALL ^ E_NOTICE); ?>