100 likes | 165 Views
Enhance your PHP skills with this class! Learn to create onLoad handlers, analyze arrays, validate emails, and optimize script writing. Improve email handling, replace if statements with switches, and boost data storage efficiency.
E N D
CIT 3353 -- Fall 2006 www.clt.astate.edu/jseydel/mis3353 Website Development & Management More PHP Odds & Ends Instructor: John Seydel, Ph.D.
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 email 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 email handling • Replace if() construct with switch() construct • Check for a valid email 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: ereg(”@”,$strTo) • Add onLoad event handler to <body> • Review and revision of the script writing process
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
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’]= “867-5309”; or $arrCustomer = array(“Person”=>”Jenny”,”Phone”=>”867-5309”); • Array contents • For element $arrCustomer[’Person’] • Name is Person • Value is Jenny • For element $arrCustomer[’Phone’] • Name is Phone • Value is 867-5307 • Some typical associative arrays • Form data, e.g., $_POST[‘txtCustomer’] and other elements • Database recordset rows; e.g., $strResult[‘Model’] and other
Review: Sending eMail • 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 <html . . . > and before <head> • 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 <head> and <body>, limit PHP to scriptlets that display pre-generated HTML output blocks