FUNCTIONS FROM / BOOK/ FUNCTIONS/ HTM L.PHP

FUNCTIONS FROM / BOOK/ FUNCTIONS/ HTM L.PHP

These functions make it easier to create common HTML tags. Most of the functions in this file are very similar. But before we get to these, you will need to understand the get_attlist() function, which has been added to the basic.php file.

GET_ATTLIST( ) This function takes an associative array and creates name=”value” pairs suitable for HTML tags.

function get_attlist ($atts=””,$defaults=””) {

$localatts = array(); $attlist = “”;

if (is_array($defaults)) { $localatts = $defaults; } if (is_array($atts)) { $localatts = array_merge($localatts,

$atts); } while (list($name,$value) = each($localatts))

{ if ($value == “”) { $attlist .= “$name “; } else { $attlist .= “$name=\”$value\” “; }

} return $attlist;

} No matter the base tag, all HTML tags take attributes in the form name=”value” .

This function will build an attribute list for any tag. The function will be called by other functions that write out individual HTML tags. As you can see, this function takes two arguments. The second is an array with a set of attributes required by a specific tag. For instance, an <img> tag isn’t much good without an src attribute. So if the function is called from another function that creates images, the second argument should be an array with one element, something like $myarray = array(“src” =>”myimage.gif”) .

The first argument will take another array containing other attributes. For the <img> tag, that first array might contain alt text, width, and height — $myarray = array(“alt” =>”My Image”, “width”=>”20”, “height”=>”25” .

230 Part III: Simple Applications

If appropriate, these two arrays will be merged into one. Then, from this merged array, a string is created that has the “name”= value pairs. If a value is empty, the name will exist without a value.

Note that elements passed in the second array will overwrite those in the first, enabling you to overcome default values easily. This occurs because in the array_ merge() function, if there are two elements with the same associative key, the last one will overwrite the previous one. This allows other functions that create HTML tags to keep a set of defaults in the first argument and values for the specific call in the second.

Take a look at the following functions to get a better idea of how this works. ANCHOR_TAG( ) This function creates an anchor tag.

function anchor_tag($href=””,$text=””,$atts=””) {

$attlist = get_attlist($atts,array(“href”=>$href)); $output = “<a $attlist>$text</a>”; return $output;

} For an anchor tag, there are only two things you could really expect every time:

an href attribute and some text to go between the opening enclosing <a> tags. However, it is possible that a name attribute might be helpful. But more often than not, the call to this function will be something like this:

anchor_tag(“myurl.com/index.html”, “this is a great link”);

Note that if there were a third argument, it would have to be in the form of an array. These arguments are then sent to the get_attlist() function and turned into a usable string, which is put together in the $output variable.