Introduction to PHP – Chapter 8 Working with PHP.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Introduction to C Programming
A Level Computing#BristolMet Session Objectives#U2 S2 MUST describe the steps of an algorithm using a program flowchart SHOULD explain the data types and.
Chapter 6: JavaScript Functions 6.1 The Purpose of Functions 6.2 Defining JavaScript Functions 6.3 Using JavaScript Functions 6.4 Global Functions and.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Introduction to PHP To boldly go where JavaScript has never been…
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Density Lab Part One. Density Lab: Part One DESCRIBE Describe the color and the shape of the object using 2 words.
The Web Warrior Guide to Web Design Technologies
Lesson 4: Formatting Input Data for Arithmetic
Video, audio, embed, iframe, HTML Form
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
Computer Science 1620 Loops.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
CIS101 Introduction to Computing Week 10 Spring 2004.
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
Guide To UNIX Using Linux Third Edition
Introduction to PHP (Part-1) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Volume.
Volume.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
4.1 JavaScript Introduction
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP : Hypertext Preprocessor
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
Density. Which do you think would have the greater mass? Greater volume? Why?  1 kg of feathers  1 kg of rock  Same mass!  Larger volume  Same mass!
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Website Development with PHP and MySQL Saving Data.
Properties of Matter. Classifying Matter Matter- anything that has mass and takes up space 2 categories 1.Mixtures- contain more than one kind of matter.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Volume of a Solid BASE UNIT: cubic meter (m 3 )
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Density. Definition Density is the mass of an object in comparison to the volume it occupies.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Squared and Cubed Conversion Factors
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Programming the Web using XHTML and JavaScript
Density Practice Problems
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Volume.
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

Introduction to PHP – Chapter 8 Working with PHP

JavaScript vs. PHP PHP scripts are similar to JavaScript scripts, but be careful with syntax! PHP variables always begin with a “$” symbol. There is no equivalent to JavaScript’s “var” data declaration. PHP scripts are embedded within a tag. PHP functions are similar to JavaScript functions, but argument passing is easier. PHP functions can “return” a single value, with multiple values returned as elements in an array. PHP scripts can read data from and write data to a file on a remote server. PHP outputs can be used within the PHP application, but not “passed back” to JavaScript.

Solving the Water Vapor Problem Pass instrument serial number, time and place, and instrument output voltages. Find calibration constants for the instrument. Calculate sun’s position based on time and place (long!). Calculate total column water vapor (short). Where should the solar position calculations be done (they depend on time and place, but are independent of the instrument outputs)?

Decisions, decisions… Solar position calculations could be done in JavaScript, but we will do them in PHP to learn how to use the language. Document 8.1 gives a complete JavaScript solution, assuming that the instrument calibration constants area known. Eventually, these calculations need to be translated into PHP.

A self-contained JavaScript solution

