Loops and Conditionals Generating , Reading files.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.
HTML & PHP What’s the difference?. HTML Hypertext Markup Language Key Word is “Markup” HTML is the code that makes the “screen” All web pages are made.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Creating Databases applications for the Web: week 2 Basic HTML review, forms HW: Identify unique source for asp, php, Open Source, MySql, Access.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
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,
Mini – Workshop on PHP Faculty of Engineering in Foreign Languages 1.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
>> Fundamental Concepts in PHP
Expressions and Control Flow in PHP
Databases.
EEE 161 Applied Electromagnetics
CS 371 Web Application Programming
Loops BIS1523 – Lecture 10.
Arrays: Checkboxes and Textareas
Web Database Programming Using PHP
Arrays in PHP are quite versatile
PHP Arrays Functions.
ITM 352 Cookies.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Arrays and files BIS1523 – Lecture 15.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Intro to PHP & Variables
Cookies BIS1523 – Lecture 23.
While Loops BIS1523 – Lecture 12.
* Lecture 26 Manifest Asynchrony
Server Side Programming Overview And file system basics
Introduction to Databases with MAMP/LAMP/WAMP thrown in!
CT Web Development, Colorado State University
* Lecture 12 Mapping, Map Reduction Summing integers,
HTML5 Drawing Canvas and Video
Decisions, repetition, Code Snippets, Comments, and Intellisense
Handling Files In particular, uploading files.
Tables from Arrays of Objects Exploding Strings
Content Management and WordPress
HTML Basics & Context & IDEs & Server Basics & Online Notes
Python I/O.
Functions BIS1523 – Lecture 17.
* Lecture 22 Images * Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.
Introduction to AJAX and the migration toward applications
MySQL Tables and Relations 101
* Lecture 5 PHP Basics * Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.
Derived types.
Querying Client Information Forms and Passing Data,
* jQuery * Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.
Authentication Stepped Up Persistent User Data Protected Content.
PHP.
Web DB Programming: PHP
CISC124 Labs start this week in JEFF 155.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
IntroductionToPHP Static vs. Dynamic websites
Basics (Cont...).
Web Programming Language
Server Side Programming Overview And file system basics
PHP: Hypertext Preprocessor
PHP an introduction.
Alternatives to our approach
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Chapter 13 Control Structures
Presentation transcript:

Loops and Conditionals Generating Email, Reading files. PHP Loops and Conditionals Generating Email, Reading files.

Loops & Conditionals foreach, for, if/elseif/else, while, do…while, switch Same syntax as in Java and C foreach is for looping through arrays completely Case switching with switch() can be more effective than if/elseif/else nesting CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

Don’t write this by hand. PHP Example 05 Don’t write this by hand. CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 05 CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 Remember, html is being created by PHP! CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 First things second … if “op” is not set. CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

PHP Example 06 Second things first … if “op” is set. CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

Yes, it did send email. CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

File I/O – Very Simple CSU CT 310 Web Development ©Ross Beverige & Jaime Ruiz 4/30/2019

Tables from Arrays of Objects Exploding Strings * PHP Tables from Arrays Tables from Arrays of Objects Exploding Strings * Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Example 5 combines: Objects Arrays HTML Tables 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Example 05 Use objects to manage information A state is a singular thing with 3 related data items: 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Punchline: Transform an array of objects to a table 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz An Array of States Create three states in an array Note line 41: array holds all the member data for the first state. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

More about get_object_vars With one function you can fully equate object member data with a keyword style array. Access to the keys is very handy. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Keys Are Column Headers Here again is a part of the code. Line 44: Object member data to an array Line 44: Retrieve keywords from array Line 45: Iterate through keys Line 46: One table column header per key. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Filling in the Table Enumerate the values in the table Upper and lower versions function the same. Lower avoids intermediate variable 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Style 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Now Without Loops Same goal, build a table from an array of objects. But, no loops, just array reduction Nested array reduction at that. Many ways this could be done. In this example, table header and table data rows first built as strings. Then sent to the client, i.e. a succinct echo command inside table tags. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Example 6 Code 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Example 06 on Client Think of it this way. Map reduce can be very good at constructing repeated patterns from arrays. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz Example 06 Observations Example 06 may be a bit extreme OK to ask, who finds this more readable? On the plus side Highly functional limits side-effects. Internal function easy to modify. Punchline for CT 310 Study and understand Example 06! For now avoid loops when you can. Once you master both styles, then you will be able to make informed choices. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Manipulating Files/Strings Here is a plain text file: As a text file, it has three lines What about if we want to Count words? Change newlines into HTML newlines? Group by words or lines? 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

First Part – Split on Newlines First, note one command reads file. Now, one command builds an array, one array entry per line. preg_split well worth studying! 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Second Part – Word Counts Ever explode a string? Similar to preg_split, but simpler, and better if there is only one delimiter. Pay attention to the ease of word counting. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Third Part - Substitution Explicitly convert ASCII newlines to line break tags. Note need to avoid counting <br> tags. 4/30/2019 CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz