Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review 3 csc242 – web programming.

Similar presentations


Presentation on theme: "Review 3 csc242 – web programming."— Presentation transcript:

1 Review 3 csc242 – web programming

2 PHP – General Syntax Double vs. single quotes
$var= “hi”; echo “$var “ . “John!”; -> hi John! echo ‘$var ‘ . “John!”; -> $var John! $_POST[‘foo’]= “bar”; print “$_POST[‘bar’]”; -> syntax error!!! print “{$_POST[‘bar’]}”; -> bar Double vs. single quotes Variables inside double quotes will be evaluated Variables inside single quotes will not be evaluated Dot (.) is the string concatenation operator Must enclose array variables in braces {} for the to print properly!

3 PHP - arrays Standard arrays Associative arrays (arbitrary indicies)
Useful functions count($arr) Number of elements is_array($arr) Is it an array? isset($arr[“third”]) Is the value set? unset($arr[“third”]) Remove a value Standard arrays $list = array(‘john’, ‘mary’, ‘fred’); Automatically indexed starting at zero ($list[0] is “john”) Associative arrays (arbitrary indicies) $arr= array(“first” => “one”, “second” => “two”); arr[“third”]= “three”; Looping for($i=0; $i< count($arr); $i++) { … } foreach ($arr as $key => $value) { … } sets $key and $value on each iteration foreach ($arr as $value) { … } just does the values

4 PHP - Functions Declare with function keyword
No need to specify return type! Arguments passed by value Variables can be made global with the global keyword Default arguments are allowed return statement Can return arrays… function addn($x, $n = 3) { global $z; $y= $x + $n; $z = 2*$y; return $y; }

5 Cookies Allows information to be stored and accessed across pages on a web site. To create: setcookie(‘name’,’value’); To access: $var= $_COOKIE[‘name’]; To remove: setcookie(‘name’); Storage is on the client side MUST be set up in PHP before any HTML is sent to the client Can be blocked by the client (disabled in the browser)

6 Sessions Rely on cookies to operate Advantages over cookies
One cookie is created for the session ID Therefore MUST be set up in PHP before any HTML is sent to the client Advantages over cookies Can be made to work without cookies ini_set("session.use_trans_sid", 1); uses “get” to pass session ID between web pages Can store more information than cookies

7 Sessions (cont.) To begin: session_start(); To end: session_destroy();
Session ID: session_id(); (or $_COOKIE['PHPSESSID']}) Store information using $_SESSION array: $_SESSION[‘foo’] = ‘bar’; It can be accessed on any page that has started a session

8 Relational databases (MySQL)
Information is stored in tables Table column lists the type of information stored Table rows contain the actual data entries Tables are often “linked” (an ID in one table is referenced in another) Like the Products and Categories tables in the grocery store project SQL commands allow access to table information SELECT column1, … columnN FROM table_name WHERE columnX = ‘value’ INSERT into table_name (col1, col2, …) VALUES (‘val1’, ‘val2’, …) - Many others…

9 More info in the Basic SQL commands Powerpoint
mysqli functions $connection= mysqli_connect(host, user, password, database) mysqli_close($connection); $error= mysqli_error($connection); $result= mysqli_query($connection, SQLcommand); $number= mysqli_num_rows($result); $value= get_mysqli_result($result, $number, $field); This is a command provided for CSC242 that replaces the mysql_result() command More info in the Basic SQL commands Powerpoint


Download ppt "Review 3 csc242 – web programming."

Similar presentations


Ads by Google