Jump to content

Recommended Posts

Hi,

 

Im new to php and have been trying to put together a simple image hosting site with code that ive found around the internet. I've got things working but I want the images in the gallery to display the latest uploaded first.

 

This is the code im using to display the images

 

<html>
  <head>
    <title>Slideshow</title>
    <style>
    </style>
  </head>
  <body>
    <?php
      require 'config.php';
      $offset=0;
      if(isset($_GET['offset'])) {
        $offset=$_GET['offset'];
      }
      $rowcount=1;
      $perc=round((1/$imgperrows)*100);
    ?>
    <table width="100%">
      <tbody>
        <tr>
        <?php
          $d=dir("files");
          $count=0;

          while (false !== ($entry = $d->read())) {
            if($entry!='.' && $entry!='..') {


              if($count>=$offset && $count<($offset+$imgperpage)) {
                if($useshrinked) {
                  if(file_exists('tmb/'.$tmbprefix.$entry)) {
                    echo '<td width="'.$perc.'%" align="center"><a href="images/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  } else {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="files/'.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  }
                } else {
                  if(file_exists('tmb/'.$tmbprefix.$entry)) {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  } else {
                    echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"';
                    if($launchnew) echo ' target="_blank"';
                    echo '><img src="noprev.gif" width="80" height="80" border="0"><br>'.$entry.'</td>';
                  }
                }
                $rowcount++;
                if($rowcount>$imgperrows) {
                  echo '</tr><tr>';
                  $rowcount=1;
                }
             }
             $count++;
          }
          }
          $d->close();
        ?>
        </tr>
      </tbody>
    </table>
    <br>
    <center>
      <input type="button" value="Previous" style="font: 11px bold arial;width:70px;"<?php if($offset==0) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset-$imgperpage; ?>';">
       
      <input type="button" value="Next" style="font: 11px bold arial;width:70px;"<?php if($count>$offset && $count<=($offset+$imgperpage)) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset+$imgperpage; ?>';">
    </center>

 

The uploading code im using creates a log file with upload times in it.

 

Can anyone give me some help and point me in the right direction?

If you have the time added stored then in the SQL query add ORDER time_added DESC

replace time_added with the name of the column that contains the time the image was added.

 

Also please use code tags

Thanks for the quick reply.

 

Im not usiing and SQL database though I just have the images in a folder. Upload times are saved in a text file. Will it make things much more diffucult doing it without an SQL database?

O right, just read that erm yeah it will make things a bit more complicated.

 

What i would advise then is to maybe store the images and times in an array and sort the array by time, i am not %100 on that though.

<?
function sortarrayByDate($array,$dir)
{ 
$temp = $array; 
$temp2 = array(); 
$targetdir = getcwd()."/".$dir;
foreach($temp as $key => $i){ 
	$modifyDate = floatval(date("YmdHis", (filemtime($targetdir.$i)))); 
	if($key < 10){ 
		$temp2[$i] = floatval($modifyDate."0000".$key); 
	}		
	elseif($key < 99){ 
		$temp2[$i] = floatval($modifyDate."000".$key); 
	}
	elseif($key < 999){ 
		$temp2[$i] = floatval($modifyDate."00".$key); 
	}
	elseif($key < 9999){ 
		$temp2[$i] = floatval($modifyDate."0".$key); 
	}
	else{ 
		$temp2[$i] = floatval($modifyDate.$key); 
	}
}
arsort($temp2); 
$counter = 0; 
foreach($temp2 as $key => $value){ 
	$temp[$counter] = $key; 
	$counter++; 
} 
return $temp; 
}
$targetdir = getcwd()."/docs";
$files = array();
$directory = opendir($targetdir);
while($filename = readdir($directory))
{
  if(strlen($filename) > 2)
  {
    array_push($files, $filename);
  }
}
$files = sortarrayByDate($files,'docs');
?>

 

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.