Download presentation
1
HTML Table and PHP Array
2
Tips in Debugging What’s wrong with my code? IST210
3
Tips in Debugging Identify errors by systematically commenting out and running parts of your code Use // to comment out a line Use /* and */ to comment out a segment of codes IST210
4
Bug must be in this line! So let’s fix it!
echo "Your number is <font size=5 color=\"green\"> $y </font> <br>"; IST210
5
Learn to Debug Bad question
“What’s wrong with my code?” “I have no clue why it is not working” You need to identify the specific location and/or the specific problem in your code, and then ask questions like “Am I using the dots and quotation marks in this echo command correctly?” “Can you explain to me what’s the difference between A==B and A=B?” Knowing how to debug is a very important skill ! Totally ok to make mistakes You are encouraged to make mistakes! But you need to know where is the mistake, how to fix it, and how to avoid making the same mistake again. that’s how you gradually become an expert in programming (a.k.a, geek) IST210
6
HTML Table Tables Elements
Format data in a way that can be easily comprehended Elements Table Headings Rows Data IST210
7
An Example of HTML Table
<html> <body> <table border="1"> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <td>John</td> <td>Smith</td> <td>Anthony</td> <td>Kim</td> </table> </body> </html> IST210
8
Exercise: Table Step 1. Open NotePad++
Step 2. Write HTML code to generate the table on the right Step 3. Save it to “color.html” to your webspace Step 4. Open a web browser, and visit Observation: HTML will become very long as the table size grows.
9
PHP Array Array is a data structure that allows you to manage a list of data easily. Color list: red, yellow, blue It is not efficient to use a variable to represent each value. $color1= "red"; $color2 = "yellow"; $color3 = "blue";
10
Use Array in PHP Color list {"red", "yellow", "blue"}
$color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; Like C/C++, PHP uses 0-based numbering Array name Key
11
Use Array in PHP <?php $color[0] = "red"; $color[1] = "yellow";
$color[2] = "blue"; echo $color[0]."<br>"; echo $color[1]."<br>"; echo $color[2]."<br>"; ?>
12
Using For Loop to Retrieve Array Values
<?php $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; for ($i=0; $i<3; $i++) { echo $color[$i]."<br>"; } ?>
13
Using Foreach Loop to Retrieve Array Values
<?php $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; foreach ($color as $currentcolor) { echo $currentcolor."<br>"; } ?> No need to know the length of the array
14
Initialization of Array
Start with index 0 $color[0] = “red” $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; $color = array ("red", "yellow", "blue"); <?php $color = array ("red", "yellow", "blue"); foreach ($color as $currentcolor) { echo $currentcolor."<br>"; } ?>
15
Use PHP Array to Control HTML
$color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; foreach ($color as $currentcolor) { //print color tag echo "<font color=".$currentcolor." >"; //print color name echo $currentcolor; //print closing tag echo "</font><br>"; } ?>
16
Use PHP Array to Generate a HTML Table (Important!)
PHP (dynamic & Efficient) HTML (Static & Inefficient) <html> <body> <table border="1"> <tr> <th>id</th> <th>Color</th> </tr> <td>0</td> <td>red</td> <td>1</td> <td>yellow</td> <td>2</td> <td>green</td> </table> </body> </html> <?php $color = array ("red", "yellow", "green"); echo "<table border=1>"; echo "<tr>"; echo "<th>id</th>"; echo "<th>Color</th>"; echo "</tr>"; for ($i=0; $i<3; $i++) { echo "<td>$i</td>"; echo "<td>$color[$i]</td>"; } echo "</table>"; ?> Try it DO NOT USE IST210
17
More Examples on Loop and Array
For loop Foreach loop While loop Array IST210
18
In-Class Exercise Create a PHP file for a Lucky Draw game among at least five students Use arrays to store the 5 students’ last name, first name and major Hint: Use 3 arrays. E.g., $lastname = array ('Smith', 'Kim', 'Davis', 'Miller', 'Taylor'); Display the students’ information in a table using FOR Loop Hint: use echo to output the table to HTML Randomly select a student, for each time the PHP page is visited/refreshed, as the winner of the game, and display his/her name Hint: you can use “$v = rand(0,4)” to generate a random number in [0,4] and store it in $v Visit your PHP file:
19
In-Class Exercise (cont.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.