Closed Thread Icon

Preserved Topic: PHP/CSS Come together... Pages that link to <a href="https://ozoneasylum.com/backlink?for=24791" title="Pages that link to Preserved Topic: PHP/CSS Come together..." rel="nofollow" >Preserved Topic: PHP/CSS Come together...\

 
Author Thread
Ensellitis
Bipolar (III) Inmate

From: New York, USA
Insane since: Feb 2002

posted posted 01-25-2005 04:27

I just thought I would share this with you all... I needed a way to get random headers, and since this was done via stylesheets, I combined PHP with CSS.

I am sure there is an easier way, but this is how *I* did it...

code:
<style type="text/css">
#header {
position: absolute;
top: 13px;
left: 155px;
background-image: url("<?php

// Change this to the total number of images in the folder
$total = "3";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".gif";

// Change to the location of the folder containing the images
$image_folder = "headers";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "$image_folder/$image_name";

?>");
background-position: top right;
background-repeat: no-repeat;
margin: 0;
height: 170px;
width:450px;
padding-left: 45px;
text-align:center;
font-family: Georgia;
font-weight: normal;
font-size: 3em;
z-index:5;
}
</style>



I couldn't make it work by actually using my stylesheets, so I just did it this way. I saw some pages that where doing this via javascript, but I didn't like how it looked...

Hope someone finds this useful...

Check out my site for a live example, http://www.ensellitis.com



:: WWW || Contact || Deviations || ThoughtPrism ::

(Edited by Ensellitis on 01-25-2005 04:35)

Ensellitis
Bipolar (III) Inmate

From: New York, USA
Insane since: Feb 2002

posted posted 01-25-2005 05:15

Oh, and the images have to be named 1, 2, 3, and so on...



:: WWW || Contact || Deviations || ThoughtPrism ::

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 01-25-2005 07:05

And in that case this should, in fact, be over in the server side forum, not here in DHTML/Javascript


Justice 4 Pat Richard

Ensellitis
Bipolar (III) Inmate

From: New York, USA
Insane since: Feb 2002

posted posted 01-25-2005 07:22

Sorry, I wasn't sure where it should go... If a moderator could please move it?



:: WWW || Contact || Deviations || ThoughtPrism ::

InI
Maniac (V) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 01-25-2005 08:29

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 01-25-2005 12:17

Indeed. I would have moved it myself, but I seemed to be having some weird Javascript bug in Firefox whereby selecting the forum and hitting the 'move' button yielded the error 'form[0] has no properties' or something like that.


Justice 4 Pat Richard

zavaboy
Bipolar (III) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 01-25-2005 13:36

Another way, so the files don't need special names:

code:
// Your directory
$dir = 'headers';

// Open directory
if ($handle = opendir($dir)){

// Get the files in the directory
while (false !== ($file = readdir($handle))){

// Make sure it's what you want
if (!is_dir($dir.'/'.$file) && preg_match('#(\.gif|\.jpg|\.png)$#i',$file))

// Add it to an array
$file_bag[] = $file;
}
// Close the folder
closedir($handle);

// Get a random key from the array
$rand = array_rand($file_bag);

// Pick out the file
$picked_file = $dir.'/'.$file_bag[$rand];

// If you couldn't open the directory, have a backup plan
}else $picked_file = $dir.'/default_image.jpg';

// Output it
echo $picked_file;



[edit]I haven't tested this BTW...[/edit]



(Edited by zavaboy on 01-25-2005 13:46)

« BackwardsOnwards »

Show Forum Drop Down Menu