You are missing our premiere tool bar navigation system! Register and use it for FREE!

NukeCops  
•  Home •  Downloads •  Gallery •  Your Account •  Forums • 
Readme First
- Readme First! -

Read and follow the rules, otherwise your posts will be closed
Modules
· Home
· FAQ
· Buy a Theme
· Advertising
· AvantGo
· Bookmarks
· Columbia
· Community
· Donations
· Downloads
· Feedback
· Forums
· PHP-Nuke HOWTO
· Private Messages
· Search
· Statistics
· Stories Archive
· Submit News
· Surveys
· Theme Gallery
· Top
· Topics
· Your Account
Who's Online
There are currently, 338 guest(s) and 0 member(s) that are online.

You are Anonymous user. You can register for free by clicking here
How to display random images in PHP-Nuke blocks

20.8.1. How to display random images in PHP-Nuke blocks

We now move to a more advanced example of a PHP-Nuke block: a block that displays random images. Suppose you have a collection of images in a directory of your webserver and would like to display a random image each time in a PHP-Nuke block. Using PHP-Nuke's own positioning capabilities for blocks (in the administration panel, from the "Blocks" link, see Section 7.1), you can achieve an almost arbitrary positioning of random images in your page - a functionality that could be used to display whatever visual content you like, ranging from a "daily babe" to a random skyscraper banner.

To create a random image block for PHP-Nuke, proceed as follows (see Random Picture Block):

Make a script called block-RandomPics.php and put it in the blocks folder. The code for block-RandomPics.php is:

<?php
$content="";
if (eregi("block-RandomPics.php",$PHP_SELF)) {
  Header("Location: index.php");
  die();
}
// $whereimgs has to be in the Domain (like localhost area) 
// or put ../ in front of folder name
$whereimgs = "../pictures";
mt_srand((double)microtime()*1000000);
$imgs = dir($whereimgs);
while ($file = $imgs->read()) {
  if (eregi("gif", $file) || eregi("jpg", $file)) {
    $imglist .= "$file ";
  }
}
closedir($imgs->handle);
$imglist = explode(" ", $imglist);
$a = sizeof($imglist)-2;
$random = mt_rand(0, $a);
$image = $imglist[$random];
$asin = explode(".", $image);
$content = "";
$content .= "<center><a href=\"PicShow.php?show=".$whereimgs."/$image\">";
$content .= "<img src=\"".$whereimgs."/$image\" border=\"0\" 
              width=\"120\" alt=\"\"><br><br>";
$content .= "Click for full picture.</a><BR></center>";
?>

The block-RandomPics.php block reads all .gif's and .jpg's from the $whereimgs directory into an array ($imglist), computes a random index between 0 and the maximum array index ($a) and uses the random index to select an image name from the array. It then writes some HTML code to display a thumbnail of 120 pixels width with a ling to the original picture. The link itself passes the show parameters on the URL as the concatenation of the $whereimgs directory and the random image name ($image) to a script we still have to write, PicShow.php.

Tip The right images path
 

Taking into account that block-RandomPics.php has to be located in the blocks folder, then, if your images folder is the folder "pictures" under the "PHP-Nuke root" directory (i.e. under the same directory where mainfile.php, index.php, config.php etc. are located), then you have to prepend a "../" to "pictures", as we did in the code, assuming that you don't want to give an absolute, fully qualified URL there.

For PicShow.php, which is the script that will show the full image, we have to write code that behaves just like a standard PHP-Nuke module (but without any administration functions), so you can see it as an example of a minimal module:

<?php
if (!isset($mainfile)) { include("mainfile.php"); }
$index = 0; 
include("header.php");
OpenTable(); 
$sl = $show;
echo " 
<center><h3><a href=\"javascript:history.back()\">Go back!<br><br>
<img src=$sl border=\"2\"></a></h3> 
</center>"; 
closetable();
include("footer.php");
?> 
Powered by TOGETHER TEAM srl ITALY http://www.togetherteam.it - DONDELEO E-COMMERCE http://www.DonDeLeo.com - TUTTISU E-COMMERCE http://www.tuttisu.it
Web site engine's code is Copyright © 2002 by PHP-Nuke. All Rights Reserved. PHP-Nuke is Free Software released under the GNU/GPL license.
Page Generation: 0.200 Seconds - 181 pages served in past 5 minutes. Nuke Cops Founded by Paul Laudanski (Zhen-Xjell)
:: FI Theme :: PHP-Nuke theme by coldblooded (www.nukemods.com) ::