| Subcribe via RSS

Viagra online
XANAXadderall onlineLevitraPuppies for sale

Read XML Files from Directory & Process

March 16th, 2010 | No Comments | Posted in Linux, PHP Snippets

Read files in a directory. Ignore hidden files. Processed XML based on first couple characters of file name.

 
$xmlDirectory = opendir($this->xml_dir);
while($entryName = readdir($xmlDirectory)) {
	$dirArray[] = $entryName;
}
closedir($xmlDirectory);
 
$indexCount	= count($dirArray);
//Print ("$indexCount files<br>\n");
sort($dirArray);
for($index=0; $index < $indexCount; $index++) {
	if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
		if(substr("$dirArray[$index]", 0, 4)=='some'){
			$this->somefiletype_path= $this->xml_dir.$dirArray[$index];
			$this->doSomething();
		}else{
			//do something else
		}
	}
}
 
Tags: , , ,

Random Photo on Page Load (jQuery)

December 22nd, 2009 | No Comments | Posted in PHP Snippets
 
function randomPhoto(){
  var photoArray=new Array()
  photoArray[1]="imgs/photo_video_guitar.jpg"
  photoArray[2]="imgs/photo_video_girl.jpg"
  photoArray[3]="imgs/photo_video_boy.jpg"
  var imagePosInArray=Math.floor(Math.random()*photoArray.length)
  if (imagePosInArray==0)
    imagePosInArray=1;
    $("div.tab-content#case-study img").attr("src",photoArray[imagePosInArray]);
}
 
Tags: , , ,

Word Wrap with Ellipses in PHP

August 19th, 2008 | No Comments | Posted in PHP Snippets

Word wrap with ellipses:

 
function prepareDescription($text,$maximum) {
    $trim_numb=$maximum-3; //for ellipses
    $text = html_entity_decode($text, ENT_QUOTES);
    if (strlen($text) &gt; $trim_numb) {
   $text = substr($text, 0, $trim_numb);
   $text = substr($text,0,strrpos($text," "));
   //This strips the full stop:
  if ((substr($text, -1)) == ".") {
   $text = substr($text,0,(strrpos($text,".")));
  }
  $etc = "...";
  $text = $text.$etc;
    }
    $text = htmlentities($text, ENT_QUOTES);
$wrapdesc=wordwrap($text, $maximum/2, "\n", false); //true will cut word
    return $wrapdesc; //wrapped and truncated
}
 
Tags: , ,

Query MySQL by Date Range Using Timestamp

August 19th, 2008 | No Comments | Posted in MySQL Snippets, PHP Snippets

Using MySQL TIMESTAMP, as opposed to DATETIME,  only requires a slight modification to our code in order to retreive records that fall within a specific date range (column_name becomes DATE_FORMAT(column_name,'%Y-%m-%d')).

Example usage:

  • Today = " AND DATE_FORMAT(date_added,'%Y-%m-%d') = curdate() "
  • Tomorrow= " AND DATE_FORMAT(date_added,'%Y-%m-%d') = date_sub(curdate(),INTERVAL 1 DAY) "
  • This Week= " AND year(DATE_FORMAT(date_added,'%Y-%m-%d'))=year(curdate()) "
  • Last Week= " AND year(DATE_FORMAT(date_added,'%Y-%m-%d'))=year(date_sub(curdate(),INTERVAL 7 DAY)) and week(DATE_FORMAT(date_added,'%Y-%m-%d'))=week(date_sub(curdate(),INTERVAL 7 DAY))) "
  • This Month= " AND year(DATE_FORMAT(date_added,'%Y-%m-%d'))=year(curdate()) and month(DATE_FORMAT(date_added,'%Y-%m-%d'))=month(curdate()) "
  • Last Month= " AND year(DATE_FORMAT(date_added,'%Y-%m-%d'))=year(date_sub(curdate(),INTERVAL 1 MONTH)) and month(DATE_FORMAT(date_added,'%Y-%m-%d'))=month(date_sub(curdate(),INTERVAL 1 MONTH)) "
Tags: , , , ,

Remove Slashes with PHP

August 18th, 2008 | No Comments | Posted in PHP Snippets
 
if (get_magic_quotes_gpc()){
    function stripslashes_deep($value){
        $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
        return $value;
    }
    $_POST = stripslashes_deep($_POST);
    $_GET = stripslashes_deep($_GET);
    $_COOKIE = stripslashes_deep($_COOKIE);
    $_REQUEST = stripslashes_deep($_REQUEST);
}
 
Tags: ,

Autoload Classes with PHP

August 16th, 2008 | No Comments | Posted in PHP Snippets

The following represents a useful function for autoloading classes. This example assumes all class files are named "something.class.php"

 

 
function __autoload ($class_name) {
  $file = 'path-to-file' . $class_name . '.class.php';
  if (!file_exists ($file)) {
      //do something
   echo 'File '.$class_name. ' does not exists';
   die;
  }
  require_once ($file);
}
 
Tags: , ,

Check for Existing Function

August 15th, 2008 | No Comments | Posted in PHP Snippets
 
if (function_exists('function_name') )
    function_name(arg);
 
Tags: , , ,