Jump to content


Photo

no mysql pagination question


  • Please log in to reply
13 replies to this topic

#1 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 02:28 PM

Dear all,

I'm trying to develop a small photo gallery in PHP, without using MySQL. The images are taken from within a folder then displayed on the front end. This is something I already managed to achieve and I'm very pleased by the results but the downside is when have large amounts of pictures within the base folder... My page is loaded with tones of thumbs and have to scroll miles to get through it. Could you point me through an easy method of somehow paginating the results and organize them a bit?

Thanks a lot!

Regards,
Rem.

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 01 August 2006 - 02:39 PM

I suppose depending on how you name your photos in the folder.. if you were to do them by numbers and only select 1 to 10 (or whatever) in your loop when you go through the folder, and then if they click on a link it will show 11 to 20?

Code wise, I'm not sure.  I am sure it can be done though but it would be having to control the loop that loads the contents of the folder and only displaying the amount you want.

Get the total amount of items in the directory.  Then divide that by how many you want per page.  Keep a variable of what page the user is on.  If you take a peek at the tutorials on the PHP/MySQL pagnation here in PHPFreaks I'm sure it can be modified to work with folders and files instead of a database :)



#3 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 02:47 PM

thanks for the reply...
the images are by names. the name is uppercased and the .ext is dropped.
i already looked over the pagination tutorials here but don't have a clue how to work with files within a folder :D

thanks again anyway, I'll try dig some other resources as well...

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 01 August 2006 - 04:17 PM

How are you pulling the image files onto the html page?  I suspect you are using a loop of some sort.  If that is the case then you might just amend the loop so it just pulls 10 images, and sets the 11th as a "next" link with the image title in the url.  Then when clicked the page would look for the image title in the file directory and would pick ten more photos starting from that photo.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 01 August 2006 - 04:25 PM

yeah something like:
$a=0;
while (($a<=10)&&(there are images in the folder)){
$a++;
grab an image & print to screen;
}
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#6 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 04:30 PM

thanks a lot guys... really apreciate it :)
i'll give that a try! is so cool to have some stuff to get me started...

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 01 August 2006 - 05:04 PM

A lot of the information you get from the MySQL pagination tutorials around the web will still apply. Your source of data only differs. You'll still calculate how many results you have in total, how many you want to print out on each page, and you want to provide links to the other results.

I would highly recommend reading some of those tutorials to get started, negating anything that is database related and replacing it with file functions.

Here is a good one to start with: http://www.phpfreaks...-Pagination.php

#8 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 06:03 PM

thanks a lot, any resource is welcomed. next time, if i'll be able to output my gallery, i will not bother you guys anymore, instead maybe i'll be able to help other php newbies like i am now :)

#9 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 08:21 PM

unfortunately i'm kinda forced to come back here guys...
the tutorial HeyRay2 pointed me to was great, helped me to understand the pagination process but still, even I managed to get this done with, I'm stuck to the fetching images from x to y stuff from within the folder.

here's my code, could you give me a hint, pretty please?
thanks a mil!

<?php
$dir = 'home/rem/www/images';
$files = @scandir($dir);
$num_of_files = count($files) - 2;

if(!isset($_GET['page']))
{
    $page = 1;
} 
else 
{
    $page = $_GET['page'];
} 

$max_results = 8;
$from = (($page * $max_results) - $max_results);  

if ($handle = opendir($dir))
{
	echo "<div id='gallery'>\n\n";
	while (false !== ($file = readdir($handle))) // this is where i got stuck and can't figure it out... :(
	{
		if ($file != "." && $file != "..")
		{
			echo "<div class='tile'><img src='images/" . $file . "' border='0' width='80' height='80' />\n";
			$title = str_replace('.jpg','',$file);
			$title = strtoupper(str_replace('_',' ',$title));
			echo "<span>" . $title . "</span>\n</div>\n\n";
		}
	}
	echo "<br clear='all' />\n\n</div>";
}

$total_pages = ceil($num_of_files/$max_results);

echo "<div style='padding: 5px; margin: 5px;'>";
if($page > 1)
{
    $prev = ($page - 1);
    echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $prev . "'>&laquo;Prev</a> ";
}

for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
    {
        echo $i . "&nbsp;" ;
    } 
    else 
    {
        echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $i . "'>" . $i . "</a> ";
    }
}

if($page < $total_pages)
{
    $next = ($page + 1);
    echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $next . "'>Next&raquo;</a>";
} 
echo "</div>";
?>


#10 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 09:05 PM

i got it, i got it!!!! :)

<?php 
$max_results = 3;
$from = (($page * $max_results) - $max_results);  
$to = $from + $max_results;
$x = -1; 

	while (false !== ($file = readdir($handle)))
	{
		if ($file != "." && $file != ".." && $from < $x && $x <= $to)
		{
			echo "<div class='tile'><img src='images/" . $file . "' border='0' width='80' height='80' />\n";
			$title = str_replace('.jpg','',$file);
			$title = strtoupper(str_replace('_',' ',$title));
			echo "<span>" .  $x . "-" . $title . "</span>\n</div>\n\n";
		}
		$x++;
	}
?>


#11 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 09:39 PM

glad that you got it :) may i suggest getting the filenames into an array with scandir, and then loop through the appropriate index of that array?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#12 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 09:45 PM

glad that you got it :) may i suggest getting the filenames into an array with scandir, and then loop through the appropriate index of that array?


i think I'll have to use other function than scandire since my host is still using php4 (just realised it's a php5 function) ...
so, I'll have to store them into an array anyhow :)

and how should i "loop through the appropiate index of that array", anyway? :) ???

#13 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 10:20 PM

something like this.
for($i=10;$i<21;$i++)
{
   echo $array[$i];
}

or take a look at array_chunk or array_slice
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#14 rem

rem
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 August 2006 - 10:28 PM

thanks a lot guys!!! your resources and directions had been very helpful!!
i managed to fire up my own gallery :) from now on... all i gotta do is tweak it here and there and imporve it!

best wishes! :)


P.S. please consider MoneyBookers for donations too. Where I come from, PayPal doesn't work...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users