Download presentation
Presentation is loading. Please wait.
Published byThomas Montgomery Modified over 8 years ago
1
PHP Array and HTML Table IST 210 Organization of Data IST2101
2
Tips in Debugging What’s wrong with my code? IST2102
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 IST2103
4
4 Bug! When we don’t comment out this line! echo "Your number is $y ";
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?” Know how to debug is a very important skill ! –Totally ok to make mistakes when coding 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 IST2105
6
HTML Table Tables –Format data in a way that can be easily comprehended Elements –Table –Headings –Rows –Data 6IST210
7
An Example of HTML Table Firstname Lastname John Smith Anthony Kim 7IST210 Try it Step 1. Open a NotePad++ Step 2. Input codes Step 3. Save it to “table.html” to your webspace Step 4. Open a web browser, and visit http://my.up.ist.psu.edu/PSUID/table.html
8
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"; IST2108
9
Use Array in PHP Color list –{"red", "yellow", "blue"} $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; Key Array name Like C/C++, PHP uses 0-based numbering IST2109
10
Use Array in PHP <?php $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; echo $color[0]." "; echo $color[1]." "; echo $color[2]." "; ?> Try it IST21010
11
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]." "; } ?> Try it IST21011
12
Using Foreach Loop to Retrieve Array Values No need to know the length of the array <?php $color[0] = "red"; $color[1] = "yellow"; $color[2] = "blue"; foreach ($color as $currentcolor) { echo $currentcolor." "; } ?> Try it IST21012
13
Initialization of Array $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. " "; } ?> Try it IST21013 Start with index 0 $color[0] = “red”
14
Use PHP Array to Generate a HTML Table (Important!) <?php $color = array ("red", "yellow", "blue"); echo " "; echo " id "; echo " Color "; echo " "; for ($i=0; $i<sizeof($color); $i++) { echo " "; echo " $i "; echo " $color[$i] "; echo " "; } echo " "; ?> Try it IST21014
15
More Examples on Loop and Array For loop –http://php.net/manual/en/control-structures.for.phphttp://php.net/manual/en/control-structures.for.php Foreach loop –http://php.net/manual/en/control-structures.foreach.phphttp://php.net/manual/en/control-structures.foreach.php While loop –http://php.net/manual/en/control-structures.while.phphttp://php.net/manual/en/control-structures.while.php Array –http://php.net/manual/en/language.types.array.phphttp://php.net/manual/en/language.types.array.php IST21015
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.