Page 1 of 1

need help with sort

Posted: Mon May 25, 2009 12:01 am
by demeter_aurion
Hello, I'm new to this forum. Right now I'm using this free script called Captivate to display my website's free icons and textures, the only thing I am unable to do is have them list in reverse order. They are currently named 001.png, 002.png, ...etc. And I need them to list reverse so the "newest" images are display at the very top.

Here is the current script being used. (There is also a config.php page but I don't think it's required to perform this)

Code: Select all

<?php 
 
 
// DON'T EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
// ----------------------------------------------------------------------------------
 
include_once("config1.php");
 
 
 
//get script start time if requested
if (($showtime == "y") OR ($showtime =="Y")); {
    $starttime = explode(' ', microtime());
    $scriptstart = $starttime[0] + $starttime[1];
}
 
//calculate number of pages according to number of images
$pagecalc = ($imagecount / $perpage);
$pagecount = ceil($pagecalc);
 
//direct invalid page requests back to the first page
$page = $_GET["page"];
if (($page > $pagecount) || ($page == "0")) {
    $start = 0;
    echo "<p>Page $page does not exist!  You have been taken to the first page.</p>";
} elseif (!$page) {
    $start = 0;
    $page = 1;
} elseif (!is_numeric($page)) {
    $start = 0;
    $page = 1;
    echo "<p>Page $page does not exist!  You have been taken to the first page.</p>";
} else {
    $start = (($page * $perpage) - $perpage);
}
 
//put put jpg, jpeg, and jpe images in thumbs directory into array
$thumbhandle=opendir($thumbdirectory);
while ($file = readdir($thumbhandle)) {
    if (eregi(".png$|.jpg$", $file)) {
        $thumbarray[] = $file;
    }
}
closedir($thumbhandle);
 
//make array of all images that also have thumbs
$combinedarray = @array_intersect($imagearray, $thumbarray);
if (!$combinedarray) {
    echo "<p>An error has occurred.  You probably haven't made or uploaded any thumbnails yet so the gallery can't be created!</p>";
    echo $footer;
    exit;
}
 
