1 / 19

Programming with php

Programming with php. By: Seth Larson. A little bit about PHP. PHP stands for PHP: Hypertext Preprocessor PHP is a widely-used general-purpose server-side scripting language that is suited for Web development and can be embedded into HTML. PHP scripts are executed on the server

kasen
Download Presentation

Programming with php

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with php By: Seth Larson

  2. A little bit about PHP • PHP stands for PHP: • Hypertext Preprocessor • PHP is a widely-used general-purpose server-side scripting language that is suited for Web development and can be embedded into HTML. • PHP scripts are executed on the server • PHP supports many different databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) • PHP is an open source software (OSS), so its free • PHP files have a file extension of ".php", ".php3", or ".phtml" • PHP files are returned to the browser as plain HTML

  3. Server Side • <html> • <body> • <?php echo '<p>PHP rules!</p>';?> • </body> • </html>

  4. Client Side • <html> • <body> • <p>PHP rules!</p> • </body> • </html>

  5. What do you need installed on your computer to use php? • A server that supports php. Some servers that support php are Apache(open source), IIS (Microsoft), and Oracle. • The latest version of PHP found at • http://www.php.net • You can test if php is installed and working with the following code.

  6. Show php information • <html> • <body> • <?php • phpinfo(); • ?> • </body> • </html>

  7. Some PHP syntax • A php statement always begins with <?php and ends with ?>. • php statements can be placed anywhere in the document. • Statements end with semicolon.

  8. PHP variables • Variables start with $, example $var • Variables have the same syntax rules as other high level languages. • To concatenate two or more variables together, use the dot (.) operator: • Using the “\” right next to a variable name tells the server to print what directly follows and not to treat it as a label.

  9. Sample Variables • <html> • <body> • <?php • $var_A = " Print \$var_A "; • $var_B = "To print a variable name instead • of what the variable is holding use the \ • character."; • echo $var_B; • echo"<P>"; • echo $var_A; • ?> • </body> • </html>

  10. PHP operators • Arithmetic Operators • +, -, /, * • Assignment Operators • =, +=, -=, *=, /=, %= • Comparison Operators • ==, !=, >, <, >=, <= • Logical Operators • &&, ||, ! • If – else statement • if (condition) • code to be executed • else • code to be executed • While • while { code to be executed; } • Do-while • do { code to be executed; } while (condition); • For • Foreach

  11. PHP forms • Form is created with html. • Any html element you put in a form is available after a submit because php automatically makes element variables. • Examples: $_POST["first"],$_POST["last" • Notice that if you use post in the form header the variables are accessible with $_POST, if you use get the variables are accessible with $_GET.

  12. Form • <html> • <body> • <form action="php_forms_next.php" method="post"> • <pre>Your Id: <input type = "text" name = "id"/> • </ br> • Your First name: <input type="text" name="first" /> • </ br> • Your Last name: <input type="text" name="last" /> • <?php • echo "male: <input type = checkbox name = sex value • male> • Female: <input type = checkbox name= sex • value=female> • ?> • <input type="submit" /></pre> • </form> • </body> • </html>

  13. Print form variables • <html> • <body><pre>You said your Id number was: • <?php echo $_POST["id"]; ?></pre> • <pre>You said your First name was <?php echo • $_POST["first"]; ?></pre> • <pre>You said you Last name was <?php echo • $_POST["last"]; ?></ pre> • <pre>You also said you are <?php echo • $_POST["sex"];?></pre> • <pre>Your favorite color is <?php echo • $_POST["color"];?></pre> • </body> • </html>

  14. Text File Read/Write • <html> • <body> • <?php • if (!($file=fopen("php_file.txt","r"))) • exit("Can’t open file."); • while (!feof($file)) • { $char=fgetc($file); • echo $char; • } • //$write = fputs($fp, “write hi”); • fclose($file); • ?> • </body> • </html>

  15. Using a database • The mysql_connect() function opens a link to a MySQL server on the specified host along with a username (root) and a password. • mysql_select_db() is used to tell my MySQL to use the specified database. • mysql_query()sends a line of SQL to the MySQL database identified in the connection for processing. The returned results are stored in $result. • mysql_result() is used to display the values of fields from our query. • %s indicates that the variable in the second half of the expression (e.g., mysql_result($result,0,"position")) should be treated as a string and printed.

  16. Reading one field from each record in the database • <html> • <body> • <?php • $db = mysql_connect("localhost", "root","seth"); • mysql_select_db("mydb",$db); • $result = mysql_query("SELECT * FROM employee",$db); • $count=mysql_numrows($result); • $i=0; • while ($i < $count) • { • printf("First Name: %s", • mysql_result($result,$i,"first")); • echo "<p>"; • $i++; • } • mysql_close(); • ?> • </body> • </html>

  17. Reading records one at a time • <html> • <body> • <?php • $db = mysql_connect("localhost", "root", "seth"); • mysql_select_db("mydb",$db); • $result = mysql_query("SELECT * FROM employee",$db); • echo "<table border=1>\n"; • echo "<tr><td>id</td><td>First Name and Last Name</td></tr>\n"; • while ($myrow = mysql_fetch_row($result)) • { • printf("<tr><td>%s </td><td>%s %s</td></tr>\n", • $myrow[0], $myrow[1], $myrow[2]); • } • echo "</table>\n"; • ?> • </body> • </html>

  18. Inserting into a Database: form part • <html> • <body> • <form action="php_insert_form_insert.php" method="post"> • <pre> • Your Id: <input type = "text" name = "id"/> • Your First name: <input type="text" name="first" /> • Your Last name: <input type="text" name="last" /> • </br> • <input type="submit" /></pre> • </form> • </body> • </html>

  19. Insert into a MySQL database • <html><body> • <?php • $id = $_POST["id"]; • $first = $_POST["first"]; • $last = $_POST["last"]; • $db = mysql_connect("localhost", "root","seth"); • mysql_select_db("mydb",$db); • $result = mysql_query("SELECT * FROM employee",$db); • echo "<table border=1>\n"; • echo "<tr><td>id</td><td>First Name and Last Name</td></tr>\n"; • while ($myrow = mysql_fetch_row($result)) • { • printf("<tr><td>%s </td><td>%s %s</td></tr>\n", • $myrow[0], $myrow[1], $myrow[2]); • } • echo "</table>\n"; • $query = "INSERT INTO employee VALUES ('$id','$first','$last')"; • mysql_query($query); • ?> • </body> • </html>

More Related