Last active
December 17, 2015 06:49
-
-
Save somatonic/5568201 to your computer and use it in GitHub Desktop.
get random images across pages optimized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example use: | |
* | |
* $helper = $modules->get("RandomImages"); | |
* $image = $helper->getRandomImages(1,"images"); | |
* echo "<img src='{$image->size(150,0)->url}'>"; | |
*/ | |
class RandomImages extends WireData implements Module { | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'RandomImages', | |
'version' => 1, | |
'summary' => 'Get random images across pages. $image = getRandomImages(1,"images",1001);', | |
'href' => '', | |
'singular' => true | |
); | |
} | |
public function init() { } | |
public function getRandomImages($count=1, $field='images', $parent=1){ | |
$query = "SELECT i.pages_id,i.data FROM field_$field i | |
LEFT JOIN pages p ON p.id = i.pages_id | |
WHERE p.status < 2048 | |
AND p.parent_id in (SELECT pages_id FROM pages_parents WHERE parents_id=$parent OR pages_id=$parent) | |
ORDER BY RAND() LIMIT $count"; | |
$res = wire("db")->query($query); | |
if($res) { | |
if($count > 1){ | |
$images = new WireArray(); | |
while($r = $res->fetch_array()){ | |
$pid = $r['pages_id']; | |
$file = $r['data']; | |
$p = wire("pages")->get($pid); | |
$images->add($p->images->get($file)); | |
} | |
return $images; | |
} else { | |
$r = $res->fetch_array(); | |
$pid = $r['pages_id']; | |
$file = $r['data']; | |
$p = wire("pages")->get($pid); | |
$image = $p->images->get($file); | |
return $image; | |
} | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment