Jump to content

Archived

This topic is now archived and is closed to further replies.

rem

no mysql pagination question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
yeah something like:
$a=0;
while (($a<=10)&&(there are images in the folder)){
$a++;
grab an image & print to screen;
}

Share this post


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

Share this post


Link to post
Share on other sites
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: [url=http://www.phpfreaks.com/tutorial_cat/25/Page-Number--Pagination.php]http://www.phpfreaks.com/tutorial_cat/25/Page-Number--Pagination.php[/url]

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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!

[code]
<?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>";
?>
[/code]

Share this post


Link to post
Share on other sites
i got it, i got it!!!! :)

[code]
<?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++;
}
?>
[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=102578.msg407586#msg407586 date=1154468379]
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?
[/quote]

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? :) ???

Share this post


Link to post
Share on other sites
something like this.
[code]for($i=10;$i<21;$i++)
{
  echo $array[$i];
}[/code]

or take a look at array_chunk or array_slice

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.