Difference between revisions of "CSC220 Lab 3 2010"

From dftwiki3
Jump to: navigation, search
(Preamble)
(Fifth Problem on Arrays)
 
(6 intermediate revisions by the same user not shown)
Line 16: Line 16:
  
  
=Preamble=
+
=Important Debugging Information=
  
 
* By default, your browser will try to be clever and not reload a page twice if you ask it to reload it.  Instead it keeps a copy of the page it just fetched in a ''cache'', and reloads it from cache instead of actually getting it back from the server.   
 
* By default, your browser will try to be clever and not reload a page twice if you ask it to reload it.  Instead it keeps a copy of the page it just fetched in a ''cache'', and reloads it from cache instead of actually getting it back from the server.   
Line 31: Line 31:
 
<body>
 
<body>
 
...
 
...
 +
 +
</body>
 +
</html>
 
</pre></code>
 
</pre></code>
  
Line 81: Line 84:
 
* Same as Problem 3, but this time the 10 lines are output in an html table.
 
* Same as Problem 3, but this time the 10 lines are output in an html table.
  
* The code for the table should look something like this:
+
* The html code for the table should look something like this:
  
 
<code><pre>
 
<code><pre>
 
       <table>
 
       <table>
       <tr><td>1 odd</td></tr>
+
       <tr><td>1 </td><td>odd</td></tr>
       <tr><td>2 odd</td></tr>
+
       <tr><td>2 </td><td>odd</td></tr>
       <tr><td>3 odd</td></tr>
+
       <tr><td>3 </td><td>odd</td></tr>
       <tr><td>4 odd</td></tr>
+
       <tr><td>4 </td><td>odd</td></tr>
       <tr><td>5 odd</td></tr>
+
       <tr><td>5 </td><td>odd</td></tr>
       <tr><td>6 odd</td></tr>
+
       <tr><td>6 </td><td>odd</td></tr>
       <tr><td>7 odd</td></tr>
+
       <tr><td>7 </td><td>odd</td></tr>
       <tr><td>8 odd</td></tr>
+
       <tr><td>8 </td><td>odd</td></tr>
       <tr><td>9 odd</td></tr>
+
       <tr><td>9 </td><td>odd</td></tr>
       <tr><td>10 odd</td></tr>
+
       <tr><td>10 </td><td>odd</td></tr>
 
       </table>
 
       </table>
 
</pre></code>
 
</pre></code>
 +
 +
=Fifth Problem on Arrays=
 +
 +
* Write a Php program (Web page) that generates the list of all the csc231 and csc220 accounts, i.e.
 +
 +
220a-aa
 +
220a-ab
 +
...
 +
220a-az
 +
231a-aa
 +
231a-ab
 +
...
 +
231a-az
 +
 +
* Note that to generate the alphabet Php you can use this loop:
 +
 +
<?php for ($i=65; $i<=90; $i++) print chr($i); ?>
 +
 +
* Assume that the classes are stored in an array:
 +
 +
  $CS_classes = array( "220a", "231a" );
 +
 +
* Note: if you need to concatenate strings, use the '''.''' (dot).  For example:
 +
 +
  $className = "csc231";
 +
  $letter = "b";
 +
  print $className . "a-a" . $letter . "\n";
 +
 +
:will print ''csc231a-ab'', followed by a new-line character.
  
 
<br />
 
<br />
Line 108: Line 140:
 
<br />
 
<br />
  
[[Category:CSC220][[Category:Php]]
+
[[Category:CSC220]][[Category:Php]]

Latest revision as of 08:34, 22 September 2010

--D. Thiebaut 23:42, 19 September 2010 (UTC)


This lab deals with Php programming. You will need to use your 220a-xx account and create your Php files/pages in the public_html directory.

To access the page you create, simply point your browser to the URL http://cs.smith.edu/~220a-xx/yourfile.php where you will replace yourfile by the actual name of the file. Similarly, replace xx by your two-letter Id.








Important Debugging Information

  • By default, your browser will try to be clever and not reload a page twice if you ask it to reload it. Instead it keeps a copy of the page it just fetched in a cache, and reloads it from cache instead of actually getting it back from the server.
  • Since you are writing Php code, you will need to modify your code as you go along, and will want to see the result by reloading your page in your browser. To prevent the browser to use the cache and force it to go back to the server for the page, make your Php page start as follows:
<html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<PRAGMA NO-CACHE>
<head>
<title>Php variables</title>
</head>
<body>
...
 
</body>
</html>

First Problem

  • Write a php page that declares 2 variables $a, and $b, set both of them to some values of your choice, and displays their contents, sum, and difference.
  • Your page should display something like this:
$a = 5
$b = 10
$a + $b = 5 + 10 =  15
$a - $b = 5 - 10 =  -5
  • Make sure you create your page in your ~220a-xx/public_html directory, and remember to chmod a+r your Php files!

Second Problem

  • Write a Php page that uses a for loop and that displays all the numbers from 1 to 10, as follows:
1
2
3
4
5
6
7
8
9
10

Third Problem

  • Same as Problem 2, but add a test in your loop so that the output becomes as shown below:
1 odd
2 even
3 odd
4 even
5 odd
6 even
7 odd
8 even
9 odd
10 even


Fourth Problem

  • Same as Problem 3, but this time the 10 lines are output in an html table.
  • The html code for the table should look something like this:
       <table>
       <tr><td>1 </td><td>odd</td></tr>
       <tr><td>2 </td><td>odd</td></tr>
       <tr><td>3 </td><td>odd</td></tr>
       <tr><td>4 </td><td>odd</td></tr>
       <tr><td>5 </td><td>odd</td></tr>
       <tr><td>6 </td><td>odd</td></tr>
       <tr><td>7 </td><td>odd</td></tr>
       <tr><td>8 </td><td>odd</td></tr>
       <tr><td>9 </td><td>odd</td></tr>
       <tr><td>10 </td><td>odd</td></tr>
       </table>

Fifth Problem on Arrays

  • Write a Php program (Web page) that generates the list of all the csc231 and csc220 accounts, i.e.
220a-aa
220a-ab
...
220a-az
231a-aa
231a-ab
...
231a-az
  • Note that to generate the alphabet Php you can use this loop:
<?php for ($i=65; $i<=90; $i++) print chr($i); ?>

  • Assume that the classes are stored in an array:
 $CS_classes = array( "220a", "231a" );
  • Note: if you need to concatenate strings, use the . (dot). For example:
  $className = "csc231";
  $letter = "b";
  print $className . "a-a" . $letter . "\n";
will print csc231a-ab, followed by a new-line character.