PHP re-usable functions

Return <option> tag

/**
 * Get the <option> section of a html <select>
 *
 * @param array $_dropdown_array   associative array with the keys as the values and the values as the display section
 * @param mixed $_selected_value   the selected value
 * @return string $_dropdown_list  the dropdown list
 */
function get_dropdown ($_dropdown_array, $_selected_value = '')
{
	$_dropdown_list = "";
	foreach ($_dropdown_array as $_value => $_display_name)
	{
		$_selected = "";
		if ($_selected_value == $_value) $_selected = "selected";
		$_dropdown_list .= "<option value='$_value' $_selected >$_display_name</option>";
	}	
	return $_dropdown_list;
}

Return <option> tag between values

/**
 * Gets the <option> section of a <select> tag for values in between the start and stop values
 *
 * @param int $_start_at    start value
 * @param int $_stop_at     stop value
 * @param int $_selected    the selected amoutn
 * @return string $_option_list
 */
function get_amount_dropdown ($_start_at = 0, $_stop_at = 2000000, $_selected = "")
{
  $_option_list = "<option value='0' >0</option>";
  $i = $_start_at;
  // create a fale safe for the while loop
  $_count = 0;
  while ($i <= $_stop_at)
  {
  	$mark_selected = '';
    if($_selected == $i) $mark_selected = ' selected ';
    if ($i < 30000)
    	$_increment = 1000;
    else if ($i < 50000)
    	$_increment = 2000;
    else if ($i < 100000)
    	$_increment = 5000; 
    else if ($i < 600000)
    	$_increment = 10000;
    else if ($i < 1000000)
    	$_increment = 20000;
    else if ($i < 10000000)
    	$_increment = 50000;
    else 
    	$_increment = 100000;  	
 
    // Add the options
    $_option_list .= "<option value='$i' $mark_selected>".number_format($i, 2, '.', ",")."</option>";
    $i += $_increment;  
 
    // if count go's over a 1000 loops break out of the loop
    ++$_count;
    if ($_count > 1000) break;
  }
  return $_option_list;
}

Numbers to Words

	/**
	 *  num2words confert numerical intigers into word strings
	 * @param int $number 	- is the number to convert to word strings
	 * @param string $language   - is the language to use for the words
	 * @return string  - the translated string
	 */
	function num2words ($number)
	{
		$test=((float)$number)*1;
		if (empty($test))return 'zero';
 
		$lasts=array('zero','one','two','three','four','five','six','seven','eight','nine');
		$teens=array('ten', 'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
		$teen=array('ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');
		$and = ' and ';
		$hundred = " hundred ";
		$thousand = ' thousand ';
		$million = ' million ';
 
		$numLen = strlen($number);
		if ($numLen >= 1)
		{
			$millions = "";
			$hundred_thousands = "";
			$ten_thousands = "";
			$one_thousands = "";
			$hundreds = "";
			$tens = "";
			$ones = "";
			$num = array_reverse(str_split($number));
 
			// Check if the value is in range
			if (isset($num[9]))
			{
				return "The number to word function can only handle values smaller than 1,000,000,000.00!!!!!";
			}
			// teens
			if (isset($num[1]) && $num[1] == 1)
			{
				$tens = $teens[$num[0]];
			}
			else if (isset($num[1]) && $num[0] == 0)
			{
				$tens = $teen[$num[1]-1];
			}
			else 
			{
				$tens = $teen[$num[1]-1];
				$ones = $lasts[$num[0]];
			}
			if (((isset($num[0]) && $num[0] > 0) || (isset($num[1]) && $num[1] > 0)) && $numLen > 2)
			{
				$tens = $and.$tens;
			}
			// hundreds
			if (isset($num[2]) && $num[2] > 0)
			{
				$hundreds = $lasts[$num[2]].$hundred;
			}
 
			// Thousands
			if (isset($num[4]) && $num[4] == 1)
			{
				$ten_thousands = $teens[$num[3]];
			}
			else if (isset($num[4]) && $num[3] == 0)
			{
				$ten_thousands = $teen[$num[4]-1];
			}
			else 
			{
				$ten_thousands = $teen[$num[4]-1];
				$one_thousand = $lasts[$num[3]];
			}
 
			if ((isset($num[5]) && $num[5] > 0) || (isset($num[4]) && $num[4] > 0) || (isset($num[3]) && $num[3] > 0))
			{
				$one_thousand .= $thousand;
			}
 
			// Hundred thousands
			if (isset($num[5]) && $num[5] > 0)
			{
				$hundred_thousands = $lasts[$num[5]].$hundred;
				if ((isset($num[4]) && $num[4] > 0) || (isset($num[3]) && $num[3] > 0))
				{
					$hundred_thousands .= $and;
				}
			}
 
			// Millions
			if (isset($num[7]) && $num[7] == 1)
			{
				$ten_millions = $teens[$num[6]];
			}
			else if (isset($num[7]) && $num[6] == 0)
			{
				$ten_millions = $teen[$num[7]-1];
			}
			else 
			{
				$ten_millions = $teen[$num[7]-1];
				$millions = $lasts[$num[6]];
			}
			if ((isset($num[8]) && $num[8] > 0) ||(isset($num[7]) && $num[7] > 0) || (isset($num[6]) && $num[6] > 0))
			{
				$millions .= $million;
			}
 
			// Hundred thousands millions
			if (isset($num[8]) && $num[8] > 0)
			{
				$hundred_millions = $lasts[$num[8]].$hundred;
				if ((isset($num[7]) && $num[7] > 0) || (isset($num[6]) && $num[6] > 0))
				{
					$hundred_millions .= $and;
				}
			}
			return "$hundred_millions $ten_millions $millions $hundred_thousands $ten_thousands $one_thousand $hundreds $tens $ones";
		}
		else 
		{
			return "zero";
		}
	}
 
	if (strpos($amount, "."))
	{
		$amount = round($amount, 2);
		list($int, $desimal) = explode(".", $amount);
		if ($desimal > 0)
		{
			$cents = ' and '.num2words($desimal).$money_decimal;
		}
	}
	else 
	{
		$int = $amount;
		$cents = "";
	}
	$amount_in_words = num2words($int).$money_main.$cents;
	return $amount_in_words; 
}

