Jump to content

[SOLVED] Storing/displaying html links in an array? (or perhaps a better way?)


Recommended Posts

Okay, I'm not sure if this is the right way to do this, or that it's even possible.

 

The situation is that I am making image gallery pages with 100+ links/images each. I want to display about 15-20 per page, and have a small numerical link bar that will let you select between the pages.

 

For example, say I had a page of 100 image links with thumbnails. I don't want to display all of them at the same time, so I'd rather it just show 15 of those to the user at a time, and let them select between pages with a small linkbar that looks like: "Page 1 2 3 4 5 6" having those all as links.

 

I hope this makes sense, because I'm pretty bad at explaining myself sometimes. =p

 

What is the best way to go about this? Would an array work, or is there a better way? The site isn't uber-complicated or anything, so the easiest way to do this is probably best. =p I just want to be able to add/update the galleries all the time and have them separated by pages automatically rather than hard coding them all.

 

Thanks!

Well, actually, I'm uploading my images to an image hosting service that automatically generates a list of links to the images in your galleries for you, like:

 

<a href=http://imagehost.com/img.php?id=12345><img src=http://imagehost.com/thumb/12345.jpeg></a>

<a href=http://imagehost.com/img.php?id=23456><img src=http://imagehost.com/thumb/23456.jpeg></a>

...

etc

 

So they're not really in a database or anything, just a long list of links. I just want to be able to display them in my own simple gallery on my website in a way that's easy for people to browse.

 

Is pagination doable without having a database?

The simplest way I think is to just read from a directory.  Make a directory called "gallery" or whatever you want and then use opendir, readdir, store them into an array and then sort them.  I use _small at the end of each thumbnail to determine that it is the thumbnail.  Heres a start: 

 

if ($_GET[pg]) { //if pg is passed

    $curpg = $_GET[pg]; //set var

} else {

    $curpg = 1; //default to page 1

}

$dir = $_SERVER['DOCUMENT_ROOT'] . '/gallery'; //gallery directory

$dirHandle = opendir($dir); //open directory

while ($file = readdir($dirHandle)) { //read directory

    if(!is_dir($file) AND strpos($file, '_small')>0 ) { //don't read directories or files with _small in the filename

        $files[] = $file; //array for all the pictures

    }

}

closedir($dirHandle); //close directory

asort($files); //sort the files

$numpics = count($files); //get the number of ipictures

$numpages = ceil($numpics/20); //get the number of pages if 20 per page

$startpic = $curpg * 20 - 19; // get the picture to start with

 

Then list your pictures how you want using that.

I'd probably create a file that contains each of your links separated by a newline or something, then read it into an array using file(), if you don't want to use a database or host your own files.

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.