© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
PHP (2) – Functions, Arrays, Databases, and sessions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Guide To UNIX Using Linux Third Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
PHP : Hypertext Preprocessor
INTRODUCTION TO DREAMWEAVER 8. What we already know…  Design basics  Contrast  Repetition  Alignment  Repetition  HTML.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Create an online booking system (login/registration)
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
INTERNET APPLICATION DEVELOPMENT For More visit:
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
PHP meets MySQL.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
PHP and JavaScript Nov. 26, 2013 Kyung Eun Park Computer and Creativity (COSC109) Towson University.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
PHP Form Processing * referenced from
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Web Database Programming Using PHP
Web Database Programming Using PHP
PHP Introduction.
PHP.
Web DB Programming: PHP
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
Presentation transcript:

© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri

© Anselm Spoerri Lecture 9 – Overview Dynamic Web Programming Concepts PHP –Introduction | Basic Operations & Coding MySQL –Introduction | Simple Queries MySQL & PHP –Set Up PHP Testing Server in Dreamweaver –login.php –Connect to whereru Database –Run Query & Process Results Source: "Learning PHP, MySQL, and JavaScript" by Robin Nixon.

© Anselm Spoerri Web Pages Created So Far Static HTML Pages –Same each time Want Interactive or Dynamic Web Pages –Depending on browser, time of day or user behavior or … different appearance –Processing user inputs data via HTML forms –Need some sort of Web programming (JavaScript, PHP)

© Anselm Spoerri Basic Client/Server Request/Response Sequence

© Anselm Spoerri Dynamic Client/Server Request/Response Sequence $query $result

© Anselm Spoerri Dynamic Web 1.JavaScript –Client-side scripting access to elements of HTML document –Perform data handling and web server requests in background 2.PHP –Server-side scripting –Open source, simplicity and built-in links to MySQL database 3.MySQL –Open-source Relational Databases that supports structured queries and is free to use and install on web servers 4.Apache Server –Open source server software that serves up HTML, media files etc PHP handles main work on web server, MySQL manages data, and JavaScript looks after web page presentation. JavaScript can also talk with your PHP code on the web server whenever it needs to update something (either on the server or on the web page).

© Anselm Spoerri Key Programming Concepts data types numbers, text, boolean (true, false) variable = container of data array = collection of data organized in a sequence statement = combination of variables, operators and data  group of statements  function = collection of statements to perform specific task perform test: true or false if (test) then for loop: where do we start? when are done? how to continue?

© Anselm Spoerri Programming Concepts – Overview Data Types : such as numbers, text, boolean (true, false) ‒ Integer | Boolean | Character | String | Floating-Point ‒ Array: [5, 4, 7] (use FOR loop to examine each array item) Variable : is way to symbolically to refer to data content ‒ scores = [5, 4, 7]; Operators : can be used to perform built-in operations ‒ 5 +5; (where + is addition operator) Statement : combination of variables, operators and data ‒ total = 12 * monthly_average;

© Anselm Spoerri Programming Concepts – Overview Function : collection of statements to perform specific task function add_scores (score1, score2) { return (score1 + score2); } total = add_scores (3, 4); Conditional Programming used so that operations are performed only if certain condition is met: ‒ IF test (only do something if certain condition is true) ‒ FOR loop (keep doing something only if certain condition is true) ‒ WHILE loop (keep doing something until certain condition not true anymore) Debugging code is essential part of programming... chasing typos, missing parentheses :)

© Anselm Spoerri PHP – Overview Open-source : documentation Server-side scripting –Insert content of one file (or the output of a system call) into another file dynamically –Create images and other files for you, either on the fly or in advance to serve up later. –PHP program responsible for passing back clean file suitable for display in web browser. Simplicity & Flexibility Built-in links to MySQL database.php file extension

© Anselm Spoerri PHP – Overview (cont.) $ count = 10 ; Must Place $ in front of all Variables Common Error = Forget Semicolon $username = "Fred Smith"; // quotation marks for string echo $username; // echo makes variable visible in Browser

© Anselm Spoerri PHP – Code Construction XHTML tags included inside of PHP code (thus have DOCTYPE declaration and basic XHTML structure) Some programmers open tag at start of document and close it at the end, outputting any HTML directly from PHP commands. Others choose to insert only smallest possible fragments of PHP within these tags wherever dynamic scripting is required, leaving the rest of the document in standard HTML. Hello World. Today is. Latter style may produce faster code, while the former has less complexity having to drop in and out of PHP many times in a single document.

© Anselm Spoerri PHP - Variable Naming Rules Starts with $ Variable Naming Rules When creating PHP variables, you must follow these four rules: ‒ Must start with a letter of alphabet or _ (underscore) ‒ Can contain only the characters: a-z, A-Z, 0-9, and _ ‒ May not contain spaces. (e.g., $user_name). ‒ Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score. In PHP, & symbol tells parser to pass reference to variable's value, not the value itself. Be careful when passing values by reference. If you need to keep the original values, make copies of your variables and then pass the copies by reference.

