A PHP newbie's first project

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!

Moderator: General Moderators

Post Reply
HellBoy
Forum Newbie
Posts: 6
Joined: Sat Aug 10, 2002 7:05 pm
Location: Norway
Contact:

A PHP newbie's first project

Post by HellBoy »

Okay, I'm an absolute PHP newbie, been doing tutorials, reading about everything related to PHP.

The main reason why I got interested in PHP was to complete a little goal I set myself - and because I'm partly lazy.

Well, the deal is - I got about... 1300 musicvideos of different sorts, that all lie in my E:\Musicvideos\A, \B, \C, and so forth, according to the first name of the artist.

Now I've gotten a program to create about 1300 screengrabs, one from each video, and putted them in /videoimages on my webserver.
The musicvideo's are mapped with the alias /musicvideo which points to E:\Musicvideos

What I now want to, is to make a PHP-script that randomly loads up any of the preloaded images from f.ex http://localhost/videoimages/A/Adema - Giving In.gif, and makes a link to http://localhost/musicvideos/A/Adema - Giving In.mpg.

Since this is a bunch of videos and images, I want to make the PHP-script load them all in automaticly, without having to know anything else than the folders.

As I said, I'm a complete and utter newbie, so I could really use some hints and pointers on how to get started on this.


-hellboy-
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Ok first make a script that reads the directory of the .gif's and places them in a string.

Like so:

Code: Select all

if ($handle = opendir("Your_imagedir")){

while (false !== ($file = readdir($handle))){

if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){

$filelistї] = "$file";
}
}
closedir($handle);
}
sort($filelist);
reset($filelist);

$count = count($filelist);
after this you should have each .gif in the string $filelist[] and also $count is the variable that counted the nr of images in the list.

Look up the rand (random) function on http://www.php.net and see how you can get a random nr between 1 and $count. And make that the variable $random

Then print the random image from the list on your page.

Code: Select all

print "<img src="directory\$filelist&#1111;$random]">";
that should get you started ;)
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Oh and I forgot you have to deduct 1 from $count since counting starts with 1 but when reading from the string later starts at 0... so basicly make the $random variable between 1 and $count and deduct 1 from $random then... that should be easiest.
HellBoy
Forum Newbie
Posts: 6
Joined: Sat Aug 10, 2002 7:05 pm
Location: Norway
Contact:

Post by HellBoy »

Something's wrong somewhere, the img src is now "http://mymachine.mydomain/directory$filelist[23]", where 23 changes randomly, so it have counted the number of images in the specified folder, but it seems like it's unable to get the imagenames...

I've probably messed up somewhere :p

Code: Select all

<?php
	
if ($handle = opendir("videoimages"))&#123; 

while (false !== ($file = readdir($handle)))&#123; 

if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html")&#123; 

$filelist&#1111;] = "$file"; 
&#125; 
&#125; 
closedir($handle); 
&#125; 
sort($filelist); 
reset($filelist); 

$count = count($filelist, - 1);
$random = rand (1, $count);

print "<img src="directory\$filelist&#1111;$random]">"; 
	
?>
HellBoy
Forum Newbie
Posts: 6
Joined: Sat Aug 10, 2002 7:05 pm
Location: Norway
Contact:

oops, my bad

Post by HellBoy »

Code: Select all

print "<img src="videoimages/$filelist&#1111;$random]">";
:)


Now, for the linking to the musicvideos I'm a bit curious - the image-script wasn't able to fetch images that was in subdirectories of "videoimages", which would probably have mad it easier, as the musicvideos are sorted into folders, artists sorted by name.

The gif's names are now the original filename, just with an added .gif at the end, giving filenames like Aphex Twin - Windowlicker.mpg.gif.

So all I now need to do, is find a way to...

1) grabbing filenames from subfolders
2) making the a-href use the original location, just switching "videoimages" with "musicvideos" and removing .gif
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Well actually... what you want to do is this.

