nmd202 web scripting
DESCRIPTION
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: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/1.jpg)
NMD202 Web Scripting
Week7
![Page 2: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/2.jpg)
What we will cover today
Student List Exercise String manipulation Exercises MVC Patterns Exercises Assignment 1
![Page 3: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/3.jpg)
Student List Exercise
Check the implementation at:
http://learnline.cdu.edu.au/units/webscripting/classmaterials/source/student/listStudents.php
![Page 4: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/4.jpg)
String Manipulation
PHP has several functions to manipulate strings:
http://au.php.net/manual/en/ref.strings.php
![Page 5: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/5.jpg)
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
![Page 6: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/6.jpg)
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
![Page 7: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/7.jpg)
String Manipulation
Get a substring from string
substr ($string , $start, $length )
Returns the portion of $string specified by the $start and $length parameters.
![Page 8: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/8.jpg)
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 .
![Page 9: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/9.jpg)
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
![Page 10: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/10.jpg)
String Manipulation
String replace:
str_replace($search ,$replace , $subject);
This function returns a string or an array (depending on $subject) with the replaced values.
![Page 11: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/11.jpg)
String Manipulation
String Hashing:
md5($string)
crypt($string)
sha($string)
![Page 12: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/12.jpg)
String Manipulation
Regular Expressions:
ereg_replace — Replace regular expression
ereg — Regular expression matcheregi_replace — Replace regular expression case insensitive
eregi — Case insensitive regular expression match
![Page 13: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/13.jpg)
Exercises
Write some code to make sure that student names have their first letter in uppercase when inserted in the database:
ie: input:luis silva Stored: Luis Silva
![Page 14: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/14.jpg)
MVC Pattern
![Page 15: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/15.jpg)
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
![Page 16: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/16.jpg)
MVC Pattern
View:
<form method=“post” action=“controller.php”>
<input type=“hidden” name=“task” value=“insert”>
![Page 17: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/17.jpg)
MVC Pattern
Controller:$task = (isset($_POST[‘task’]))? $_POST[‘task’]:$_GET[‘task’];
switch ($task)
{
case “insert”:
doInsert();
case “update”:
doUpdate();
case “addNew”:
renderAddForm();
default:
renderList ();
}
![Page 18: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/18.jpg)
MVC Pattern
Controller:
function doInsert();
{
insertStudent($_POST[‘name’], $_POST[‘email’]);
renderList ();
}
![Page 19: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/19.jpg)
MVC Pattern
Controller:
function renderList ();
{
$students = getStudentsList();
require_once(‘studentList.php’);
}
![Page 20: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/20.jpg)
MVC Pattern
view:<table>
<?php
While ($student = mysql_fetch_array($students))
{
echo ...
![Page 21: NMD202 Web Scripting](https://reader035.vdocument.in/reader035/viewer/2022062519/56814e41550346895dbbaebb/html5/thumbnails/21.jpg)
Exercises
Redo the student list exercise implementing it as an MVC pattern