PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
OK someone plz save me
Ive been assigned to helping build paintboard.ca and im already stuck We need to be able to show random pics from the database so I need a php script that will look in a folder for any .htm files and pick one at random.
The pictures are all converted to .htm are numbered in ordered numeric sequence such as 100.htm, 101.htm, 102.htm when they are saved.
The best I can come up with is this playing with perl but I would much rather use php, as I can incude it much easier in the page, and wont need a cgi-bin
[syntax="perl"]#!/usr/bin/perl
#----------------------------------
# RANDOM PAGE BETA1.0
# Kendra Adams
#----------------------------------
$base_url = "/db/";
$datapath = "/localhost/paintboard/db/";
$align = "left";
$border = "1";
print "Content-type: text/html\n\n";
# Search Directory for htm or html Files
opendir (FILELIST, "$datapath");
rewinddir (FILELIST);
@dirlist = grep(!/^\.\.?$/, readdir (FILELIST));
closedir (FILELIST);
$num_in_dir = @dirlist; # the number of items in @dirlist
$counter = 0;
while ($num_in_dir > $counter) {
@check = split(/\./, $dirlist[$counter]);
if ($check[1] =~ /jpg/i || $check[1] =~ /gif/i){
push (@files, $dirlist[$counter]);
}
$counter++;
}
srand(time ^ $$);
$num = rand(@images)
print qq~
<img src="$base_url$images[$num]" border=$border align="$align">
~;
exit;
Someone plz help I just dont know what 2 do....?? I have no problem including the picture, just the random part, once I have the filename im good but Ive always used an array for random so im lost picking a random file from a folder.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="feyd"]you can use [url=http://php.net/function.opendir]opendir()[/url]-[url=http://php.net/function.readdir]readdir()[/url] or [url=http://php.net/function.glob]glob()[/url] to look at a directory for files. [url=http://php.net/function.shuffle]shuffle()[/url] can work to select a random element, or simply use [url=http://php.net/function.rand]rand()[/url] or [url=http://php.net/function.mt_rand]mt_rand()[/url] against the [url=http://php.net/function.count]count()[/url] of files you find.[/quote]
hmmm okay..
Ive been playing with it but not quite tuned? Any ideas how to improve on this??
<?php
//Database folder
//open directory to be browsed
$Dir=opendir("db");
//Close the directory
closedir($Dir);
//Create Array for files
//instantiate array and counter for **file** selection
$files=Array();
$counter=1;
//Open selected dir for browsing
$Dir2=opendir("db/".$folder);
//loop through directory, push each file into the array
while ($doc=readdir($Dir2)){
if (strlen($doc)>2){
//line below checks for .htm extension
$htmlcheck=substr($doc, -4);
//line below only allows files with extension .htm into the array
if ($htmlcheck == ".htm"){
$files[$counter]=$doc;
$counter=$counter+1;
}
}
}
//count number of array elements, select a random index number, and redirect the browser to the random file
$numfiles=count($files);
$random=(rand(1,$numfiles));
HEADER("Location: db/".$folder."/".$files[$random]);
?>
It works but sometimes shows the same a few times in a row, what am I doing wrong??
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I did enclose it in a code tag, You can still understand the code without the highlights If you ask me that makes it harder to debug using relative tags.
Anyways its ur house
~sits in the corner~
Kendra
Last edited by kendra on Sat May 06, 2006 11:35 pm, edited 1 time in total.
the call to rand() won't have any memory of previous runs, so it's possible the result may be the same especially when the range it can return is rather small. If you use shuffle() you will could have unique entries on each successive call provided you stored the results from it in a more persistant place such as in sessions.
Well theres thousands of files in the folder that are entered into the array. I just seems like its not producing a realistic "random effect" if u know what I mean.
I realize that the array is not stacked, I can do that but being as there is so many I dont think I need to do that to get a more random effect.
PS: I have the php manual, and sometimes its just hard to get functions to work together for you, even if you do know them all which I dont, but I can look them up.
I was hoping somone would offer ideas, so I came here and asked
I didnt realize that getting actual script examples was like pulling teeth here, for some reason I got the impression the purpose of the forums was to help one another.
I know If I had a function or a script that someone was looking for I would surely help and post it for others to see, but thats me.
Neways floyd thanks for your help I feel so wanted here