Friday, April 15, 2011

PHP Class

Classes in Php are constructed similar to the class's in java-script using the this keyword. When calling a method in Php, the syntax is name_of_class->name_of_method. Before calling the method you must initialize it using a variable and the new keyword.

Example:

$myclass = new name_of_class;
$myclass->name_of_method();

When using class's in Php you construct the class similar to java-script. A construct method is used to store the variables used in the class, and is accessed using the name_of_class->$variable. When sending information to the screen with variables the syntax is as follows: echo "The hen laid an,".$name_of_class=$variable. To use a parameter passed into the function, the syntax is similar.

Example:

echo $this->name_of_class.$parameter

To configure the class use the class keyword. The construct is defined as public function __construct().

Example:

//The example was taken from something I needed. It takes a directory path as a parameter, and then //displays the contents of that directory. Since Ignore the string1 variable and the line that says, echo //"Show_New_Apps"."[BRK]".$string1."[BRK]";.  I will be using that code to output the screen //contents to a specific area of the screen, using Ajax, for the project I am working on. The contents //is sent back, and split off into an array. It is parsed, evaluated, and placed into the correct tag.

class file_operations
  {
 
    // constructor
    public function __construct() {
        $this->test = 120;  //Define variable
    }
   
    public function list_rentalapp_docs($directory) {
        $count=0;
        $clr="#CCFFFF";
        $dir = $directory;
       
        // Open a known directory, and proceed to read its contents
        //if (is_dir($dir)) {
        //echo "yes";
           if ($dh = opendir($dir)) {
               while (($file = readdir($dh)) !== false) {
                   $count=$count+1;
                  
                   if ($count==2){
                           $clr="#66CCFF";
                           $count=0;
                   }
                   else
                   {
                           $clr="#CCFFFF";
                   }
                  
                   $string1 .= "
";
                   $string1 .= "
";
                   $string1 .= "$file";
                   $string1 .= " ";
                   $string1 .= "Delete";    
                   $string1 .= "
";
                          



                 echo ""."$file"." "."Delete"."";
                  //echo "Show_New_Apps"."[BRK]".$string1."[BRK]";

               }
               closedir($dh);
           }
      }
  }


Use the class as part of your script or call it in using the require function. An example of this function can be found at fpdf.org.

For more information visit the site for the Php manual. This was a basic tutorial on Php classes. It is a starting point. There is a lot more complexity involved, but this is the foundation. For most programming projects, this is all you need. Always keep it simple. I hope you enjoyed this tutorial and found it informative.

Additonal Resources:

Php Manual 

Recommended PC Products

  • Speed up your computer. 
  • Permanently delete files.
  • Fix registry problems
  • Uninstall software without registry traces
  • Clean up the tracks of your Internet browsing

Get PC Optimizer Pro

Get started with your web design dreams!

Get the Web Design Business Start Up Kit. A 200 page Ebook with 67 ready made contracts. 

If you want your own web design business, but don't know where to start, this book will get you started.  

See the first 3 chapters for free.








Saturday, April 2, 2011

Mail Attachment with PHP

Mail attachments are tricky with php are tricky. After hours of research, and allot of trial and error I finally learned how to do mail attachments.

Example:



$to =     'ilive72@hotmail.com';
$subject =     'PHP Mail Attachment Test';
$bound_text =     "jimmyP123";
$bound =     "--".$bound_text."\r\n";
$bound_last =     "--".$bound_text."--\r\n";
     
$headers =     "From: admin@server.com\r\n";
$headers .=     "MIME-Version: 1.0\r\n"
      ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
     
$message .=     "If you can see this MIME than your client doesn't accept MIME types!\r\n"
      .$bound;
     
$message .=     "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
      ."Content-Transfer-Encoding: 7bit\r\n\r\n"
      ."hey my good friend here is a picture of regal beagle\r\n"
      .$bound;
     
$fileatt = "./008.jpg";
$file = fopen($fileatt,'rb');
$data = fread($file, filesize($fileatt));
   
//$file =     file_get_contents("http://internetinfoanddesign.com/webdevelopment/brockconnect/admin/008.jpg");

//When adding the attachment, you must include a slash n escape
     
$message .=     "Content-Type: image/jpg; name=\"008.jpg\"\n"
      ."Content-Transfer-Encoding: base64\n"
      ."Content-disposition: attachment\n; file=\"008.jpg\"\n"
      ."\n\n"
      .chunk_split(base64_encode($data))
    //.chunk_split(base64_encode($file))
      .$bound_last;

if(mail($to, $subject, $message, $headers))
{
     echo 'MAIL SENT';
} else {
     echo 'MAIL FAILED';
}

?>

Thursday, March 31, 2011

The Onload Event Handler


