Presentation is loading. Please wait.

Presentation is loading. Please wait.

(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 1 Basic PHP Class 2.

Similar presentations


Presentation on theme: "(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 1 Basic PHP Class 2."— Presentation transcript:

1 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 1 Basic PHP Class 2

2 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 2 Declaring PHP The three different forms are as follows: PHP Code In Here

3 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 3 Your First Script Testing Your Script ?

4 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 4 Displaying Information & Variables Printing Text : Variables: $welcome_text = "Hello and welcome to my website."; $user_id = 987 Example:

5 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 5 Formatting Your Text PHP: print(" Hello and welcome to my website. ");

6 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 6 IF Statements IF (something == something else) ( THEN Statement } else { ELSE Statement } ----------------------- if ($username == "webmaster") { echo "Please enter your password below"; } _______________________ if ($username == "webmaster") { echo "Please enter your password below"; } else { echo "We are sorry but you are not a recognised user"; }

7 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 7 Other comparisons if ($enteredpass == $password) ---------------------------------------------------------- if ($age $finished) ----------------------------------------------------------- You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use: if ($name == "" || $email == "" || $password == "") { echo "Please fill in all the fields"; }

8 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 8 The WHILE Loop $times = 5; $x = 0; while ($x < $times) { echo "Hello World"; ++$x; /* It means $x=$x+1; */ } -------------------------- Using $x : $number = 1000; $current = 0; while ($current "; }

9 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 9 Arrays > Setting Up An Array Setting up an array is slightly different to setting up a normal variable. In this example I will set up an array with 5 names in it: $names[0] = 'John'; $names[1] = 'Paul'; $names[2] = 'Steven'; $names[3] = 'George'; $names[4] = 'David'; OR  $names=array(“John”,”Paul”,”Steven”); > Reading From An Array echo "The third name is $names[2]";

10 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 10 Using Arrays And Loops if I wanted to print out the following list of names: Name 1 is John Name 2 is Paul Name 3 is Steven Name 4 is George Name 5 is David I could use the following code: $number = 5; $x = 0; while ($x "; ++$x }

11 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 11 PHP With Forms Setting Up Your Form   Please write your message here.  All the elements for your form must be enclosed in the tags. They are used as follows: Form elements and formatting etc.

12 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 12 Getting The Form Information There are basically two different methods of getting the data into PHP, which depend on how they were submitted. There are two submission methods, GET and POST, which can both be used by forms. The difference between the two is that using GET, the variables and data will be shown in the page address, but using POST it is invisible. The benefit of GET, though is that you can submit information to the script without a form, by simply editing the URL: yourpage.php?user=david could show David's page and: yourpage.php?user=tom could show Tom's page, using the same script.

13 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 13 Contd.. It is also possible to pass more than one piece of information to the script using this system by separating them with the & symbol: yourpage.php?user=david&referrer=gowansnet&area=6 To get a variable which has been sent to a script using the POST method you use the following code: $variablename=$_POST['variable']; Similarly, if you are using the GET method you should use the form: $variablename=$_GET['variable'];

14 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 14 Example (Creating The Form To Mail Script ) create this form for your HTML page: Your Name: E-mail: Comments

15 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 15 Contd.. <? $name=$_POST['name']; $email=$_POST['email']; $comments=$_POST['comments']; $to="php@gowansnet.com"; $message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email"; if(mail($to,"Comments From Your Site",$message,"From: $email\n")) { echo "Thanks for your comments."; } else { echo "There was a problem sending the mail. Please check that you filled in the form correctly."; } ?>

16 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 16 E-mail With PHP (Optional to basic level) The Mail Command mail($to,$subject,$body,$headers);  Sending An E-mail $to = "php@gowansnet.com"; $subject = "PHP Is Great"; $body = "PHP is one of the best scripting languages around"; $headers = "From: webmaster@gowansnet.com\n"; mail($to,$subject,$body,$headers); echo "Mail sent to $to";

17 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 17 Contd..  Mail Without Variables mail("php@gowansnet.com","PHP Is Great","PHP is one of the best scripting languages around","From: webmaster@gowansnet.com\n");  Error Control if(mail($to,$subject,$body,$headers)) { echo "An e-mail was sent to $to with the subject: $subject"; } else { echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid"; }

18 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 18 File handling Opening a File : $filename = 'c:\file.txt'; $fp = fopen($filename, "r"); Reading Info from a File: $contents = fread($fp, filesize($filename)); fclose($fp); /* it reads the entire file in */

19 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 19 Contd.. Let us assume C:file.txt contains: line1 line2 line3 line4 Line5 So now $contents would contain: $contents = "line1\n line2\n line3\n line4\n line5";

20 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 20 Contd.. Writing to a File: $filename = 'c:\file.txt'; $fp = fopen($filename, "a"); $string = "\nline5"; $write = fputs($fp, $string); fclose($fp); Firstly we open the file. Notice the "a" parameter. That means "open the file for writing only, and place the file pointer at the end of the file". So now PHP opens myfile.txt for writing and positions the file pointer at the end of the line. We then use fputs() to write to the file. We define our string in $string which i put "\nline6. The \n bit means start a new line, and the print out "line 6". We then close the file.

21 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 21 Modes 'r' - Open for reading only; place the file pointer at the beginning of the file. 'r+' - Open for reading and writing; place the file pointer at the beginning of the file. 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

22 (c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 22 Questions?


Download ppt "(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL 1 Basic PHP Class 2."

Similar presentations


Ads by Google