nmd202 web scripting

21
NMD202 Web Scripting Week7

Upload: ryo

Post on 19-Jan-2016

23 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: NMD202 Web Scripting

NMD202 Web Scripting

Week7

Page 2: NMD202 Web Scripting

What we will cover today

Student List Exercise String manipulation Exercises MVC Patterns Exercises Assignment 1

Page 3: NMD202 Web Scripting

Student List Exercise

Check the implementation at:

http://learnline.cdu.edu.au/units/webscripting/classmaterials/source/student/listStudents.php

Page 4: NMD202 Web Scripting

String Manipulation

PHP has several functions to manipulate strings:

http://au.php.net/manual/en/ref.strings.php

Page 5: NMD202 Web Scripting

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

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

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

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

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

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

String Manipulation

String Hashing:

md5($string)

crypt($string)

sha($string)

Page 12: NMD202 Web Scripting

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

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

MVC Pattern

Page 15: NMD202 Web Scripting

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

MVC Pattern

View:

<form method=“post” action=“controller.php”>

<input type=“hidden” name=“task” value=“insert”>

Page 17: NMD202 Web Scripting

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

MVC Pattern

Controller:

function doInsert();

{

insertStudent($_POST[‘name’], $_POST[‘email’]);

renderList ();

}

Page 19: NMD202 Web Scripting

MVC Pattern

Controller:

function renderList ();

{

$students = getStudentsList();

require_once(‘studentList.php’);

}

Page 20: NMD202 Web Scripting

MVC Pattern

view:<table>

<?php

While ($student = mysql_fetch_array($students))

{

echo ...

Page 21: NMD202 Web Scripting

Exercises

Redo the student list exercise implementing it as an MVC pattern