Jump to content

no mysql pagination question


rem

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.
Link to comment
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 :)

Link to comment
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...
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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? :) ???
Link to comment
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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.