PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have an array of files with each file being mapped to the date it was created. I want to introduce a 3rd 'keyid' column so that I can order the array by date selected and return the first x files easily. Is there a particular function I should be looking at for this or is my approach fundamentally wrong in some way?
will return what you want. You have constructed the array $picture_array with Unix timestamps as the keys and the names of the picture as the values. If you want the ten most recent pictures, you could sort the array (or, more strictly, you could use rsort() so that the highest timestamp is first), then array_slice() (to isolate the top ten pictures) and then iterate through the sliced array with 'foreach' to display the pictures.
Hi thanks very much for the suggestion that seems exactly what I want. I'm having trouble with the array_slice function though. Have used krsort to sort the array as I think the unix timestamp (which I want to order the array by) is the key in this case. Then I've tried using the array_slice as you suggested but it still returns all the values of the array. Could anyone see what I'm doing wrong please?
foreach(glob("gallery/*") as $album){
foreach(glob("$album/*_th.jpg") as $picture){
$time = filemtime($picture);
$picture_array[$time] = $picture;
}
}
krsort($picture_array);
$top_ten = array_slice($picture_array,0,10,true);
print_r($top_ten);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Thanks AbraCadaver this does give me a top ten as I wanted. If printed the array returns the top ten files with their unix timestamps. According to this the ten most recent files are indeed returned (i.e. the ten timestamps returned are chronological) but this isn't actually the case.
I upload files using filezilla so I expected that when I uploaded a folder with ten or more items in it all the files returned by this script would be from that most recent folder but this isn't the case (some files returned are from most recent folder but with 2 or 3 exceptions). Any idea why this is? I'm not sure if the problem is with my foreach loops or with my use of the filemtime function.
Also the array $picture_array doesn't seem to contain every file anymore, printing it only outputs about 25 there should be a couple of hundred? Would be grateful if anyone could help me out!
I don't know exactly why you are having these problems. However, relying on filemtime() can lead to unexpected results (it is the time the file was last modified, not the time the picture was uploaded). If I wanted to manage several hundred pictures, I would use a database to store information about them. A database would be more reliable and flexible than your present approach. You can store the information you want and extend it and search it in all sorts of ways.
Array keys must be unique, so if there are 10 files with the same timestamp then only the last one added to the array will be there. You might try using this instead:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Unique key ids does seem to be a problem which brings me back to my original question. Is it possible/how could I have an array with two columns (timestamp and filename) and a third column for unique key ids, where the array could be sorted by timestamp?
Just gives me the timestamp and an id the filename is lost.
I realise databases could be used but I've only ever used MySQL with PHPmyadmin and don't have that now (I'm not even sure if the host has MySQL installed) so was hoping it could be done this way.
I'm losing it, sorry. The problem is with rsort(). You need to use arsort().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Sorry, I'm a bit confused - why is the problem with the sorting? Have tried using arsort but think I want asort to have it in order of newest first. Either way though, it doesn't solve the fact that only some files are stored in the array (presumably because of the unique keyid thing?).
Oh sorry, realise what you meant now. Have modified my code to use arsort now and printing the array does give me the array I want. The only problem now is that the filenames are the keyids rather than the values (unless I'm mistaken?). I'm trying to loop through the array using a foreach statement but obviously it now loops through the timestamps rather than the filenames. How do I specify that I want the foreach loop to loop through the keyids rather than the values?
This is the code as it currently is if that helps:
foreach(array_keys($latest) as $file) {
echo $file;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.