It can be useful to set a placeholder in a web page, such as an image, and for this place holder to be set to be the latest file in a given folder; or to select a random image/file from a given folder and set it as the placeholder. For example in an image of the day or daily quote scenario.
// set this value to 1 for a random image, or zero for the latest image
$userand = 0;
// folder where the images are located
$dir = "targetfolder";
// filemask to match images
$filemask = "*.{gif}";
// name of the target image
$oldfile = "placeholderfile.gif";
//set a random seed using the system time
mt_srand( (double)microtime()*1000000 );
// change to the given directory
if ( $dir )
{
// change to the given directory
chdir( $dir );
// remove the old latest image - if it exists
if ( file_exists ( $dir . "/" . $oldfile ) ) { unlink( $dir . "/" . $oldfile ); }
// $files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
$files = glob( $filemask, GLOB_BRACE );
// count the number of files returned
$countfiles = count($files);
// only continue if files are found
if ( $countfiles > 0 )
{
// sort the files into date order
usort( $files, 'compare_filetime' );
// copy the selected file
if ( $userand == 0 )
{
// copy the file
copy ( $dir . "/" . $files[0] , $dir . “/” . $oldfile );
}
else
{
// select a random file
$randfile = mt_rand (0, $countfiles-1 );
// copy the random file
copy ( $dir . “/” . $files[$randfile] , $dir . “/” . $oldfile );
}
}
}
// compare the dates/times of files and return the latest one
function compare_filetime( $file1, $file2 )
{
return filemtime( $file2 ) - filemtime( $file1 );
}
Tags: Code
To quickly loop through a list node and set a rollover CSS value, use the following // Set all LI of a node to rollover CSS
// Use two CSS [...] Continue Reading…
Tags: Code
It is sometimes necessary to maintain the special characters in text, such as the apostrophe, speech marks, null character and backslash as these can be mis-interpreted by systems such [...] Continue Reading…
Tags: Code
To set the focus of a HTML text box include this short piece of code in the <head> section
<script>function setfoc() { document.f1.KeyWords.focus(); }
In the example above f1 is the [...] Continue Reading…
Tags: Code
It is often useful to check that a given domain name or host is valid, this can be achieved using the checkdnserr() command, as in the following example:
$hostname = [...] Continue Reading…
Tags: Tip
Quick routine to mix up the items in an array
public Object[] MixUpArray(Object inarray[])
{
// get the size of the array
int iSize = inarray.length;
// create an array the [...] Continue Reading…
Tags: Code
This short routine shows how to ensure a selected item in a list is within bounds. If the list is empty the getSelectedIndex will return -1. The index starts [...] Continue Reading…
Tags: Code
Sample code to avoid SQL injections using mysql_real_escape_string (http://uk.php.net/mysql_real_escape_string) which converts special characters to escape sequences to ensure they are suitable for submission to SQL
$mySQL = “UPDATE address SET [...] Continue Reading…
Tags: Code
Essentially, require() and include() act in much the same way in that they both include the contents of another file - the only difference being that if include() fails [...] Continue Reading…
Tags: Comment
April 23rd, 2008 · 1 Comment
To define a constant, use the format:
define(constant_name,constant_value,case_insensitive)
e.g.
define(”MYCONSTANT”,”This is the value of the constant”,false);
print “Constant is ” . constant(”MYCONSTANT”);
?>
Tags: Code