$combinedarraycount = count($combinedarray);
 
 
//if image isn't set, show gallery
$image = $_GET["image"];
if (!$image) {
 
 
    //make array from above array of images to be displayed on requested page
    $pagearray = array_slice($combinedarray, $start, $perpage);
    $pagearraycount = count($pagearray);
 
 
    //open gallery table
    echo "<table align=\"center\" id=\"gallery\">\n";
 
 
    //open top menu if requested
    if (($topmenu == "y") OR ($topmenu =="Y")) {
        echo "<tr><td colspan=\"$perrow\" class=\"topmenu\" style=\"text-align: center\">\n";
 
 
        //menu calculation
        $j = 1;
        while ($j <= $pagecount) {
            $pagespanstart = ((($j * $perpage) - $perpage) + 1);
            $pagespanend = ($j * $perpage);
 
            //determine link style
            if ($menustyle == "page") {
                $pagelink = "page $j";
            } elseif ($menustyle == "number") {
                $pagelink = "$j";
            } else {
                if ($j == $pagecount) {
                    $lastpagecount = ($perpage - (($perpage * $pagecount) - $combinedarraycount));
                    $lastpagespanend = (($pagespanstart + $lastpagecount) - 1);
                    $pagelink = "$pagespanstart - $lastpagespanend";
                } else {
                    $pagelink = "$pagespanstart - $pagespanend";
                }
            }
 
            //actual menu links
 
            if ($returnlink == "y") {
                if ($j == $pagecount) {
                    if ( $j == $page) {
                        echo "$pagelink | <a href=\"$returnlinkname\">$returnlinktext</a>";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | <a href=\"$returnlinkname\">$returnlinktext</a>";
                    }
                } else {
                    if ($j == $page) {
                        echo "$pagelink | ";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | ";
                    }
                }
 
            } else {
 
                if ($j == $pagecount) {
                    if ( $j == $page) {
                        echo "$pagelink";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a>";
                    }
                } else {
                    if ($j == $page) {
                        echo "$pagelink | ";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | ";
                    }
                }
            }
 
        
            $j++;
        }
 
        
        echo "</td></tr>";
    }
 
 
    
    //build gallery
    $i=0;
 
    foreach($pagearray as $pic) {
 
        //make a new row if the remainder of $i divided by $perrow is 0
        if($i % $perrow == 0) {
            echo "<tr>\n";
        }            
 
        //get size of thumbnail
        $thumbsize = getimagesize("$thumbdirectory/$pic");
 
        //determine method to open full-size image
        if ($openstyle == "gallery") {
            echo "<td><img class=\"imgbord\" src=\"$thumbdirectory/$pic\" $thumbsize[3] border=\"0\" alt=\"\" /></td>\n";
        } elseif ($openstyle == "samepage") {
            echo "<td><a href=\"$imagedirectory/$pic\"><img class=\"imgbord\" src=\"$thumbdirectory/$pic\" $thumbsize[3] border=\"0\" alt=\"\" /></a></td>\n";
        } else {
            echo "<td><a href=\"$imagedirectory/$pic\" target=\"_blank\"><img class=\"imgbord\" src=\"$thumbdirectory/$pic\" $thumbsize[3] border=\"0\" alt=\"\" /></a></td>\n";
        }
                    
        //add an extra cells if there are not enough columns
        if($i == ($pagearraycount - 1)) {
            while(($i + 1) % $perrow != 0) {
                echo "<td>&nbsp;</td>\n";
                $i++;
            }                
        }            
                    
        //end table row if the remainder of $i+1 divided by $perrow is 0
        if(($i + 1) % $perrow == 0) {                
            echo "</tr>\n";
        }
                
        $i++;
    }
 
 
    //bottom menu
    if (($bottommenu == "y") OR ($bottommenu =="Y")) {
        echo "<tr>\n<td colspan=\"$perrow\" class=\"bottommenu\" style=\"text-align: center\">\n";
 
 
        //menu calculation
        $j = 1;
        while ($j <= $pagecount) {
            $pagespanstart = ((($j * $perpage) - $perpage) + 1);
            $pagespanend = ($j * $perpage);
 
            //determine link style
            if ($menustyle == "page") {
                $pagelink = "page $j";
            } elseif ($menustyle == "number") {
                $pagelink = "$j";
            } else {
                if ($j == $pagecount) {
                    $lastpagecount = ($perpage - (($perpage * $pagecount) - $combinedarraycount));
                    $lastpagespanend = (($pagespanstart + $lastpagecount) - 1);
                    $pagelink = "$pagespanstart - $lastpagespanend";
                } else {
                    $pagelink = "$pagespanstart - $pagespanend";
                }
            }
 
            //actual menu links
 
            if ($returnlink == "y") {
                if ($j == $pagecount) {
                    if ( $j == $page) {
                        echo "$pagelink | <a href=\"$returnlinkname\">$returnlinktext</a>";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | <a href=\"$returnlinkname\">$returnlinktext</a>";
                    }
                } else {
                    if ($j == $page) {
                        echo "$pagelink | ";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | ";
                    }
                }
 
            } else {
 
                if ($j == $pagecount) {
                    if ( $j == $page) {
                        echo "$pagelink";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a>";
                    }
                } else {
                    if ($j == $page) {
                        echo "$pagelink | ";
                    } else {
                        echo "<a href=\"$gallery?page=$j\">$pagelink</a> | ";
                    }
                }
            }
 
 
            $j++;
        }
 
        echo "</td>\n</tr>\n";
    }
 
 
    //show gallery generation time
    if (($showtime == "y") OR ($showtime =="Y")) {
        echo "<tr><td colspan=\"$perrow\" style=\"font-size: 9px; padding-top: 10px; margin: 0px; text-align: center;\">";
        $endtime = explode(' ', microtime());
        $scriptend = $endtime[0] + $endtime[1];
        $totaltime = round($scriptend - $scriptstart, 5);
        echo "<span>Gallery generated in $totaltime seconds.</span></td></tr>";
    }
 
    //close table
    echo "</table>";
 
 
// else, if image is set, show single image
} elseif ($image) {
    $combinedarray = array_values($combinedarray);
    $combinedarraycount = count($combinedarray);
 
    //get key of current image
    $imagekey = array_search($image, $combinedarray);
 
    //set back and next images
    if ($imagekey > 0) {
        $backkey = ($imagekey - 1);
        $backimage = "<a href=\"$gallery?image=$combinedarray[$backkey]\">&laquo; previous</a>";
    } else {
        $backimage = "&laquo; previous";
    }
 
    if ($imagekey < ($combinedarraycount - 1)) {
        $nextkey = ($imagekey + 1);
        $nextimage = "<a href=\"$gallery?image=$combinedarray[$nextkey]\">next &raquo;</a>";
    } else {
        $nextimage = "next &raquo;";
    }
 
    //calculate gallery page of image
    $sourcepage = ceil(($imagekey +1) / $perpage);
 
    //display single image
    $imagesize = getimagesize("$imagedirectory/$image");
 
    echo "<table id=\"image\" cellspacing=\"0\" cellpadding=\"0\">\n<tr>\n<td style=\"text-align: center;\"><table cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto; margin-right: auto;\"><tr><td style=\"text-align: right;\">$backimage </td><td style=\"text-align: center;\"> | <a href=\"$gallery?page=$sourcepage\">back to thumbnails</a> |</td><td style=\"text-align: left;\"> $nextimage</td></tr></table></td>\n</tr>\n";
    echo "<tr>\n<td ><img src=\"$imagedirectory/$combinedarray[$imagekey]\" $imagesize[3] alt=\"\" /></td>\n</tr>\n";
    echo "<tr>\n<td style=\"text-align: center;\"><table cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto; margin-right: auto;\"><tr><td style=\"text-align: right;\">$backimage </td><td style=\"text-align: center;\"> | <a href=\"$gallery?page=$sourcepage\">back to thumbnails</a> |</td><td style=\"text-align: left;\"> $nextimage</td></tr></table></td>\n</tr>\n";
    if (($imageinfo == "y") OR ($imageinfo == "Y")) {
        echo "<tr>\n<td class=\"imageinfo\" style=\"font-size: 9px; margin: 0px; padding-top: 10px; text-align: center;\">$imageinfotext</td>\n</tr>\n</table>";
    } else {
        echo "</table>";
    }
}
 
 
// ----------------------------------------------------------------------------------
// DON'T EDIT ABOVE THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
?>
 
 
Any help would be greatly appreciated, as I am not yet skilled with PHP. Thanks!

