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.
header(): Do I have any whitespace in my file or include-files?
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.header on php.net.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.
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');
COUNT() and GROUP BY in the MySQL documentation.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). LOAD DATA LOCAL INFILE in the help page for MySQL at ITU. ORDER BY.
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.
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!
Your pattern matched
"; } else { echo "Your pattern did not match
"; } } ?>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!
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.
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); ?>
rand(). 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!
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.number variable write this:
$numberFetched = $_GET['number'];
<!--First form: --> <form action="somepage.php" method="get"> <input type="submit" /> </form> <!--Second form: --> <form action="somepage2.php" method="get"> <input type="submit" /> </form>
$BMI = weight / ((height/100.0) * (height/100.0)).rand().
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.
<!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>
<? //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:
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.
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
Og her er en til mySQL når I når så langt:
På en Mac: (Tusind tak til Jonas Voss!)
Installation af Apache2, PHP/MySQL som nemme installationspakker (de ovennævnte kræver lidt tastning i terminalen).