-
Feb 12th, 2013, 02:48 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] First PHP code - help creating mutiple image pages
I am new to PHP (VB is my weapon of choice!) and I am trying to make an image viewer. I have a large number (say around 2000) of small images in a directory on my website. I would like to display around 25 on a web page with simple Next Page / Previous Page navigation. I don't need thumbnails as the images themselves are only 320 x 256. I don't know all the filenames and I don't know exactly how many images there are. I currently have PHP that displays ALL the images in the current directory. It works well but obviously is a bit slow to load with a large number of images:
Code:
<?php
//path to current directory
$directory = "./";
//get all files with a .jpg extension
$images = glob($directory . "*.jpg");
?>
<html>
<body bgcolor="#EEEEEE" style='font-family: Arial'>
<?php
echo("<p><b><font face='arial black,arial' size='6' color='#004488'>G4IJE SSTV Archive</font></b></p>");
echo("<p><a href='http://www.classicsstv.com/rxpics.htm'>Click here to return to the main SSTV pics page</a></p>");
foreach($images as $image)
{
echo("<img src=\"$image\" border='1' hspace='2' vspace='2'/>");
}
echo("<p><a href='http://www.classicsstv.com/rxpics.htm'>Click here to return to the main SSTV pics page</a></p>");
?>
</body>
</html>
Can someone point me in the right direction to convert the above code to display around 25 images at a time? I'm sure I can find out how to get the number of items in the array (count?) and use a For-Next rather than For-Each, and can probably manage to output a page of 25 images, but what functionality is needed to create a "Next" or "Previous" page? In other words, having generated a page of images, how does one use PHP to "refresh" the page with the next batch of images?
Thanks for your help.
-
Feb 12th, 2013, 03:39 PM
#2
Frenzied Member
Re: First PHP code - help creating mutiple image pages
The code below should at least get you started and give you an idea on how to approach the situation.
Basically you set up your pagesize and then figure out how many pages you'll have by dividing your image count by the
specified page count (25 in this case).
So what we do initially is try to get the $_POST value at 'currentpage'. This variable will be posted back to the same page
on button click so we can load the appropriate images.
once we know the page to load (current page) we can start to draw the images at that offset in the $images array.
as we echo the images, we have to make sure that the index is less than the count() of the $images array.
if we reach the end prematurely (Ex. last page only has 3 images) then simply break the loop.
at the end we'll echo our buttons that are used for the page navigation. We just loop from 1 to totalpages
and if we hit the currentpage, just echo a span instead (visual for user of what page they're on).
I used a form and simply use javascript to set the hidden field and call the .submit().
Hope this helps,
Justin
Code:
<?php
//path to current directory
$directory = "./";
//get all files with a .jpg extension
$images = glob($directory . "*.jpg");
$pageitemcount = 25;
$totalpages = intval(count($images)/$pageitemcount);
$totalpages=$totalpages==0?1:$totalpages;
$currentpage = !isset($_POST['currentpage'])?1:intval($_POST['currentpage']);
?>
<html>
<body bgcolor="#EEEEEE" style='font-family: Arial'>
<?php
echo("<p><b><font face='arial black,arial' size='6' color='#004488'>G4IJE SSTV Archive</font></b></p>");
echo("<p><a href='http://www.classicsstv.com/rxpics.htm'>Click here to return to the main SSTV pics page</a></p>");
$i = $currentpage>1?($currentpage * $pageitemcount)-1:0
for($i;$i < $pageitemcount; $i++){
if($i < count($images)){
echo(sprint("<img src='%s' border='1' hspace='2' vspace='2'/>",$images[$i]));
}
}
echo("<br />");
?>
<form name='pagenav' method="post" action="thispage.php" enctype='...' >
<?php
for($j=1;$j<=$totalpages;$j++){
if($j == $currentpage){
echo(sprint("<span>%d</span>",$j));
}
else{
echo(sprintf("<input type='button' onclick=\"loadselection('%d')\" value='%d' />",$j,$j));
}
}
echo("<input type="hidden" id='selectedpage' name='currentpage' />");
?>
</form>
<?php
echo("<br /><p><a href='http://www.classicsstv.com/rxpics.htm'>Click here to return to the main SSTV pics page</a></p>");
?>
<script type='text/javascript'>
function loadselection(value){
document.forms['pagenav'].currentpage.value = value;
document.forms['pagenav'].submit();
}
</script>
</body>
</html>
in progess...
Last edited by MonkOFox; Feb 12th, 2013 at 04:21 PM.
-
Feb 12th, 2013, 03:57 PM
#3
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
-
Feb 12th, 2013, 05:15 PM
#4
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
Many thanks for your help Justin. I'm getting an error: Parse error: syntax error, unexpected T_FOR at line 24, which is the line with for($i;$i < $pageitemcount; $i++){
Any ideas?
Paul.
Last edited by paulg4ije; Feb 12th, 2013 at 05:30 PM.
-
Feb 12th, 2013, 05:47 PM
#5
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
I think I have fixed the first error but I now have "Fatal error: Call to undefined function sprint()" at line 27, which is echo(sprint("<img src='%s' border='1' hspace='2' vspace='2'/>",$images[$i]));
Paul.
OK, I'm gradually working through the errors, and learning a bit of PHP as I go! I now have the first page working, but clicking on the buttons for page 2, page 3 etc just gives a blank page. I'm sure I can fix it tomorrow ... closing down now.
Many thanks for your help Justin.
Last edited by paulg4ije; Feb 12th, 2013 at 05:58 PM.
-
Feb 13th, 2013, 08:18 AM
#6
Frenzied Member
Re: First PHP code - help creating mutiple image pages
The reason you got the 'sprint' error is because it was supposed to be sprintf(), sorry : (.
also in the <form> opening tag, be sure and put 'multipart/form-data' in the quotes in stead of '...'.
I just didn't know off the top of my head the correct thing to put at the time.
Try that and see if the subsequent pages work. If not, echo out $currentpage and see if the
value is actually getting posted.
Justin
-
Feb 13th, 2013, 08:29 AM
#7
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
Thanks Justin. I'll check that 'multipart/form-data' bit tonight - I'm at work at the moment, on-site with a customer, watching 10GB of photos copy very slowly on to a memory stick ...
I worked out the sprintf error and also changed some double-quotes to singles (input type="hidden" ...)
Thanks again,
Paul.
-
Feb 13th, 2013, 09:17 AM
#8
Frenzied Member
Re: First PHP code - help creating mutiple image pages
No problem man. Let me know how it goes!
-
Feb 13th, 2013, 04:47 PM
#9
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
Still not quite working - first page OK but others are blank. If I echo $i and $pageitemcount just before for($i;$i < $pageitemcount; $i++){ I get 0 and 25 on the first page then 49 and 25 on page 2. This doesn't look quite right to me.
Thanks,
Paul.
EDIT: $currentpage is showing the correct page value.
Last edited by paulg4ije; Feb 13th, 2013 at 04:53 PM.
-
Feb 13th, 2013, 05:24 PM
#10
Thread Starter
Fanatic Member
Re: First PHP code - help creating mutiple image pages
I think I'm nearly there Justin. The crucial changes are:
Code:
$i = $currentpage>1 ? ($currentpage-1) * $pageitemcount : 0;
$ilimit = $i + 25;
for($i;$i < $ilimit; $i++){
I'm not getting the partial last page at the moment, but I expect I can work that out.
Thanks again for all your help. I couldn't have done it without you.
EDIT: $totalpages = (intval(count($images)/$pageitemcount))+1; now gives me the partial page at the end of the list.
Last edited by paulg4ije; Feb 13th, 2013 at 05:32 PM.
-
Feb 14th, 2013, 08:16 AM
#11
Frenzied Member
Re: First PHP code - help creating mutiple image pages
That's great! I'm glad everything is working out for you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|