2 keljem Jan 23, 2008 11:56

For an not related project I made this:
<?php
/**
* Genererate random number and pick random foto's from a folder.
*
*/
function genRandomArray( $min, $max, $num )
{
$result = array();
$count = 0;
$currentnum = 0;
while(count($result) < $num)
{
$currentnum = rand($min, $max);
if (!in_array($currentnum, $result))
{
$result[$count] = $currentnum;
$count++;
}
}
return $result;
};
$handle = opendir( '../images/' ); // the folder with pics
while ( false !== ( $file = readdir( $handle )))
{
if ( $file!= "." && $file != ".." ) {
$picArray[] = $file;
}
};
closedir( $handle );
$k = 0;
$aantalFotos = count( $picArray );
$aantalFotos = $aantalFotos - 1; // Stupid arrays
$rrand = genRandomArray(0, $aantalFotos, 1); //Now number of pics is 1
/* for($i = 1; $i <= 2; $i++) */ //So no need for this loop
{
$pNumber = $rrand[$k];
$picture[] = $picArray[$pNumber];
$k++;
}
?>
<?php
//Show the picture
$random_pic = '../images/'.$picture[0];
?>
<img src="<?php echo $random_pic ?>" alt="Random Picture" />
Put this code in a file randompic.php, put the file on the server.
Go to backoffice, add a freeHTML widget to the sidebar, edit it and point it to the randompic.php file.
Simply put an arbitrary number of pictures in the ../images/ folder and it should work.
Questions?
*edit*
This is not going to work in a freeHTML widget or some one must know how to call a php file in the widget. Probably you'll have to put the code in the skin. I will follow up on this post in a few hours time.
Hi Afwas,
Thanks for your help with this, its very good of you :)
I will check back later to see if it can be done, thanks again for all you help.
Gosh, this is a nice forum :)
Gz
PHP in the Free HTML widget works?
Thanks,
I trust when Afwas said 'Point it to the php file' he meant like this ?
(in the free html in the widget I put)
<img src="randompic.php" alt="Random Picture" title="Random Picture" />
Is this correct please ?
If it is then it doesn't seem to be working unless I have messed up somewhere ?
Gz
Okay that might work. If not the same trick can be done with javascript. Not the best solution imaginable though so let's hope using a php file as the img src works.
Maybe ....
<img src="<?php echo $random_pic ?>" alt="Random Picture" />
No luck guys with this one, sorry.
Thanks for your help everyone though :)
Gz
No this is not going to work the way I hoped. The code is OK, see http://www.blog.hemminga.net/randompic.php I am now converting the code into a widget.
Random Picture plugin version 0.1
I have made a new topic in the Plugin section of the forum: [url=http://forums.b2evolution.net/viewtopic.php?t=14100&start=0&postdays=0&postorder=asc&highlight=][2.3] Random Picture plugin (widget)[/url].
Yes, the plugin is ready.
Good Morning Afwas :)
Thank you very much indeed, I cant believe that you have gone to so much trouble on my behalf. It is very good of you and I think this will be a top plugin download for everyone. THANK YOU.
Could you have a look at the link for the download of the plugin on the post please, I can't seem to get the file.
Thanks very much, it looks really good from the screen shots and just what I was looking for.
:) :) :) :) :) :) :) :) :) :)
Gz
@Gary, just get rid of the " and the , at the end of the active download link in that post
Cheers John,
I didn't see that ;)
Gz
I fixed the link
Nice one, thanks again for all your help Afwas.
I just hope I pick up some knowledge over the next few years to help someone in the future like you helped me. Although coding isn't my strongest point and php goes straight over my head.
Your a star :)
Gz
WOW, just installed it and it worked first time a treat.
Thanks again Afwas, just one glitch (if I dare mention it) It doesnt seem to like to work in IE :-/
Not that I care as I never use Internet Exploder 8|
Gz
keljem wrote:
Thanks again Afwas, just one glitch (if I dare mention it) It doesnt seem to like to work in IE :-/ z
Fixed, see the other topic.
Glad you like it.
* at the risk of looking stupid *
Thanks, Sorry which other topic do you mean ?
Gz ;)
[url=http://forums.b2evolution.net/viewtopic.php?t=14100&start=0&postdays=0&postorder=asc&highlight=][2.3] Random Picture plugin (widget)[/url].
Fantastic, thanks again.
Its just what I wanted ;)
I owe you a beer :P
Gz
Forgot to say, that this could maybe use images in an original post
or
from a separate folder on the server.
Cheers
Gz