210 likes | 317 Views
NMD202 Web Scripting. Week7. What we will cover today. Student List Exercise String manipulation Exercises MVC Patterns Exercises Assignment 1. Student List Exercise. Check the implementation at:
E N D
NMD202 Web Scripting Week7
What we will cover today Student List Exercise String manipulation Exercises MVC Patterns Exercises Assignment 1
Student List Exercise Check the implementation at: http://learnline.cdu.edu.au/units/webscripting/classmaterials/source/student/listStudents.php
String Manipulation PHP has several functions to manipulate strings: http://au.php.net/manual/en/ref.strings.php
String Manipulation strlen ($string) - Length String strtolower($string) – Convert string to lower case strtoupper($string) – Convert string to upper case trim($string) - Strip whitespace from the beginning and end of a string
String Manipulation Find strings within strings: strpos ($haystack , $needle); This will give you the index of the searched substring. Use stripos for case insensitive search
String Manipulation Get a substring from string substr($string , $start, $length ) Returns the portion of $string specified by the $start and $length parameters.
String Manipulation Split a string into segments Explode($delimiter , $string); Returns an array of strings, each of which is a substring of $string formed by splitting it on boundaries formed by the string $delimiter .
String Manipulation Join a string from segments implode($glue, $pieces); Returns a string containing a string representation of all the array elements ($pieces) in the same order, with $glue between each element
String Manipulation String replace: str_replace($search ,$replace , $subject); This function returns a string or an array (depending on $subject) with the replaced values.
String Manipulation String Hashing: md5($string) crypt($string) sha($string)
String Manipulation Regular Expressions: ereg_replace — Replace regular expression ereg— Regular expression match eregi_replace— Replace regular expression case insensitive eregi— Case insensitive regular expression match
Exercises Write some code to make sure that student names have their first letter in uppercase when inserted in the database: ie: input:luissilva Stored: Luis Silva
MVC Pattern Implementation in php: Controller is called with parameters (post or get). Controller routes the request according to some parameters (task) and calls the model to collect, perform changes on the data View is built with data collected from the model and rendered to the user
MVC Pattern View: <form method=“post” action=“controller.php”> <input type=“hidden” name=“task” value=“insert”>
MVC Pattern Controller: $task = (isset($_POST[‘task’]))? $_POST[‘task’]:$_GET[‘task’]; switch ($task) { case “insert”: doInsert(); case “update”: doUpdate(); case “addNew”: renderAddForm(); default: renderList (); }
MVC Pattern Controller: function doInsert(); { insertStudent($_POST[‘name’], $_POST[‘email’]); renderList (); }
MVC Pattern Controller: function renderList(); { $students = getStudentsList(); require_once(‘studentList.php’); }
MVC Pattern view: <table> <?php While ($student = mysql_fetch_array($students)) { echo ...
Exercises Redo the student list exercise implementing it as an MVC pattern