W2

Web Publishing with Databases
Fall 2004

Latest change: 2/11 2004

This is the homepage of instructor Camilla. Here you will find some hints for each exercise and the dates for turning in exercises. Lasse and Martin, the other instructors also have homepages. Go take a look at them, they might have the answer to something i haven't thought of.

Links:

Exercise 9

  1. If you want to, you can make more than one table.
  2. Easy-peasy
  3. Please make the structure into an image, not a word-document!
  4. This is where problems B and C should be a help to you now. If you know what you are coding, it is so much easier than if you are unclear about the structure.
    If you have any problems, then try to go through this checklist:
    1. Am I getting my form-variables right? And are they passed between pages OK? Try echo'ing them on all pages.
    2. Is my SQL query OK? Do test it in the shell before you use it in the script.
    3. When you use header(): Do I have any whitespace in my file or include-files?
    Try to test your code very often as you write it. I sometimes test after every 5 lines to be sure. The thing is, once you have 50 lines of code it is very difficult to locate an error. It is much easier with smaller amounts of code.
    When you suspect an error in a piece of code, try to isolate it (perhaps in another file) to see if that is really the problem.

Exercise 8

  1. This should be the easy part.
  2. There are a few things that are important here. First, make the inclusion of mydb.php work. Extract some comments from the database and display them on the page. Then, make the link on your index-page
  3. When using header() you have to make absolutely sure that no output has been send to the browser before the header. That means any output - even whitespace.
    Read the documentation for header on php.net.
    Also remember that header() takes an absolute URI - so header("Location: comments.php?url=$url") is wrong, whereas
    header("Location: http://www.itu.dk/people/yourlogin/w2/ps8/comments.php?url=$url") is correct.
  4. You should remember from the "guess a number" assignment that hidden variables are a good way to pass values on betweeen pages.

Exercise 7

  1. When you insert data into your tables you can write all insert statements like this:
    INSERT INTO mytable VALUES ('Peter', 'Pan');
    INSERT INTO mytable VALUES ('Betty', 'Boob');
    INSERT INTO mytable VALUES ('Lonnie', 'The Liar');
    INSERT INTO mytable VALUES ('Monty', 'The Melon');
    
    That is a lot of redundant text. In MySQL you can write the insert statement once, and then seperate data with commas like this:
    INSERT INTO mytable VALUES ('Peter', 'Pan'),
                               ('Betty', 'Boob'),
                               ('Lonnie', 'The Liar'),
                               ('Monty', 'The Melon');
    
  2. Check COUNT() and GROUP BY in the MySQL documentation.
  3. When you use LOAD DATA LOCAL INFILE, you have to use a UNIX-path to tell MySQL where to find the file you want it to read. My UNIX-path to wwwstats.txt is /import/home/ckj/wwwstats.txt. That is assuming that I put the file in the root of my folder (that would be H:\ in windows language).
    So what you do is put your file in the root of your folder and change ckj to your login.
    Read about LOAD DATA LOCAL INFILE in the help page for MySQL at ITU.
    You should also take a look at ORDER BY.

Exercise 6

This exercise is not about PHP at all. It is about a good friend of PHP's - MySQL. Like PHP, it has a documentation page that you should use a lot from now.

If you read Danish, this small tutorial might be helpful. MySQL has a tutorial on their own website as part of their manual. It also has some instructions for installing MySQL.

As it says in the problem set.. Read instructions for MySQL before starting!!

When you hand in the solutions, do a copy paste of the content in the SSH-secure shell.

Exercise 5

Regular expressions are important in PHP. To check user input, it is very helpful. This page may give you some hints.

You should really check this Super cool thing that can generate an image of the states in a regular expression. Try it!

  1. Martin has a pattern tester in his page. I'm not sure, but it seems to disagree with this one some times, so try both if you are confused. Your pattern matched

    "; } else { echo "

    Your pattern did not match

    "; } } ?>
    " method="post" > Input pattern:

    Input string:

  2. For this problem I strongly suggest that you take everything into small bits. For instance, first check that your function fetchurl is working and gets the page you want it to get. Second, check the source code of the file you get to find the context in which the dollar rate will appear. Third, write a pattern to match it. Fourth, test it in the pattern tester above against the string you are looking for. Last, put it all together!
  3. Your turn to be creative!

Exercise 4

This problem set is mainly about arrays. PHP uses arrays a lot, and it is very good idea to spend some time getting to understand how they work. Try to play with them a little, going through them with for-loops, while-loops, and foreach-loops. I found some short stuff about arrays here.

  1. Remember that count() returns the number of items in the array, not the highest index number:
    <?PHP
      $arr[0] = "hi";
      $arr[1] = "there";
      $arr[2] = "stranger";
      // the length of the array is 3, 
      //even though 2 is the highest number
      echo count($arr);
    ?>
    
  2. Here is the documentation for rand().
    This is an exercise in passing on variables between files, or preserving the state of variables. This is good article on preserving state.

Exercise 3