Re: need help with sort

Posted: Mon May 25, 2009 12:04 am
by mikemike
Hi there,

I think you need to be a little bit more specific when asking here, as posting 309 lines then asking to fix it isn't really how it works unfortunately.

What you need to do is work out how the files are ordered at the moment. It sounds like they're ordered by name. This may mean that it is database driven or simply file and directory driven, if it's the later you'll find it harder to order by the date modified. If you find it's database-driven then you just need to edit the MySQL statements so that the ORDER BY field is that of the date, assuming it has a date field.

Good luck.

Re: need help with sort

Posted: Mon May 25, 2009 12:17 am
by demeter_aurion
It isn't using a database or else I wouldn't be having this problem. MySQL is much simpler for me.
Another script I use has them ordered reversely.

Code: Select all

<?php 
CONFIG VARIABLES
------------------------------------------------*/
$catdir = 'categories';  // the icon categories folder (see step 1)
$sort = 'first';         // set 'first' to show newest icons first, 'last' to show newest icons last
$getcount = true;        // true shows the icon count per category in the menu, and false turns it off
$perpage = 28;          // the number of icons shown on each page (set to a really high number to turn off pagination)
$divider = '&nbsp;';     // the divider between the numbers and front/back arrows in the page navigation
 
 
// DO NOT EDIT THE PHP BELOW THIS LINE.  Scroll down to edit the text above the menu, and to add your footer.
// ----------------------------------------------
 
// get category folders and sort
if (!file_exists($catdir)) {
    echo 'Error!  The folder named <strong>' . $catdir . '</strong>, as specified in the config, does not exist in this directory.  The value of the variable $catdir must match the name of the created folder (case-sensitive), and that folder must be in this directory for the script to work.  Create the folder and put the individual icon category folders inside it.';
    exit();
}
$getfolders = opendir($catdir);
while (($file = readdir($getfolders)) !== false) {
    if (($file != '.' && $file != '..') && (is_dir($catdir.'/'.$file))) {
        $cats[] = $file;
    }
}
if (!$cats) {
   echo 'No category folders were found inside the folder named <strong>' . $catdir . '</strong>.  Create your individual category folders inside that folder and the menu will appear.';
    exit();
}
natcasesort($cats);
 