© Anselm Spoerri PHP - Strings PHP supports two types of strings: "hello" or 'hello' –Literal string (preserve exact contents), use apostrophe: $info = 'Preface variables with a $ like this: $variable'; –Include value of variable (evaluate string): use quotation marks $count = 44; echo "There have been $count presidents of the US"; Escape Character : \ –$text = 'My friend\'s car is a Chevy'; –$text = "My mother always said \"Eat your greens\"."; –Important to use when HTML code and variables used inside echo statement

© Anselm Spoerri PHP – Array Operations $team = array('Bill', 'Joe', 'Mike', 'Chris'); // apostrophes echo $team[3]; // Displays the name Chris  array indexes start at zero $twoDim = array(array('a', '', 'c'), array('', 'b', ''),); echo $twoDim[1][1]; count($a); // number of array items Array union: $a + $b Associative Array –$a['firstname'] = "John";

© Anselm Spoerri PHP - Operators Operators ++$j or --$j // Increment or Decrement "FirstName". " ". "LastName" // String concatenation Boolean AND: && AND OR: || OR $firstname && $lastname $firstname || $lastname Equality Operator is == Not Equal is != Identity Operator is === (prevents type conversion) Operators precedence rules  use parentheses to control what gets computed first and in which order

© Anselm Spoerri PHP – Constants, Global and Function Define Constant define("ROOT_LOCATION", "/usr/local/www/"); –Can not be prefaced with $ sign and need to use define function –Use UPPERCASE global $username; Define Function function function_name ([parameter [,...]]) { // Statements } function squared ($count) { return $count * $count;} Scope of Variable –Local variables are accessible just from part of code where defined: Defined inside of function, only accessible inside of function. Defined outside of function, only accessible outside of function. –Global variables are accessible from all parts of your code. Forgetting Scope of Variable is Common Error.

© Anselm Spoerri PHP – IF test if (test) {do1;} else {do2;} test ? doifTRUE : doifFALSE; if (test1) {do1;} elseif (test2) {do2;} else {do3;}

© Anselm Spoerri PHP – FOR Loop AND FOREACH Loop for loop for (init variable ; test variable ; increment variable) { do something; } for ($count = 1 ; $count "; } foreach loop used for arrays $paper = array("Copier", "Inkjet", "Laser", "Photo"); $j = 0; foreach ($paper as $item) { echo "$j: $item "; ++$j; }

© Anselm Spoerri PHP – Include Code from Other Files include_once "filename"; –Tell PHP to fetch a particular file and load all its contents –To avoid error of trying to define same constant or function multiple times require_once "filename"; –To make sure that file is found

© Anselm Spoerri PHP – Questions What tag is used to cause PHP to start interpreting program code? What are the two types of comment tags? Which character must be placed at the end of every PHP statement? Which symbol is used to preface all PHP variables? What can a variable store? What is the difference between $variable = 1 and $variable == 1? Why do you suppose that an underscore is allowed in variable names ($current_user) whereas hyphens are not ($current-user) ? Are variable names case-sensitive? Can you use spaces in variable names? How do you convert one variable type to another (say, a string to a number)? Are the operators && and and interchangeable? Can you redefine a constant? How do you escape a quotation mark? How can you make a variable accessible to all parts of a PHP program? What is the result of combining a string with a number?

© Anselm Spoerri SQL – Querying Relational Database SQL = Structured Query Language Database computer language designed for Managing & Accessing Data in Relational Database Queries –Ask question about data –Receive answer back by returning subset of table data. –Use JOIN, SELECT, WHERE –Need semicolon ; at the end of query –SQL commands and keywords are case-insensitive

© Anselm Spoerri MySQL – Overview Open-source Fast & Powerful Yet Easy-to-use Database –One or more tables that contain your data Database –The overall container for a collection of MySQL data. Table –A subcontainer within a database that stores the actual data. Row –A single record within a table, which may contain several fields. Column –The name of a field within a row. SQL commands and keywords are case-insensitive

© Anselm Spoerri MySQL – Overview (cont.) Field –The name of each field or column within a table. Type –The type of data being stored in the field. Null –Whether a field is allowed to contain a value of NULL. Key –MySQL supports keys or indexes, which are quick ways to look up and search for data. The Key heading shows what type of key (if any) has been applied. Default –The default value that will be assigned to the field if no value is specified when a new row is created.

© Anselm Spoerri Set Up PHP Testing Server If not already done to access DW via SoftwareAnywhere: login to to provision credentialshttp://account.comminfo.rutgers.edu Site > New Site Select “Site” Site > Site Name = “PHP Testing Server” Site > Local Site Folder = “PHPfiles” in “My Documents” Select “Servers" Category in Site Dialog Click on + (Add new server) Specify Server Name Select “SFTP" in pull-down “Connect using” menu SFTP Host = “cactuar.scilsnet.rutgers.edu” Root Directory = “/www/whereru.scils.rutgers.edu/htdocs/iti320semesteryear/yourteam” where for example semesteryear = su13 or fa13 Login = “yourNetIDusername” Password = “yourNetIDpassword” Web URL =“ Test Advanced Tab: Select “PHP MySQL” in pull-down Server Model menu Save Make sure “Remote” and “Testing” checkboxes are selected

© Anselm Spoerri Testing PHP Page Preview in Browser Update Copy on Testing Server? Select “Yes” Put Dependent Files ? Select “No“ Permissions Folder / Files Permission Automatically set to 770 … you don’t have do anything Folder for Each Team Member Need to update: Root Directory = “…/iti320semesteryear/yourteam/ username ” where for example semesteryear = su13 or fa13 for example: “…/iti320fa13/yourteam/ username ” Web URL =“ username ” for example: “ username ”

© Anselm Spoerri Site Definition for PHP Testing Server Local Folder = YOUR files and Upload Your Files to Your Server Folder Local Folder = contains YOUR PHP files Root Directory = “ /iti320fa13/yourteam/ username ” Web URL = username Local Folder = Contains Folder for YOUR files and Upload Your Files to Your Server Folder Local Folder = contains a folder that contains YOUR PHP files and this folder needs to have same name as YOUR folder on the server. Root Directory = “ /iti320fa13/yourteam/” Web URL =“  Number of Local Folder Hierarchy Levels needs to match Number of Server Folder Hierarchy Levels If the local folder of your Site Definition contains just your PHP files, then your Site Definition needs to include the name of YOUR folder on the server so that your PHP files get automatically uploaded to YOUR folder on the server. If the local folder of your Site Definition contains a folder that contains your PHP files, then your Site Definition does not need to include the name of YOUR folder on the server so that your PHP files get automatically uploaded to YOUR folder on the server.

© Anselm Spoerri MySQL & PHP – Process Steps The process of using MySQL with PHP: 1.Connect to MySQL. 2.Select the database to use. 3.Build a query string. 4.Perform the query. 5.Retrieve the results and output it to a web page. 6.Repeat Steps 3 to 5 until all desired data retrieved. 7.Disconnect from MySQL (usually done automatically)

© Anselm Spoerri MySQL & PHP – login.php for your database Create login.php filemake sure to place in personal folder <?php // login.php make sure to place in personal folder $db_hostname = 'localhost'; $db_database = 'whereru'; $db_username = 'yourMySQLusername'; $db_password = 'yourMySQLpassword'; ?> Upload to Server, View Source in Browser Now that you have login.php file saved, you can include it in any PHP files that will need to access the database by using the require_once statement.

© Anselm Spoerri MySQL & PHP – Connect to Database – Step 1 <?php // connectDatabase.php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if ( ! $db_server) die ("Unable to connect to MySQL: ". mysql_error()); mysql_select_db($db_database) or die("Unable to select database: ". mysql_error()); ?>

© Anselm Spoerri MySQL & PHP – Query Database – Step 2 Add to connectDatabase.php $db_table = 'whereru_gigapan'; // description in Sakai Resources $query = " SELECT * FROM $db_table"; $result = mysql_query ($query); if (!$result) die ("Database access failed: ". mysql_error());

© Anselm Spoerri MySQL & PHP – Display Query Result – Step 3 Add to connectDatabase.php $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ echo 'ID: '. mysql_result ( $result,$j,'id' ). ' '; echo 'Title: '. mysql_result($result,$j,'title'). ' '; echo 'Campus: '. mysql_result($result,$j,'campus'). ' '; } This code is inefficient and slow, because $rows * 3 calls are made to the function mysql_result in order to retrieve all the data.

© Anselm Spoerri MySQL & PHP – Display Query Result (improved) – Step 4 $id = 0; // assign index needed to access field in $row array … see data info in Sakai $title = 2; $campus = 3; for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row ($result); // returns array echo 'ID: '. $row[ $id ]. ' '; echo 'Title: '. $row[ $title ]. ' '; echo 'Campus: '. $row[ $campus ]. ' '. ' '; }

© Anselm Spoerri MySQL & PHP – Refined Query $db_table = 'whereru_gigapan'; $query = "SELECT * FROM $db_table WHERE campus='Newark'"; $result = mysql_query($query); if (!$result) die ("Database access failed: ". mysql_error()); $rows = mysql_num_rows($result); $id = 0; // assign index needed to access field in $row array $title = 2; $campus = 3; for ($j = 0 ; $j '; echo 'Title: '. $row[$title]. ' '; echo 'Campus: '. $row[$campus]. ' '. ' '; }