Log in

View Full Version : sort function and readdir



calumogg
02-20-2008, 08:10 PM
I use this code below to update my database when I add new photos, but I want it to sort the pictures by filename before they are added, I know I need to use the sort function but I am not sure of where to place it, any help would be really useful.



// Define the full path to the folder whose contents you want to list
$path = ".";

//File Types allowed
$pattern="\.(jpg|jpeg)$";

// Open the directory
$dir_handle = @opendir($path) or die("Error opening $path");

// Loop through the files and filter out no jpgs
while(false !== ($file = readdir($dir_handle))){
if(eregi($pattern, $file)){

$first_letter = $file{0};

$query = "SELECT filename FROM pictures WHERE filename='$file'";
$result = mysql_query($query) or die('Error, query failed one');
if (mysql_num_rows($result)==''){

//Insert data into database
$query = "SELECT * FROM pictures WHERE category='$first_letter'";
$result = mysql_query($query) or die('Error, query failed');
$count = mysql_num_rows($result);
$count = $count + 1;

//Insert data into database
$sql="INSERT INTO pictures (filename, category, seq_num, large, thumb)VALUES('$file', '$first_letter', '$count', 'http://www.calumogg.co.uk/pictures/large/$file', 'http://www.calumogg.co.uk/pictures/thumbs/$file')";
$result = mysql_query($sql) or die('Error, query failed two');

echo "<p>$file has been added to the database</p>";

}
}


Thanks in advance

boogyman
02-20-2008, 09:32 PM
this script appears to only be able to handle 1 file at a time.

It also almost sounds as if you want to re-sort your entire database every time a new image file is added. If that is the case that is the wrong thing to do. When grabbing the files from the database to display / process, it is very easy to sort them then, rather then resort the entire database every time a new image is added



SELECT field(s) FROM table(s) WHERE condition(s) ORDER BY field ASC/DESC

ASC = ascending
DESC = descending

calumogg
02-20-2008, 10:02 PM
Sorry I must have phrased my question wrong, I don't want to resort my database every time. I just want the files to be arranged into alphabetical order before they are added into the database so that the script assigns the correct $count value to them. Would I need to use scandir to achieve this?

boogyman
02-20-2008, 10:09 PM
you could load the files into an array and sort the array than call this function/script to upload.

calumogg
02-20-2008, 10:12 PM
I am not familiar with arrays, how would I go about doing this? Would it be possible for you to show me?

boogyman
02-21-2008, 03:22 PM
well first you would need to know how your script is being called in the first place...

if you are creating a new function you would need to do something like


function sortImages($load=array(''))
{
if(is_array($load) && !empty($load)
{
$load = sort($load);
}
insertIntoDatabaseFunction($load);
}

calumogg
02-22-2008, 08:10 AM
Thanks very much for that, I will have a play and see what I come up with