SL8R.co.uk - Code Zone

Code, Code and Code

SL8R.co.uk - Code Zone header image 1

PHP: Select the latest image or file in a folder

July 23rd, 2008 · No Comments

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 );
}

→ No CommentsTags: Code

Javascript: CSS rollover List routine

June 18th, 2008 · No Comments

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…

→ No CommentsTags: Code

PHP: Add Slashes to text

June 3rd, 2008 · No Comments

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…

→ No CommentsTags: Code

Javascript: Set Focus of HTML Textbox

May 23rd, 2008 · 1 Comment

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…

→ 1 CommentTags: Code

PHP: Checking a DNS entry

May 22nd, 2008 · 1 Comment

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…

→ 1 CommentTags: Tip

Java: Mix up items in array

May 20th, 2008 · No Comments

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…

→ No CommentsTags: Code

J2ME: Bounds checking of a List

May 8th, 2008 · No Comments

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…

→ No CommentsTags: Code

PHP: Avoiding mySQL Injections

April 30th, 2008 · No Comments

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…

→ No CommentsTags: Code

PHP: Require() and Include()

April 23rd, 2008 · No Comments

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…

→ No CommentsTags: Comment

PHP: Defining Constants

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”);
?>

→ 1 CommentTags: Code