Click to See Complete Forum and Search --> : Need Help Deciphering Code


Elle0000
08-14-2006, 06:32 PM
I was given the code for an example of a 10x10 table.
I have to create one of my own but I don't understand what each line's specific function is. I can't create a table if I don't understand what the code means or how it works.
I understand the HTML but not most of the PHP code. Lines 8-10 assign values. Beyond that, I just don't get it. I don't understand how to tell that there is a loop or even nested loops.

1: <html>
2: <head>
3: <title>Exercise 1</title>
4: </head>
5: <body>
6: <H1 align="center">Exercise 1</H1>
7: <?php
8: $username = "Jane";
9: $x_val = 5;
10: $y_val = 7;
11: echo "<p>Welcome, <b>$username</b>!</p>";
12: echo "<table border=\"1\" cellpadding=\"4\" cellspacing=\"4\">";
13: for ($y=1; $y<=$y_val; $y++) {
14: echo "<tr>";
15: for ($x=1; $x<=$x_val; $x++) {
16: echo "<td align=center>";
17: echo (($x - $y) * ($x + $y));
18: echo "</td>";
19: }
20: echo "</tr>";
21: }
22: ?>
23: </body>
24: </html>

sitehatchery
08-14-2006, 06:46 PM
Well, you're building rows and data cells and filling them with data.

Elle0000
08-14-2006, 06:50 PM
I'm completing new to this concept.
For example, I have no idea what the { bracket by itself means.
I barely understand that the word echo will print something.
What lines make the code loop?
What I guess that I need is a 1 line summary for each line of what the PHP code does.
I understand the HTML.

sitehatchery
08-14-2006, 07:09 PM
11. $username is a variable. It can hold anything. But, in this case it holds "Jane", so "Welcome $username" Prints "Welcome Jane" to your browser and it displays on your monitor.
12. Prints table tag to the browser.
13. It's a for loop. In this case, variable $y is initialized with 1. It will continue to increase by one ($y++) until it is less than or equal to (<=) 7 ($y_val). In other words, it will make 7 rows.
14. Prints the row to the browser.
15. Now each row has 5 ($x_val) rows. So, before you complete each row, you loop 5 times...
16. creating the tabular data tag...
17. inserting the data...
18. closing the tabular data tag.
19. The end of the loop which creates the tabular data cells.
20. Close the tabular row tag.
21. The outer loop which creates the rows closes.
22. PHP code ends.