Zdravím. Potřeboval bych pomoc. Jak zobrazit náhodně obrázky ze všech galerií, když je mám uloženy ve složce Galerie v různých podsložkách. Nevíte někdo, jak na to? Potřeboval bych, aby na stránkách byl zobrazen vždy jeden obrázek, ale vybraný náhodně ze všech galerií. Díky.
Diskuse
Téma: Náhodný obrázek - rada
no pokud by to byl obrazek z klasicke galerie, tak nejakym takovym prikazem:$img=mysql_result(mysql_query("SELECT * FROM `"._mysql_prefix."-images` ORDER BY RAND() LIMIT 1"),0);se vytahne z databaze, pokud to chces ze slozek, tak tam uz by se musela napsat funkce ktera vsechny podadresare projde a pak jeden nahodne vybere
Pokud ze slozky, tak by to slo nejak takhle:function get_files($dir){
$files = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while (($file = readdir($dh)) !== false) {
if(!($file == '.' || $file == '..')){
$file = $dir.'/'.$file;
if(is_dir($file) && $file != './.' && $file != './..'){
$files = array_merge($files, get_files($file));
}
else if(is_file($file)){
$files[] = $file;
}
}
}
}
}
return $files;
}
$images=get_files("images");
echo $images[array_rand($images, 1)];
Pokud ze slozky, tak by to slo nejak takhle:function get_files($dir){
$files = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while (($file = readdir($dh)) !== false) {
if(!($file == '.' || $file == '..')){
$file = $dir.'/'.$file;
if(is_dir($file) && $file != './.' && $file != './..'){
$files = array_merge($files, get_files($file));
}
else if(is_file($file)){
$files[] = $file;
}
}
}
}
}
return $files;
}
$images=get_files("images");
echo $images[array_rand($images, 1)];
A pomocí HCM modulu - Náhodný text nebo obrázek - by to nejak neslo?
<?php
/*--- kontrola jadra ---*/
if(!defined('_core')){exit;}
if (!function_exists('jv_randomimg')) {
function jv_randomimg($dir){
$files = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while (($file = readdir($dh)) !== false) {
if(!($file == '.' || $file == '..')){
$file = $dir.'/'.$file;
if(is_dir($file) && $file != './.' && $file != './..'){
$files = array_merge($files, jv_randomimg($file));
}
else if(is_file($file)){
$files[] = $file;
}
}
}
}
}
return $files;
}
}
/*--- definice funkce modulu ---*/
function _HCM_jv_randomimage($path, $title=null){
if(isset($titulek) and $titulek!=""){$titulek=_htmlStr($titulek);}
$images=jv_randomimg($path);
$image=$images[array_rand($images, 1)];
return "<img src='"._indexroot.$image."' alt='".(($title!="")?$title:'img')."' />\n";
}
?>
cely kod hcm, pouziti uz snad je jasne, prni parametr je cesta a je povinny, druhy je nepovinny titulek.
/*--- kontrola jadra ---*/
if(!defined('_core')){exit;}
if (!function_exists('jv_randomimg')) {
function jv_randomimg($dir){
$files = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while (($file = readdir($dh)) !== false) {
if(!($file == '.' || $file == '..')){
$file = $dir.'/'.$file;
if(is_dir($file) && $file != './.' && $file != './..'){
$files = array_merge($files, jv_randomimg($file));
}
else if(is_file($file)){
$files[] = $file;
}
}
}
}
}
return $files;
}
}
/*--- definice funkce modulu ---*/
function _HCM_jv_randomimage($path, $title=null){
if(isset($titulek) and $titulek!=""){$titulek=_htmlStr($titulek);}
$images=jv_randomimg($path);
$image=$images[array_rand($images, 1)];
return "<img src='"._indexroot.$image."' alt='".(($title!="")?$title:'img')."' />\n";
}
?>
cely kod hcm, pouziti uz snad je jasne, prni parametr je cesta a je povinny, druhy je nepovinny titulek.
Dík, ale stejne mi to nejde. Kod HCM jsem nakopiroval ho novyho HCM modulu jv_randomimg a ten jsem pak vyvolal pomoci [hcm]custom,jv_randomimg,upload/galerie[/hcm]. Kde je chyba?
[hcm]jv_randomimage,upload/galerie[/hcm]soubor se musi jmenovat jv_randomimage.php a musi byt ve slozce require/hcm
Díky moc. A ještě jeden dotaz. Vím, že jsem otravnej :-), ale dalo by se nastavit, aby ty obrázky měly nějaký konstantní rozměr (např. šířku)? Dík.
Samozřejmě:return "<img width="100" src='"._indexroot.$image."' alt='".(($title!="")?$title:'img')."' />\n";případně to předělat aby se generoval náhled jako třeba v galeriích, ale u 1 obrázku je to myslím zbytečné
Tak tohle se mi zalíbilo, s tím, že jsem si řekl, že to taky použiju. Ale nejede mi to.. Píše mi to tuto chybu:
Parse error: syntax error, unexpected T_LNUMBER in /home1/zahradyb/public_html/skjimm_cz/require/hcm/jv_randomimage.php on line 32A na 32 řádku mám tohle:
return "<img width="100" src='"._indexroot.$image."' alt='".(($title!="")?$title:'img')."' />\n";
Parse error: syntax error, unexpected T_LNUMBER in /home1/zahradyb/public_html/skjimm_cz/require/hcm/jv_randomimage.php on line 32A na 32 řádku mám tohle:
return "<img width="100" src='"._indexroot.$image."' alt='".(($title!="")?$title:'img')."' />\n";
Samotný mi to funguje, ale kdyz tam pridam tu vysku, tak mi zmizi cely obsah stranek+pravy sloupec, ve kterem jsem ten HCM kod mel (zustane tam jenom levy sloupec s menu). Bez te vysky obrazku to ale funguje bezchybne...
Díky za radu. Bez width="100" mi to tedy jde taky.. ale jak na tu změnu velikosti???