Website Development & Management More PHP Odds & Ends Instructor: John Seydel, Ph.D. CIT Fall
Student Objectives Upon completion of this class meeting, you should be able to: Create an onLoad event handler for a web page Use the foreach() construct to loop through an array of data Use ereg() to validate addresses Use switch() in place of the if()/elseif() construct Discuss how data are stored and processed using arrays
Some Other Things (continued) Note some things in the FKAuto calculator page (calc.php) JavaScript Arrays foreach() construct Review and enhancement of handling Replace if() construct with switch() construct Check for a valid address Use ereg() Case-sensitive; use eregi() for case-insensitive Alternative to preg_match(), which is a Perl-compatible function Search for an “at” symbol Syntax: Add onLoad event handler to Review and revision of the script writing processprocess
What We’ve Looked at Today Using JavaScript to create an onLoad event handler How for() and foreach() constructs work The ereg() function Comparison of switch() and if()/elseif() Array concepts
Appendix
Arrays Creating a simple array $arrTerm = array(2,3,4,5,6,7); or $arrTerm = range(2,7); This is an indexed array For element $arrTerm[0] Index is 0 Value is 2 For element $arrTerm[5] Index is 5 Value is 7 To add an element: $arrTerm[6] = 8; or array_push($arrTerm,8); The number of array elements is count($arrTerm)
Associative Arrays Creating an associative array $arrCustomer[‘Person’] = “Jenny”; $arrCustomer[‘Phone’] = “ ”; or $arrCustomer = array(“Person”=>”Jenny”,”Phone”=>” ”); Array contents For element $arrCustomer[’Person’] Name is Person Value is Jenny For element $arrCustomer[’Phone’] Name is Phone Value is Some typical associative arrays Form data, e.g., $_POST[‘txtCustomer’] and other elements Database recordset rows; e.g., $strResult[‘Model’] and other
Review: Sending Makes use of the mail() function String Arguments accepted: Recipient Subject Message Header From (overrides value specified in php.ini) Reply-to Other... Consider a one-line example (poor coding)
Process Guidelines for Writing Scripts: Original Recommendation Start by initializing Assign initial values to variables Define constants if any Get data From form From other sources Prepare data for processing Write the processing logic Branching as appropriate Calculations as appropriate String manipulations as appropriate Other processing (e.g., send mail, write to database,... ) Format results for output as appropriate
Revised Script Writing Process Prior to the XML/HTML Initialization Get data From forms and other sources (but not databases) Validate as appropriate After and before Prepare data for processing Get data from database Write the processing logic Branching, calculations, and string manipulations as appropriate Other processing (e.g., send mail, write to database,... ) Other database queries Format results for output as appropriate Within and, limit PHP to scriptlets that display pre-generated HTML output blocks