Archive

Posts Tagged ‘name’

Find File Extension In PHP

February 23rd, 2009 1 comment

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';
$ext = substr(strrchr($fileName, '.'), 1);
echo $ext;

The output here is 'wibble';

Printing Out File And Class Information In PHP

July 18th, 2008 No comments

If you are debugging a PHP application then you might want more information than the values of some current variables. There are a number of built in magic variables that can be used to print out the file name, line number, class and method that the debug statement is printed out on. Here is an example that prints out some information from a class.

class class_test{
 
function method_test(){
  // the full path to the current file
  print 'File: '.__FILE__.'<br />';
 
  // print the current line
  print 'Line: '.__LINE__.'<br />';
 
  // print the current class name
  print 'class: '.__CLASS__.'<br />';
 
  // print the current method name
  print 'method: '.__METHOD__.'<br />';
 
  // directory separator of the current
  // system (windows = \ and linux = /)
  print 'Directory separator: '.DIRECTORY_SEPARATOR.'<br />';
 }
}
$test = new class_test();
$test->method_test();

This will print out something like the following.

File: C:\Apache Software Foundation\Apache2.2\htdocs\test.php
Line: 10
class: class_test
method: class_test::method_test
Directory separator: \

Categories: PHP Tags: , , , , ,

Assign Or Get Class Name Attribute With JavaScript

May 24th, 2008 No comments

To get the class of an element with JavaScript you use the className property of the element object. Take the following section of HTML code.

<div id="adiv" class="theClass">some text</div>

Use the following bit of code to print off the class name of the div element in a message box.

alert(document.getElementById('adiv').className);

To set the class name to something else you can use the same property, but in this case just pass it a value. The following example changes the class name of the div element to "newClass".

document.getElementById('adiv').className = 'newClass';

So how is this useful? Well accessing styles can be a bit of a pain on some browsers, so a better way is to set up some styles for different classes in your style sheets and use the className property to change the class of your elements to match your styles.

For example, lets say that you wanted to create a hover effect on an element that is not an anchor. Because it isn’t an anchor there are going to be some issues with IE6 and some other browsers not understanding the pseudo-class :hover, so an alternative is to use the className property to change the class so that it has a different style.

The following example uses the onmouseover and onmouseout events to create a hover like effect.

<div id="mydiv" onmouseover="changeClass(true);" onmouseout="changeClass(false);">Content</div>
 
<script type="text/javascript">
// <!--
 
function changeClass(set){
 if(set){
  document.getElementById('mydiv').className = 'bigfont';
 }else{
  document.getElementById('mydiv').className = '';
 }
}
 
// -->
</script>

The new class is called bigfont and simply has a style that has a bigger font size. Put the following into your stylesheets.

div.bigfont{
  font-size: 123px;
  border:1px solid black;
}

Categories: JavaScript Tags: , , ,

Finding The Current File Or Directory With PHP

May 15th, 2008 No comments

Having a header file that prints out a standard menu on a site is a good idea and saves you time in the long run as you only have to edit one file to change an item on the menu. However, what if you only want to display a menu or sub-menu when a particular page is loaded? This is a common problem, and finding out what page you are on is something that all PHP programmer come across at some point or another.

The PHP $_SERVER superglobal array has three items of interest which can be used to find out the current page. These are PHP_SELF,REQUEST_URI and SCRIPT_NAME and they all appear to have the same values but there are some subtle and important differences. Here are some examples of their values (on the right) with the original URL (on the left).

PHP_SELF
test.php = test.php
/example/ = /example/index.php
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php
/example/test.php/test/123 = /example/test.php/test/123

REQUEST_URI
test.php = test.php
/example/ = /example/
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php?id=123
/example/test.php/test/123 = /example/test.php/test/123

SCRIPT_NAME
test.php = test.php
/example/ = /example/index.php
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php
/example/test.php/test/123 = /example/test.php

So from these tests we can say that SCRIPT_NAME is probably our best bet for getting the filename that is running the code we are looking at. We can get that output like this.

$currentFile = $_SERVER["SCRIPT_NAME"];

Next, you will need to extract the parts of the path into an array and pick out the last item. This will be the filename.

$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = explode('/', $currentFile);
$filename = $parts[count($parts) - 1];

I have seem some variations on this theme, but using SCRIPT_NAME seems to be the most reliable method. One thing to watch out for is that one some server setups you might find that SCRIPT_NAME simply doesn’t exist. In this case I would use PHP_SELF as this is implemented by the language and will always be present.

To find the current directory you only need to look at the rest of the array. So if the file we are looking at has the following URL.

http://www.talkincode.com/example/test/123/test.php

Then our array contained in the $parts variable will contain the following.

[0] =>
[1] => example
[2] => test
[3] => 123
[4] => test.php

The first item is always blank because the SCRIPT_NAME variable always starts with a slash. So when we use the explode() function on it a blank item is created. This can easily be deleted by using the following:

unset($parts[0]);

Categories: PHP Tags: , , , ,

Getting All Form Objects In Access VBA

January 10th, 2008 No comments

In Access all forms are contained as objects in the AllForms collection, which is part of the Application.CurrentProject object. To iterate through them and print them off just load them one by one into a temporary object and use the Name property of the form object to print off its name.
Sub AllForms()
  Dim obj As AccessObject, dbs As Object
  Set dbs = Application.CurrentProject
  ' Search for open AccessObject objects in AllForms collection.
  For Each obj In dbs.AllForms
    ' Print name of obj.
    Debug.Print obj.Name
  Next obj
End Sub

If you want to get a list of all of the active form objects (ie. those forms that are currently open) then use the IsLoaded property for each object.
Sub AllForms()
  Dim obj As AccessObject, dbs As Object
  Set dbs = Application.CurrentProject
  ' Search for open AccessObject objects in AllForms collection.
  For Each obj In dbs.AllForms
    If obj.IsLoaded = True Then
      ' Print name of obj.
      Debug.Print obj.Name
    End If
  Next obj
End Sub