It is important that you understand how to use functions. They are central to programming in PHP. You might want to take a look at this page, which has some basic stuff about functions. PHP Beginner has a really nice little tutorial on functions. -Read it!

  1. The form variable number means a variable accessible from the address bar. That would be a GET-variable. It is a really good idea to use $_GET to access form variables, you can read here why I think so.
    If you want the number variable write this:
    $numberFetched = $_GET['number'];
    
  2. Just like one can increment a numeric value using $variable++, one can decrement it by using $variable--.
    When a page has two forms it means that there are two sets of form tags. For instance:
    <!--First form: -->
    <form action="somepage.php" method="get">
    <input type="submit" />
    </form>
    <!--Second form: -->
    <form action="somepage2.php" method="get">
    <input type="submit" />
    </form>
    
  3. The body mass index is computed by using the formula:
    $BMI = weight / ((height/100.0) * (height/100.0)).
    Try to think of what happens if the user enters a string instead of a number. Take a look at the very useful function is_numeric to help you check that the user actually entered numbers.
  4. You are, of course, not asked to represent the dices graphically. Take a look at the documentation for rand().

Exercise 2

In the beginning of the problem set it says that you are to make 3 web services. The difference between a web service and an ordinary web page is that a web service can consist of more than one file, and it usually prompts the user for some input to be processed. So Problems B, C, and D are web services whereas problem A is just an ordinary dynamic web page.

Please remember to place a link to the solutions on the index-page you made last time. Also remember to put a link from your solutions back to the index page.

To pass information from one web page to another one uses either POST-variables or GET-variables. It is very important that you understand the passing around of variables and the difference between GET and POST.
There are a number of ways to get the value of a form variable. You can read about the way that the makers of PHP recommends to get them here.

  1. Remember to ALWAYS put a header and a footer on the page that is outputted to the browser. It is no problem to mix HTML and PHP like this:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>My web page<title>
    </head>
    <body>
    <p>
    <?PHP
    $tempcelcius = 25;
    echo $tempcelcius;
    ?>
    </p>
    </body>
    </html>
    

    You can mix it even more to format the output:

    <?
    $tempcelcius = 25;
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>My web page<title>
    </head>
    <body>
    <p><? echo $tempcelcius; ?></p>
    </body>
    </html>
    
  2. If you feel like it, try to consider what happens if the user does not enter an input in the form. Or what happens if the user enters letters instead of numbers? If this gets you thinking, you might want to look at the is_numeric function.
  3. As in problem B, try to consider what happens if the input is something unexpected.
  4. Here you are to dynamically generate a recipe with the ingredients for an Apple Pie. Remember that you can make your own variables, like a useful string for instance. In this problem, it might be nice to have a string with </li><li> to use between ingredients. One could do it this way: (a "//" is a comment. The PHP interpreter simply ignores everything on the line after a "//")
    <?
    //putting the value in my variable 
    $li = "</li><li>";
    ?>
    <ul>
    <li>
    <?
    echo "butter";
    echo $li; //using my variable
    echo "sugar";
    echo $li;
    echo "apples";
    ?>
    </li>
    </ul>
    

    This would output:

    • butter
    • sugar
    • apples

Exercise 1

This exercise is on basic HTML and a little PHP. Don't spend too much time making the HTML really flashy and complicated. If you really feel creative and colorful, use a style sheet. This actually applies for all exercise. The simpler the code (both PHP and HTML), the easier for both you and me to understand.

  1. You will, during the course, be asked to put a link on the page you're making now for each exercise. To make it easier for me to navigate your pages quickly, please put the newest exercises at the top of the page. Remember to write ALL HTML tags in lower case. If you really insist on using upper case, then do it all the time. So: be consistent, choose either upper or lower case.
    The magic formula for writing links i HTML is as follows: <a href="http://www.google.com">Google</a>
  2. When you write for instance <form action="blah blah"> remember the quotes around the value of the attribute (in this case the attribute is action and the value is blah blah).
  3. It also says so in the exercise text, but a lot of people get confused by this in the beginning: When you save your file in th w2-folder in H:\public_html they are put on the school's web server. That means that if your file is a PHP file, the PHP interpreter will interpret it before sending the output to the users browser. This is just the way you want it.
    Where people get confused is when they open windows explorer and click the files they put in the public_html-folder. A browser will open the file as expected, but the PHP will not be interpreted by the interpreter, because it only lives on the web server. You have to "exit the building and come back again" so to speak to use the school's web server.

A few links to help install an Apache web server and PHP at home

If you have a slow Internet connection at home it's nice to have one's own server at home. With a server you can test your code on your own machine. The links are to pages in Danish, but if anyone finds a good site with a how-to in English please let me know.

På windows:

Her er en guide til installation af Apacheserveren

Her er en til PHP-fortolkeren

Og her er en til mySQL når I når så langt:

På en Mac: (Tusind tak til Jonas Voss!)

PHP

MySQL

Installation af Apache2, PHP/MySQL som nemme installationspakker (de ovennævnte kræver lidt tastning i terminalen).