In the function where you get the contents of videoimages.. you want to say that if it encounters a directory, it should also add that directory/the_contents in $filelist.

Sorry... I personally haven't done that yet, so I'm not much help there. I think you need to check the isdir function (if that's it.. am not sure)

The trick is... assuming that you have exactly the same filenames (apart from mpg/gif) and subfolder names for the video files and .gif's.

Well ok here goes.... see the sort($filelist) function? that sorts the filenames in $filelist alphabeticly.

So say you create a link like so.

print "<a href=\"$the_view_mpg_page?id=$random\"><img src=\"videoimages/$filelist[$random]\"></a>";

Now by doing this you submit the random number as $id to the next page.

So then you again let the script put the filenames in the $filelist string, but this time from the vieo directory, and just have it print out $filelist[$id] which is the random number from the page before. So you don't need the random function on this page.. since you know which file.
And since both strings have the same alphabetical order $id should get you the corresponding file.
HellBoy
Forum Newbie
Posts: 6
Joined: Sat Aug 10, 2002 7:05 pm
Location: Norway
Contact:

Post by HellBoy »

...you talk about different pages here, how do I specify all this? I'm a bit lost now...
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Ok right of course you don't want to see the video inside a paeg.. you jsut want to access it through the image's link.

Even simpler.Just try this.

Code: Select all

<?php 
    
if ($handle = opendir("videoimages"))&#123; 

while (false !== ($file = readdir($handle)))&#123; 

if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html")&#123; 

$imagelist&#1111;] = "$file"; 
&#125; 
&#125; 
closedir($handle); 
&#125; 
sort($imagelist); 
reset($imagelist); 

$count = count($imagelist, - 1); 
$random = rand (1, $count); 

if ($handle = opendir("musicvideos"))&#123; 

while (false !== ($file = readdir($handle)))&#123; 

if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html")&#123; 

$videolist&#1111;] = "$file"; 
&#125; 
&#125; 
closedir($handle); 
&#125; 
sort($videolist); 
reset($videolist); 




print "<a href="musicvideos\$videolist&#1111;$random]"><img src="videoimages\$imagelist&#1111;$random]"></a>"; 
    
?>

See just create 2 strings, one for the image, 1 for the video, use the video one for the link and the image one for the image.

Now you must make sure of course that for each image there is a correspondnig video file with basicly the same spot when all is placed in alphabetical order.

I think that's what you want, but I also think you could have figured this last step out yourself ;)
LC
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

make life easy...

Post by fatalcure »

here u go man, this should work for u...it gets all the mpgs stored in the folder and subfolers and puts it in array, gets a radom mpg from the array, and adds a .gif to the end...since ur files are name thisfile.mpg and thisfile.mpg.gif...

Code: Select all

<?php

$fileArray = array();

function get_files($location) &#123;
	global $fileArray;
	$hook = opendir($location);
	while (($file = readdir($hook)) !== false) &#123;
		if ($file != "." && $file != "..") &#123;
			$path = $location . "/" . $file; 		// set up full path
			if (is_dir($path)) get_files($path);
			else array_push($fileArray, $path);
		&#125;
	&#125;
	closedir($hook);
&#125;

$location = "musicvideos";
get_files($location);
$random = rand(1, sizeof($fileArray)) - 1 ; 

echo "<a href='" . $fileArray&#1111;$random] . "'>";
$img = "videoimages" . str_replace($location, "", $fileArray&#1111;$random]) . ".gif";
echo "<img src='$img' border='0'></a>";
?>
:)
HellBoy
Forum Newbie
Posts: 6
Joined: Sat Aug 10, 2002 7:05 pm
Location: Norway
Contact:

Post by HellBoy »

thanks a lot, both of you'll :)

I'm heading off to work now, so I'll test 'em when I get back.
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

cool, post again after you try it and tell me if mine works for ya, i just threw it together, but it seems like that code is what u wanted...

peace
Post Reply