Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Pagination on a 'foreach' loop
Old 07-23-2008, 02:43 PM Pagination on a 'foreach' loop
SpottyDog's Avatar
Skilled Talker

Posts: 82
Trades: 0
Hey guys, been a long time since ive posted here but im after a little help.

I have a foreach loop running on my page displaying all images in a folder using the following code:
PHP Code:
   echo "<table width=\"50%\" border=\"0\" align=\"center\"> ";
   echo 
"<tr>";
   
$i=0;
foreach (
glob("events/$dire/*.jpg") as $image) {

   
$i++;
 
    echo 
"<td><div align=\"center\"><img class=\"photo\" src=\"$image\" alt=\"\"></div><div align=\"center\" class=\"style6\">Price: £30</div></td>"
if (
$i == "3") {
echo 
"</tr> <tr>";
$i="0";

}
echo 
"</tr> </table>"
Now some folders have more than 200 images so i need to be able to paginate them. Is there an easy way to do it? I dont really want to have to do it with a database as the folders content is always changing and i dont want to have to update the database each time.

Thanks.

Scott

Last edited by SpottyDog; 07-23-2008 at 02:45 PM..
SpottyDog is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-23-2008, 05:47 PM Re: Pagination on a 'foreach' loop
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I'm not an expert on glob, but I'm pretty sure it doesn't support an offset, so you'd have to create some kind of index of the files. Perhaps storing the first filename to display for each page of results and then echoing out files once you loop through the directory and get to that point...

Anyone else have a better suggestion?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 09:51 PM Re: Pagination on a 'foreach' loop
Super Talker

Posts: 116
Trades: 0
if the images are in a folder, then the script must be reading the folder.

use a function to count how many files in the function (google), and then do some math so you get the total number of pages with how many ever pix u want on the page. then just make it so it only displayes the pictures from x to y, where y can be calculated from x like x+20 or w/e u want to be on the page and x would just be like a get variable (?page=2).
__________________


Please login or register to view this content. Registration is FREE

nasaboy007 is offline
Reply With Quote
View Public Profile Visit nasaboy007's homepage!
 
Old 07-23-2008, 09:54 PM Re: Pagination on a 'foreach' loop
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
That's a better suggestion that mine -- uses less memory.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Pagination on a 'foreach' loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13544 seconds with 12 queries