Jump to content

PHP image from folder next page help


DCI

Recommended Posts

Here is the code i have.

 

<? $album=$_GET['album']; ?>

<link rel="stylesheet" href="light box/css/lightbox.css" type="text/css" media="screen" />

 

<script src="light box/js/prototype.js" type="text/javascript"></script>

<script src="light box/js/scriptaculous.js?load=effects,bui… type="text/javascript"></script>

<script src="light box/js/lightbox.js" type="text/javascript"></script>

<?

$dirname = "uploader/photo/$album/";

$images = scandir($dirname);

$ignore = Array(".", "..");

foreach($images as $curimg){

if(!in_array($curimg, $ignore)) {

echo "

 

<a href=\"uploader/photo/$album/$curimg\">

 

<a href=\"uploader/photo/$album/$curimg\" rel=\"lightbox [album]\"><img src=\"uploader/photo/$album/$curimg\" height=\"100\"/></a>";

};

};

?>

 

 

______________________________________…

 

Here is what i would like

 

right now that echos all images in the directory (folder)

I would like it to echo 20 images and then after 20 images i would like it to creat a next button and then display the next 20 images.

 

If you could help that would be great thank you!

 

NOTE: I AM NOT USING A MYSQL DB TO GET THESE IMAGES.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

What you're going to want to do is before the code that echos, get the number of files in your directory. Take that number, divide by 20 and you'll get the number of pages you need to have access to (the old <- 1 2 3 4 -> type of nav). Then, you'll have to make it so that each link will have an action/id assigned to it, so that when the page loads, it checks which portion is should load. Finally, when you start to show the pictures, you can use the number you're on to tell it where to start the array pointer, and set up a counter so that it will only loop through 20 images.

Link to comment
Share on other sites

What you're going to want to do is before the code that echos, get the number of files in your directory. Take that number, divide by 20 and you'll get the number of pages you need to have access to (the old <- 1 2 3 4 -> type of nav). Then, you'll have to make it so that each link will have an action/id assigned to it, so that when the page loads, it checks which portion is should load. Finally, when you start to show the pictures, you can use the number you're on to tell it where to start the array pointer, and set up a counter so that it will only loop through 20 images.

 

 


 

Hey thank you for the input here is what i got but it does not work could you help me out, Im kinda new to php teaching my self so sorry if its a simple error

 

 

<?

 

!empty($_GET['start']) ? $start = $_GET['start'] : $start = 0;

if($start >= 20) $prev = $start - 20;

else $prev = 0;

$end = $start + 20;

$next = $end + 1;

$i = 0;

foreach($images as $curimg){

if(!in_array($curimg, $ignore)) {

 

if(($i >= $start) && ($i <= $end)) {

echo "

 

<a href=\"uploader/photo/$album/$curimg\">

 

<a href=\"uploader/photo/$album/$curimg\" rel=\"lightbox [album]\"><img src=\"uploader/photo/$album/$curimg\" height=\"100\"/></a>";

 

$i++;

}

}

}

echo "<a href='?start=$prev'>Previous Page</a> | <a href='?start=$next'>Next Page</a>";

 

?>

 

 

 


 

It does not display images if i put this in $dirname = "uploader/photo/$album/";

$images = scandir($dirname); It echos the corect amount but the next buttons do not display the next 20?

 

There is 45 images in the folder too

Link to comment
Share on other sites

Ok, here's some updated code for you

 

<?
    $album=basename($_GET['album']);
    $dirname = "uploader/photo/$album/";
    $images = scandir($dirname);
    $ignore = Array(".", "..");
    
    $start = (!empty($_GET['start']) ? $_GET['start'] : 0);
    $prev = ($start >= 20 ? $start - 20 : 0);
    $end = $start + 20;
    $i = 0;
       
    foreach($images as $curimg){
        if(in_array($curimg, $ignore)) continue;
        
        if(($i >= $start) && ($i < $end)) {
            echo '<a href="uploader/photo/'.$album.'/'.$curimg.'" rel="lightbox">'.$curimg.'</a>   '; 
        }      
        $i++;
    }
    echo '<br /><br /><a href="?album='.$album.'&start='.$prev.'">Previous Page</a> | <a href="?album='.$album.'&start='.$end.'">Next Page</a>';
?>

 

A few notes:

When you use the inline conditional switch ($start variable, I'm not sure if that's what it's called but it's what I call it), you set your variable first, then feed it the conditional.

Got rid of the $next variable, unnecessary

Condensed your if check for the $ignore part

Put all HTML in single quotes - yes, you have more quotes in echoing your variables, but it's easier to port to a plain HTML page for page construction without having to find/replace all " to \"

 

You still need to put in some checks to see if there are files for a valid next/previous link to show up (right now they show up regardless and the pages will be blank/break)

Link to comment
Share on other sites

Ok, here's some updated code for you

 

<?
    $album=basename($_GET['album']);
    $dirname = "uploader/photo/$album/";
    $images = scandir($dirname);
    $ignore = Array(".", "..");
    
    $start = (!empty($_GET['start']) ? $_GET['start'] : 0);
    $prev = ($start >= 20 ? $start - 20 : 0);
    $end = $start + 20;
    $i = 0;
       
    foreach($images as $curimg){
        if(in_array($curimg, $ignore)) continue;
        
        if(($i >= $start) && ($i < $end)) {
            echo '<a href="uploader/photo/'.$album.'/'.$curimg.'" rel="lightbox">'.$curimg.'</a>   '; 
        }      
        $i++;
    }
    echo '<br /><br /><a href="?album='.$album.'&start='.$prev.'">Previous Page</a> | <a href="?album='.$album.'&start='.$end.'">Next Page</a>';
?>

 

A few notes:

When you use the inline conditional switch ($start variable, I'm not sure if that's what it's called but it's what I call it), you set your variable first, then feed it the conditional.

Got rid of the $next variable, unnecessary

Condensed your if check for the $ignore part

Put all HTML in single quotes - yes, you have more quotes in echoing your variables, but it's easier to port to a plain HTML page for page construction without having to find/replace all " to \"

 

You still need to put in some checks to see if there are files for a valid next/previous link to show up (right now they show up regardless and the pages will be blank/break)

 

 


Hey man thanks for the help! That worked out great!

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.