View Single Post
Old 11-29-2007, 01:03 AM   #1 (permalink)
codyodell
The Wanderer
 
codyodell's Avatar
 
Join Date: Nov 2007
Posts: 12
Thanks: 2
codyodell is on a distinguished road
Default Useful snippets.

These are some of the function, classes and useful arrays I use most.

Code:
function refresh($strURL = NULL){
	$page = ($strURL == NULL)?$_SERVER['PHP_SELF']:$strURL;
	if(basename($_SERVER['PHP_SELF']) != $strURL){
		echo '<meta http-equiv="refresh" content="0;url='.$page.'">';
	}
}
function toString($str){
	echo '<br /><pre>';
	print_r($str);
	echo '</pre>';
}

function makeSafe($dangerous){
	$dangerous = trim($dangerous);
	$dangerous = htmlspecialchars($dangerous);
	return $dangerous;
}

function addQuote($string){
	return '"'.$string.'"';
}

function showErrors($arErrors){
	$dispErrors = '';
	if(is_array($arErrors)){
		if(count($arErrors) > 0){
			foreach($arErrors as $error){
				$dispErrors .= '<li>'.$error.'</li>';
			}
			
			return '<ul id="errors">'.$dispErrors.'</ul>';
		}else{
			return false;
		}
	
	}else{
		if($arErrors != ''){
			$dispErrors .= '<li>'.$arErrors.'</li>';
			return '<ul id="errors">'.$dispErrors.'</ul>';
		}else{
			return false;
		}
	}
}

class mailer{
    var $to         = "";
    var $subject    = "";
    var $message    = "";
    var $fromName   = "";
    var $fromEmail  = "";
    var $replyEmail = "";
    var $header     = "";
    var $type       = "text/html";
    var $characterSet = "ISO-8859-1";
    
    
    function send(){
        $this->createHeader();
        if (@mail($this->to,$this->subject,$this->message,$this->header)){
            return true;
        } else {
            return false;
        }
    }
    
    function createHeader(){
        $from   = "From: $this->fromName <$this->fromEmail>\r\n";
        $replay = "Reply-To: $this->replyEmail\r\n";    
        $params = "MIME-Version: 1.0\r\n";
        $params .= "Content-type: $this->type; charset=$this->characterSet\r\n";
        
        $this->header = $from.$replay.$params;
        return $this->header;
    }
    
}
/*
      $mailer = new mailer();
        
      $mailer->to         = isset($_POST['to']) ? $_POST['to'] : "";
      $mailer->fromName   = isset($_POST['fromname']) ? $_POST['fromname'] : "";
      $mailer->fromEmail  = isset($_POST['fromemail']) ? $_POST['fromemail'] : "";
      $mailer->replyEmail = isset($_POST['replyemail']) ? $_POST['replyemail'] : "";
      $mailer->subject    = isset($_POST['subject']) ? $_POST['subject'] : "";
      $mailer->message    = isset($_POST['message']) ? $_POST['message'] : "";

      if ($mailer->send()) {
        echo "Thanks for your message!";
      } else {
        echo "Sending email was failed!";
      }
*/


function fnRandomPassword($nLength = 8, $nChars = 3) {
	$minLength   = 6;
	$maxLength   = 20;
	$strPassword = "";
	
	for ( $i = 1 ; $i <= $nLength ; $i++ ) {
		# Randomize the type of this character
		# (1) Uppercase, (2) Lowercase, (3) Numeric
		$nChar = rand(1, $nChars);

		switch ($nChar) {
			case 1:
				# Uppercase character
				$strPassword .= chr(rand(0, 25) + 65);
				
				break;

			case 2:
				# Lowercase character
				$strPassword .= chr(rand(0, 25) + 97);
				
				break;

			case 3:
				# Numeric character
				$strPassword .= chr(rand(0, 9) + 48);
				
				break;
		}
	}
	
	return($strPassword);
}

function fnGetStates($strSelID) {
   $htmlData  = '<option value=""></option>';
   $hashData  = array("AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "DC" => "District of Columbia", "FL" => "Florida", "GA" => "Georgia", "GU" => "Guam", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "PR" => "Puerto Rico", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming");

   foreach ($hashData as $strID => $strName) {
      $strSel    = ($strID == $strSelID) ? "selected" : "";
      $htmlData .= '<option value="' . $strID . '" ' . $strSel . '>' . $strName . '</option>';
   }
   
   return($htmlData);
}

function getMonths($curSel){
	$dispOptions = '
	<option value="01">January</option>
	<option value="02">February</option>
	<option value="03">March</option>
	<option value="04">April</option>
	<option value="05">May</option>
	<option value="06">June</option>
	<option value="07">July</option>
	<option value="08">August</option>
	<option value="09">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>';
	return $dispOptions;
}

function getYears($numYears){
	$year = date("Y");
	$dispOptions = '';
	for($i = 0;$i < $numYears; $i++){
		$dispOptions .= '<option value="'.($year + $i).'">'.($year + $i).'</option>';
	}
	return $dispOptions;
}
__________________
The man who invented the wheel was smart. The man who invented the other 3, he was a genius.
codyodell is offline  
Reply With Quote
The Following 2 Users Say Thank You to codyodell For This Useful Post:
marxx (12-02-2007), Wildhoney (11-30-2007)