// build main menu
if ((!$_GET['cat']) || (!in_array($_GET['cat'], $cats))) {
    ?>
 
    <!-- Content Above Menu -->
 
    
    <!-- End Content Above Menu -->
 
    <?php
    echo '<ul id="iconmenu">';
    foreach ($cats as $value) {
        $catname = str_replace('_', ' ', $value);
      echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?cat=' . $value . '">' . $catname . '</a>';
      if ($getcount) {
         $icons = array();
            $getcount = opendir($catdir . '/' . $value);
            while (($icon = readdir($getcount)) !== false) {
                if (eregi(".jpg$|.jpeg$|.gif$|.png$|.bmp$", $icon)) {
                    $icons[] = $value;
                }
            }
            $count = count($icons);
         if ($count == 1) {
                echo ' (' . $count . ' base)';
            } else {
                echo ' (' . $count . ' bases)';
            }
      }
      echo '</li>';
    }
    echo '</ul>';
}
 
// get icons and sort
if (in_array($_GET['cat'], $cats)) {
    $icons = array();
    $geticons = opendir($catdir.'/'.$_GET['cat']);
    while (($icon = readdir($geticons)) !== false) {
        if (eregi(".jpg$|.jpeg$|.gif$|.png$|.bmp$", $icon)) {
            $icons[] = array("name" => $icon, "modified" => filemtime($catdir . '/' . $_GET['cat'] . '/' . $icon));
        }
    }
    $count = count($icons);
    if ($count > 0) {
        function modcmp($a, $b) {
            return strcmp($a['modified'], $b['modified']);
        }
        usort($icons, 'modcmp');
        if ($sort == 'first') {
            $icons = array_reverse($icons);
        }
 
      // show category name and total icon count
        $catname = str_replace('_', ' ', $_GET['cat']);
      echo '<p><strong>' . $catname . ': ' . $count;
        if ($count == 1) {
            echo ' base';
        } else {
         echo ' bases';
        }
        echo '</strong></p>';
 
      // configure pagination
        if ((!$perpage) || ($perpage < 1) || (!is_numeric($perpage))) {
            $perpage = 20;
        }
        $pagecalc = ($count / $perpage);
        $pagecount = ceil($pagecalc);
      $page = $_GET['page'];
        if (($page > $pagecount) || ($page == '0')) {
            $start = 0;
        } elseif (!$page) {
            $start = 0;
            $page = 1;
        } elseif (!is_numeric($page)) {
            $start = 0;
            $page = 1;
        } else {
            $start = (($page * $perpage) - $perpage);
        }
        $pagearray = array_slice($icons, $start, $perpage);
 
        // if there is more than one page, show the page menu
        if ($pagecount > 1) {
            if (($page <= $pagecount) && ($page > 1)) {
                $backpagecalc = ($page - 1);
            $backpage = '<a href="?cat=' . $_GET['cat'] . '&page=' . $backpagecalc . '">&laquo;</a> ' . $divider;
            }
            if ($page < $pagecount) {
                $nextpagecalc = ($page + 1);
                $nextpage = $divider . ' <a href="?cat=' . $_GET['cat'] . '&page=' . $nextpagecalc . '">&raquo;</a>';
            }
            echo '<p class="pagemenu">Pages: &nbsp;' . $backpage . ' ';
            $i = 1;
            while ($i <= $pagecount) {
            $pagespanstart = ((($i * $perpage) - $perpage) + 1);
            $pagespanend = ($i * $perpage);
            if ($i == $pagecount) {
               if ( $i == $page) {
                  echo $i;
               } else {
                  echo '<a href="?cat=' . $_GET['cat'] . '&page=' . $i . '">' . $i . '</a>';
               }
            } else {
               if ($i == $page) {
                  echo $i . ' ' . $divider . ' ';
               } else {
                  echo '<a href="?cat=' . $_GET['cat'] . '&page=' . $i . '">' . $i . '</a> ' . $divider . ' ';
               }
            }
            $i++;
            }           
            echo ' ' . $nextpage . '</p>';
        }
        
        // show the icons
        echo '<p class="icons" align="center">';
            foreach ($pagearray as $value) {
                $iconsize = getimagesize($catdir . '/' . $_GET['cat'] . '/' . $value['name']);
                echo '<img class="imgbord" src="' . $catdir . '/' . $_GET['cat'] . '/' . $value['name'] . '" ' . $iconsize[3] . ' alt="" /> ';
            }
    } else {
        echo '<p>There are no bases in this category.';
    }
    echo '</p><p>&laquo; <a href="' . $_SERVER['PHP_SELF'] . '">back</a></p>';
}
?>
 
Obviously, it's the sort=first
and then the section which is described "Get icons and sort" (line 67)

Unfortunately I don't know very much, thus I am asking for help...