Take a look at the on load event on the body tag of your document. It will run everything on the page, after the page has finished loading, including table elements, dividers, images, scripts, style sheets, multimedia files, etcetera.

Often it is necessary to perform some action on the page, for example, filling a form with information from a database table, displaying a list of information, or getting a value from a table for future reference.

Example:
In the on load event you can write; on load=javascript:getvalue();

You can program multiple events simply by adding semicolons.

Example:

If you need to program multiple events you can specify the functions in your onload event as follows;
onload=javascript:getvalue();javascript:displaylist()

The on load event handler becomes very useful when displaying dynamic information to different parts of your page. It is good to become familiar with this event, and use it to your advantage when designing your content.

Checking for the Existence of an Object

When using hidden fields, or generic form fields, as part of best practice it is advisable to check for the existence of the object on the page. 

Example

var Element = document.getElementById("page");

if (Element != null){ // Check object exists
                //Do something
}

Checking for the object on the page enables it to be reusable. It does not need to be on the given page your working with, to be part of your code.

Sunday, March 27, 2011

Get Parameter

This is the most important function you will every need in your online endeavors. It is called getparm. You use it by calling it into a variable.

Example:

var recordnumber=getparm('parm'); [be sure your parameter match's what was passed]


Here is the function to get the parameter;

function getparm(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
 

I found this function posted on the internet while working on a project. Similar solutions are posted all over the internet. Over the years, I have learned that if you need a solution, let your fingers do the walking. If you can't find what your looking for WALK AWAY AND THEN COME BACK, works every time.


Additional Resources:


http://www.netlobo.com/url_query_string_javascript.html







Saturday, March 26, 2011

Multiple Requests with Ajax

The Ajax programing technique can only be used to make one request at a time: however, it is possible to get more data using the java script split command.

Returning only one piece of information at a time is very limiting, and impractical. Many times you need to return more than one item to a web page. While maintaining and adding to a commercial website, I needed to find a solution.


The solution was not easy to find. It took a while to wrap my head around the concept, but I eventually figured it out.


Here it is. In your statechange function under readystate 4, after,

document.getElementById("data").innerHTML=xmlhttp.responseText;,

add the following line;


//The message is split wherever [BRK] is part of the message and stored in an array.
var result=document.getElementById("portdata").innerHTML.split("[BRK]");.


If you are using PHP for your server side script,  your last line should include the following (this is only an example, but your code should follow the same format);

$variable="George Thomson";
echo "Customer_Name".[BRK].$variable.[BRK];.

In readystate 4, evaluate the data and assign it.


Example:

if (result[0]=="Customer_Name"){
      document.getElementById("msg").innerHTML=result[1];
}


Remember that the index number for an array always starts at zero.


Additional resources;


http://www.w3schools.com/JS/js_obj_array.asp


Friday, March 25, 2011

Ajax Tutorial

Ajax stands for Asynchronous JavaScript And XML. Once you learn how to use it properly, you will never go back to posting data to a PHP or ASP script.

Since I don't have alot of time to go into detail, I will provide the basics, you need to get started. 

Step1

Create a javascript file and make a link to it in your html page after your title tag.  If you have a style sheet, put it after the style sheet reference.


Example: 


Step2

Add the following lines to the top of your javascript file;



var xmlhttp

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");


Step3

Add the following functions. These functions are mandatory. They tell the browser what to do when the data is received. 

function stateChanged()
{
   
    if (xmlhttp.readyState==1)  //Do while page is loading.
   {
          document.getElementById("Wait").style.visibility="visible";    Make span id visible.
          document.getElementById("Wait").src="ajax-loader.gif";   
          document.getElementById("lblwait").innerHTML="Processing.....";   
    }



   if (xmlhttp.readyState==4)  //Do when complete.
  {
         document.getElementById("Wait").style.visibility="hidden";  //Set wait status to hidden.
         document.getElementById("lblwait").innerHTML="";  //Clear message
         document.getElementById("portdata").innerHTML=xmlhttp.responseText; //Get data
  }

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

Step4 

Add function to execute server side script, like php or asp.


Example;


function getinfo(parm){


var recordnumber=parm; //get record number passed in
;

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="../inc/getinfo.php";

url = url+"?recordnumber=" + recordnumber;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

}


Call your function getinfo from an onclick or load event from the page.


Example:  onclick=javascript:getinfo();


Due to the nature of Ajax, only one piece of data is returned at a time, which makes it very difficult to do anything useful; however, there is a trick to getting more than one piece of data at a time. Stay tuned for additional blogs.



Here are some additional resources you may find useful;


http://www.tizag.com/ajaxTutorial
http://www.w3schools.com/ajax/default.asp













Programming for the Web