JavaScript… function getSunpos(m,d,y,hour,minute,second,Lat,Lon) { with (Math) { // Explicit type conversions to make sure inputs are treated like numbers, not strings. m=parseInt(m,10); d=parseInt(d,10); y=parseInt(y,10); hour=parseFloat(hour); minute=parseFloat(minute); second=parseFloat(second); Lat=parseFloat(Lat); Lon=parseFloat(Lon); Note the use of parseFloat() and parseInt() to convert form field values to numbers. function get_PW(IR1,IR2,A,B,C,beta,tau,airm,p) { var x = C*airm*tau - (Math.log(IR2/IR1)-A)/B; var PW = Math.pow(x,1./beta)/airm; return Math.round(PW*1000.)/1000.; }

PHP equivalent… $m=getSunpos($_POST["mon"],$_POST["day"],$_POST["yr"],$_POST["hr"], $_POST["min"],$_POST["sec"], $_POST["lat"],$_POST["lon"]); $x = $C*$m*$tau - (log($IR2/$IR1)-$A)/$B; $PW = pow($x,1./$beta)/$m;

An HTML interface to PHP

Output from PHP

Returning multiple values Document 8.4. (circleStuff.php) <?php /* Note that this: function CIRCLESTUFF($r) { will also work because PHP function names are case-insensitive! */ function CircleStuff($r) { $area=M_PI*$r*$r; $circumference=2*M_PI*$r; /* However, this won't work: return array($AREA,$circumference); because variable names are case-sensitive. */ return array($area,$circumference); } list($area,$circumference) = CircleStuff(3); echo $area. ", ". $circumference; ?>

More about file I/O A text file contains wind speed data: , 0.4, 3.8, 4.5, 3.3, 1.9, 1.6, 3.7, 0.8, 2.3, 2.8, 2.4, 2.5, 3.2, 4.1, 3.9, 5.0, 4.4, 4.4, 5.5, 3.0, 3.7, 2.2, , 2.8, 2.3, 2.3, 1.2, 2.4, 3.1, 4.0, 3.6, 2.9, 6.0, 4.4, 0.8, 3.8, 3.5, 4.5, 2.7, 3.4, 6.6, 5.2, 1.6, 1.2, 2.3, 2.4 … , 5.9, 3.1, 3.2, 4.5, 4.4, 3.9, 4.4, 7.5, 8.4,10.2, 9.2, 8.1, 6.3, 3.1, 3.5, 2.2, 1.4, 0.4, 4.2, 5.4, 4.0, 2.9, , 2.3, 2.1, 1.5, 2.3, 4.1, 5.3, 6.0, 6.0, 9.7,11.3,12.7,13.0,13.0,11.6, 9.9, 9.6, 8.7, 5.4, 5.1, 5.3, 5.6, 4.4, 4.2 … For each day, there are 24 hourly wind speed values. Missing hours are represented by a value of -1. Read this file and count and display the month (1-12) and the number of missing values for each month. Write the results into a file and save it.

Document 8.4. (windspd.php) <?php $inFile="windspd.dat"; $outFile="windspd.out"; $in = fopen($inFile, "r") or die("Can't open file."); $out=fopen("c:/Documents and Settings/ All Users/Documents/phpout/".$outFile,"w"); while (!feof($in)) { // Read one month, year, # of days. fscanf($in,"%u %u %u",$m,$y,$nDays); if (feof($in)) exit; echo $m. ', '. $y. ', '. $nDays. ' '; $nMissing=0; for ($i=1; $i<=$nDays; $i++) { $hrly = fscanf($in, "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f"); for ($hr=0; $hr '; } echo 'Number of missing hours this month is '. $nMissing. '. '; fprintf($out,"%u, %u, %u\r\n",$m,$y,$nMissing); } fclose($in); fclose($out); ?>

Another application Write an HTML document that allows a user to select a solid object shape and enter its dimensions and the material from which it is made. The choices could be a cube, a rectangular block, a sphere, or a cube. You could choose a number of possible materials—air, gold, water, etc. Then call a PHP application that will find the mass of the object by calculating its volume based on the specified shape and looking up the density of the material in a data file.

Document 8.5a (getMass.htm) Calculate mass Enter length: Enter width: Enter height: Enter radius: cube cylinder rectangular block sphere air aluminum gold oxygen silver water <input type="submit" value="Click to get volume." <!-- <input type="button" value="click" onclick="alert(document.form1.shapes.selectedIndex); alert(shapes.options[shapes.selectedIndex].value); " --> />

PHP, the first step… This code will display something like this: Array ( [L] => 1 [W] => 1 [H] => 1 [R] => 3 [shapes] => cube [material] => oxygen ) Display the input values…

Create data files… (volume.dat) shape volume cube $L*$L*$L sphere 4/3*M_PI*$R*$R*$R cylinder M_PI*$R*$R*$L block $L*$W*$H (density.dat) material density (kg/m^3) water 1000 aluminum 2700 gold silver oxygen air 1.2

Find the material… Document 8.5b (getMass.php) ". $material. ", ". $shape. " "; $materialFile=fopen("density.dat","r"); $shapeFile=fopen("volume.dat","r"); // Read materials file. $found=false; $line=fgets($materialFile); while ((!feof($materialFile)) && (!$found)) { $values=fscanf($materialFile,"%s %f",$m,$d); if (strcasecmp($material,$m) == 0) { echo $material. ", ". $m. ", ". $d. " "; $found=true; } }

Calculate the volume… // Read volume file. $found=false; $line=fgets($shapeFile); while ((!feof($shapeFile)) && (!$found)) { $values=fscanf($shapeFile,"%s %s",$s,$v); if (strcasecmp($shape,$s) == 0) { echo $shape. ", ". $v. " "; $found=true; } } // Close both data files. fclose($materialFile); fclose($shapeFile); // Calculate mass. $vv=$v. "*$d"; echo "Result: ".eval("return round($vv,3);")." kg "; ?> This is the clever code!

Self-contained HTML/PHP applications Document 8.11 (CompoundInterest.php) Calculate Compound Interest Calculate Compound Interest " method="post"> Initial amount (no commas), $: <input type="text" name="initial" value="10000" /> Annual interest rate, %: <input type="text" name="rate" value="4" /> How many years?: <input type="text" name="years" value="20" /> <input type="submit" value="Generate compound interest table." /> "; for ($i=1; $i "; } ?>