By jason-1 on
The random thumbnails weren't working for me -- sometimes I wouldnit get a thumbnail for an album on the gallery page.
When I looked at the code, the query didn't seem to make sense...
A patch follows, although it uses the mysql specific LIMIT clause -- Postgres has something similar, but what is the prefered way of handling this type of thing?
J
<code>
--- image/image.module 2003-08-18 12:03:14.000000000 -0700
+++ image2/image.module 2003-08-18 12:08:21.000000000 -0700
@@ -1229,10 +1229,10 @@
$albums[$term_id]->thumb = $last_update->thumb_path;
if (variable_get("image_gallery_thumb", "last") == "random") {
- $nodes = db_fetch_object(db_query("SELECT MIN(nid) as min, MAX(nid) as max FROM {node} WHERE type = 'image'"));
- $id = rand($nodes->min, $nodes->max);
- $random = db_fetch_object(db_query("SELECT i.thumb_path FROM {node} n INNER JOIN {image} i ON n.nid = i.nid INNER JOIN {term_node} t ON n.nid = t.nid WHERE t.tid IN (". implode(",", $terms) .") AND n.type = 'image' AND i.personal = 0 AND n.nid >= ". $id ." LIMIT 1"));
- $albums[$term_id]->thumb = $random->thumb_path;
+ $id = rand (0, ($albums[$term_id]->image_count - 1));
+ $result = db_query ("SELECT i.thumb_path FROM {term_node} tn INNER JOIN {image} i ON tn.nid = i.nid WHERE tn.tid = ". $term_id ." LIMIT " . $id . ", 1" );
+ $term = db_fetch_object ($result);
+ $albums[$term_id]->thumb = $term->thumb_path;
}
}
Comments
The LIMIT MySQL has to be rem
The LIMIT MySQL has to be remove. When using PEAR DB any query using LIMIT must be modify tu use $db->limitQuery($sql, $from, $count) where $from and $count are the exact same value as after the LIMIT statement.
This is the only way that this can be then implemented with postgreSQL
http://pear.php.net/manual/en/package.database.db.db-common.limitquery.php
Use db_query_range()
I think we should use db_query_range() instead of db_query() when we want to return a LIMITed set of results. See this.
And, this means that image.module needs some cleanup...