| Subcribe via RSS

Viagra online
XANAXadderall onlineLevitraPuppies for sale

Passing Arrays to PHP via AJAX

I've written on processing AJAX queries before but neglected discussing how to pass an array to PHP for processing. This is an example of getting all checked rows in an EXTJS grid and posting them to an awaiting PHP script.

First we have the users confirm they really want to process all checked records:

 
Ext.MessageBox.confirm('Confirm', 'Are you sure your want to email login info to all selected users?', confirmEmailUsers);
 

Now the jQuery method:

 
function confirmEmailUsers(btn){
   if(btn=='yes'){
	   var recArray=new Array();
	   var ids=new Array();
	   var recArray = mailqueueGrid.getSelectionModel().getSelections();
	   for (var i = 0 ; i < recArray.length ;i++) {
		  ids[i] = recArray[i].get('id');
	   }
	  $.ajax({
		type: "POST",
		dataType: 'json',
		url: '/cpsia/?c=form&m=processqueue',
		data: 'ids='+ids,
		success: function(msg){
			alert(msg)
		}
	  });
   }
}
 

Finally, the PHP script to processes it:

 
$ids = explode(",",$_POST['ids']);
foreach ($ids as $id) {
   echo $id .'';
}
 
Tags: , , ,

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: , , ,

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: , , ,