473,807 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to capture only 5 random urls from a directory of hundreds

This code:

<?
//PHP SCRIPT: getimages.php
Header("content-type: application/x-javascript");

//This function gets the file names of all images in the current
directory
//and ouputs them as a JavaScript array
function returnimages($d irname="images" ) {
$pattern="(\.jp g$)|(\.png$)|(\ .jpeg$)|(\.gif$ )"; //valid image
extensions
$files = array();
$curimage=0;
if($handle = opendir($dirnam e)) {
while(false !== ($file = readdir($handle ))){
if(eregi($patte rn, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'fadeimages['.$curimage.']=["images/'.$file .'", "", ""]';
echo "\n";
$curimage++;
}
}

closedir($handl e);
}
return($files);
}

echo 'var fadeimages=new Array();'; //Define array in JavaScript
echo "\n";

returnimages() //Output the array elements containing the image file
names
?>

captures every file in the images directory into the fadeimages array.
How can I tell php to randomly capture only 5 or so of the images and
store only those in the array? Any help would be greatly appreciated!

Dec 16 '05 #1
2 1338
NC
gu********@gmai l.com wrote:

This code captures every file in the images directory into
the fadeimages array. How can I tell php to randomly
capture only 5 or so of the images and store only those
in the array?


Instead of outputting every file name, store file names in an array
(lets say, $files) and then randomly draw five elements from that
array:

$n = count($files) - 1;
for ($i=1; $i <= 5; $i++) {
echo 'fadeimages[', $i, ']=["images/', $files[rand(0, $n)] ,'", "",
""]';
}

For more information on rand(), see the Manual:

http://www.php.net/rand

Cheers,
NC

Dec 16 '05 #2
Thanks for the info.

Dec 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2949
by: AES/newspost | last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?) standard Internet servers a URL can point to a subdirectory name followed by a backslash, and that links to this URL will automatically go to an "index.html" file located in that subdirectory, if there is one. Q1: Is this more or less correct? At least some HTML texts also recommend using this as the basis for organizing a web site: Put the web pages associated...
5
2603
by: Linda | last post by:
Greetings: I have found a random image script that I like. It is located here: http://www.javascriptcity.com/scripts/local/simage3.htm I'd like to edit this to have 4 different slots for images with a different set of 5 images for each slot, something like this page: http://www.uchsc.edu The script says /* no need to edit past this point (unless you want to
3
1633
by: groucho | last post by:
I want to click on text and have it open a different *random* webpage every time from a list of urls I have already provided. So if I have 3 pages - p1, p2, p3 - i want to be able to click the text link and come up with, for e.g.: p2, p2, p3, p1, p2, p2, p2, p1 etc... Thanks Matt
7
1252
by: Roshawn Dawson | last post by:
Hi, I have an xslt file located in the root directory. It is used by an aspx pages in both the root directory and a subdirectory. But for some strange reason, the aspx page in the subdirectory can neither locate the xslt file or the css file needed to get the desired results. What's the problem? Can anyone help me? Thanks, Roshawn
22
3461
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
10
2559
by: Mike.Duffy | last post by:
What is the default directory in the client file system that is used for file urls? From performing a few experiments, it is clear that an unqualified reference from another file url is always directed to the source directory. I.e., from within url "file://c:/abc/def.htm", if you make a reference like <href="ghi.htm">, or <img src="jkl.gif">,the browser will look for "file://c:/abc/ghi.htm" or "file://c:/abc/jkl.gif".
8
1779
by: Lloyd Sheen | last post by:
I have a list of JPG's which are found in a SQL Server database. When the page selects a certain piece of data it will refer to the file system (resident on IIS server with a virtual directory) and files contained within a certain folder. I have been trying for quite a while to set the ImageURL of an image control to the correct information. I have googled till my mind hurts but can find nothing to help. I have even tried to copy the...
1
1977
by: bn4 | last post by:
I have been trying to modify a script that i had working in the past but have lost.. ORIGINAL URL: http://www.thescripts.com/forum/thread147097.html STEPS: I want it to do the following: 1-Load a Random Image each time page loads or is refreshed / script starts (with corresponding url link when clicked) 2-Images change by blend/fade into one another, on an adjustable time delay 3-Next image loaded also loads new corresponding url/link
4
1399
by: hdbbdh | last post by:
Hello everyone, I am trying to capture URLs in HTML file which appears like <ahref=> I found this code but it does not work well.
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.