Recursive Directory Scan

<?php
 
 // ------------ lixlpixel recursive PHP functions -------------
 // scan_directory_recursively( directory to scan, filter )
 // expects path to directory and optional an extension to filter
 // of course PHP has to have the permissions to read the directory
 // you specify and all files and folders inside this directory
 // ------------------------------------------------------------
 
 // to use this function to get all files and directories in an array, write:
 // $filestructure = scan_directory_recursively('path/to/directory');
 
 // to use this function to scan a directory and filter the results, write:
 // $fileselection = scan_directory_recursively('directory', 'extension');
 
 function scan_directory_recursively($directory, $filter=FALSE)
 {
     // if the path has a slash at the end we remove it here
     if(substr($directory,-1) == '/')
     {
         $directory = substr($directory,0,-1);
     }
 
     // if the path is not valid or is not a directory ...
     if(!file_exists($directory) || !is_dir($directory))
     {
         // ... we return false and exit the function
         return FALSE;
 
     // ... else if the path is readable
     }elseif(is_readable($directory))
     {
         // we open the directory
         $directory_list = opendir($directory);
 
         // and scan through the items inside
         while (FALSE !== ($file = readdir($directory_list)))
         {
             // if the filepointer is not the current directory
             // or the parent directory
            if($file != '.' && $file != '..')
             {
                 // we build the new path to scan
                 $path = $directory.'/'.$file;
 
                 // if the path is readable
                 if(is_readable($path))
                 {
                     // we split the new path by directories
                     $subdirectories = explode('/',$path);
 
                     // if the new path is a directory
                     if(is_dir($path))
                     {
                         // add the directory details to the file list
                         $directory_tree[] = array(
                             'path'    => $path,
                             'name'    => end($subdirectories),
                             'kind'    => 'directory',
 
                             // we scan the new path by calling this function
                             'content' => scan_directory_recursively($path, $filter));
 
                     // if the new path is a file
                     }elseif(is_file($path))
                     {
                         // get the file extension by taking everything after the last dot
                         $extension = end(explode('.',end($subdirectories)));
 
                         // if there is no filter set or the filter is set and matches
                         if($filter === FALSE || $filter == $extension)
                         {
                             // add the file details to the file list
                             $directory_tree[] = array(
                                 'path'      => $path,
                                 'name'      => end($subdirectories),
                                 'extension' => $extension,
                                 'size'      => filesize($path),
                                 'kind'      => 'file');
                                 print "&lt;filename&gt;".substr($path, 79)."&lt;/filename&gt;<br>";
                         }
                     }
                 }
             }
         }
         // close the directory
         closedir($directory_list); 
 
         // return file list
         return $directory_tree;
 
     // if the path is not readable ...
     }else{
         // ... we return false
         return FALSE;    
     }
 }
 // ------------------------------------------------------------
	$list = scan_directory_recursively('C:/Websites');
	//print_r ($list);
?>
 
/home/dodgydevil/wiki.zendfusion.com/data/pages/scripts/php.txt · Last modified: 2009/03/14 01:46 by bosbaba
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Zend Fusion