Sort files in a directory using PHP + Mysql

Hi!

Basically, I have an image carousel in which each image is dynamically associated with the product. I have a script that reads a directory, saves the file names in an array. Then I crop the file extension, query the database for the article in which I want the image to link, by searching for the article with the same / similar name. for example: blets.jpg links to a full article. So I got this job.

But what I would like to do is be able to duplicate the images in the order that my articles assigned them in the ordering column.

What I now receive images and displays them in alphabetical order, what I'm trying to do is show them in the order that I assigned to my articles.

So here is my code:

echo "<div id='carousel'>\n"; // Get image file name // open directory $myDirectory = opendir("./images/products/carousel/spring-summer-2011/"); while($fileName = readdir($myDirectory)) // get each file { $dirArray[] = $fileName; } closedir($myDirectory); // close directory //sort($dirArray); // sort files echo "<div class='infiniteCarousel'> <div class='wrapper'> <ul>\n"; foreach ($dirArray as $file) { if (substr("$file", 0, 1) != ".") //don't list hidden files { $name = substr($file, 0, strrpos($file, '.')); // trim file name extension $res = mysql_query("Select id, alias, ordering from content where alias like '{$name}' ORDER BY ordering"); // order by is pretty useless here !! while ($row = mysql_fetch_array($res)) { $id = $row['id']; $alias = $row['alias']; $ordering = $row['ordering']; echo "<li><a href='index.php?option=com_content&view=article&id={$id}' title='{$alias}'>\n"; echo "<img src='./images/products/carousel/spring-summer-2011/{$file}' height='80' width='120' alt='{$alias}' />"; echo "</a></li>\n"; } } } echo "</ul>\n</div>\n</div>\n"; echo "</div>"; //Close Carousel 

I left my comments in the code. And I basically know what to do, just not sure how to do it. I need a professional! help.

+4
source share
5 answers
 $Line[] = array(); foreach ($dirArray as $file) { if (substr("$file", 0, 1) != ".") //don't list hidden files { $name = substr($file, 0, strrpos($file, '.')); // trim file name extension $res = mysql_query("Select id, alias, ordering from content where alias like '{$name}' ORDER BY ordering"); // order by is pretty useless here !! while ($row = mysql_fetch_array($res)) { $id = $row['id']; $alias = $row['alias']; $ordering = $row['ordering']; $Line[] = "<li id='$ordering'><a href='index.php?option=com_content&view=article&id={$id}' title='{$alias}'>\n <img src='./images/products/carousel/spring-summer-2011/{$file}' height='80' width='120' alt='{$alias}' /></a></li>\n"; } } } sort($Line); foreach ( $Line as $ordering => $line ) { echo "$line"; } 
+2
source

If you understand correctly, there is no need to read the catalog and store the images in an array.

You can simply query the database to get a collection of articles that you want to show in the right order and add .jpg to the article title to get the image name.

An added benefit is that you only need to query the database once for all of your articles instead of a separate query for each article.

+2
source

I recommend that you grab the file names like you do, but run the query and rendering separately. Each time you request information about a file, you must store this information as an object / array in another array. After you finish all your queries, you can sort them based on the order value. Finally, you will iterate over this array and display the results.

Hope this helps, let me know if you need more specific details.

+1
source

You can get all the rows in the array first, and then usort () to sort them according to your specific criteria:

 $rows = array(); foreach ($dirArray as $file) { // ... while ($row = mysql_fetch_array($res)) { $rows[] = $row; } } function my_sort($a, $b) { return $a['ordering'] - $b['ordering']; } usort($rows, 'my_sort'); foreach ($rows as $row) { // display the data } 

However, as recommended by jeroen, you should group all your queries into one, this complicates the code a bit, but is more efficient!

(By the way, you might also be interested in using mysql_fetch_assoc () over mysql_fetch_array () if you only use $ row as an associative array.)

+1
source

Good!!!

The eagle has landed. Ok, so I got permission from AR to work without a hitch.

@Benjamin I also earned your work, but I had to execute a function from the foreach loop and change it to:

 usort($rows, function($a, $b) { return $a > $b ? 1 : -1; }); 

then it worked.

I learned a lot from this experience, and therefore I thank you all. You really helped me understand arrays.

Now it works fine on my test server (LAMP), but if it works with a digit, it will not work on a Godaddy server. Ouf!

Peace.

+1
source

All Articles