Arrays in PHP:
According to the PHP.net site, " An array in PHP is actually an ordered map. A map is a type that maps values to keys." What does this mean? We usually think of an array as a list of data: In Lab 3 we created an array called $words out of the current top-ranked words at 10 x 10. We can refer to different elements in the array using an index, eg $words[0] refers to the first word, $words[1] to the second, etc. Another way to describe this indexing is to say that we have a mapping from the numbers {0, 1, 2,..., 99} to the 100 current words. The numbers are the keys here and the words are the values. PHP allows us to work with mappings where the keys are not restricted to numbers. The image to the right describes a mapping from keys {title, isbn, edition, publisher} to values {Learning XML, 0-7357-6821-7, 2, O'Reilly}. This mapping is defined in PHP with the following code:
<?php
$bookInfo = array("title" => "Learning XML",
"isbn" => 0-7357-6821-7, "edition"=>2,
"publisher"=> "O'Reilly");
>
Note that the values include mixed data types - strings and numbers. The keys can also be mixed and the traditional approach involves using numerical values for the keys.
We access (key, value) data using the syntax $bookInfo['isbn']. Test this out by copying and pasting the above code into a new script and echoing out all the values, one by one.
Review of HTML Forms:
Read the W3schools primer on html forms if you need a refresher. Create an html document that contains a form that allows the user to input the book data associated to the above array example. Use radio buttons for the edition and a select box for the publisher.
Handling GET Request Data in PHP:
Get requests are typically used whenever the request does not lead to a change in the server. When the method attribute of our html form is set to GET the htpp request is appended to include the form data formatted in name/value pairs (note the url in the browser's location window in weather app graphic at the top of this page).
PHP makes it particularly simple to get at the form data. _GET is a global array variable that stores the form data in (key, value) pairs where the key is the name of the particular form element (given using the html name attribute) and the value is the form elements value. For example _GET['title'] refers to the title of the book provided that there is a form element named "title". Building on the code from part (1), write a script that echoes all the form data back to the user. You will need to modify the html form tag to look something like:
<form name="book" action="echobookinfo.php" method="GET">
where echobookinfo.php is the name of the php script that will process the form request and book the name of the form. (Of course other names would be fine.)
Creating a Domain Specific Search Engine
Many websites provide site specific search capabilities. For example the php.net site provides very useful search functionality. This is easy to do in PHP as the heavy lifting is left to the Google. The key to this is understanding how Google processes advanced searches. Try out a Google advanced search where google is restricted to looking on the purchase.edu domain. Note the url sent back to the server as part of the GET request.
Create a simple web page that allows the user to do a search of the purchase.edu domain. The query string should be sent back to a script on the server which has to splice together the appropriate google url. A php script can read a (text) file and output it using the readfile function. For example the statement
readfile('http://www.amazon.com');
will have the effect of redirecting the browser to the amazon website.
(For Hackers) A More Advanced Domain Specific Search Engine
Improve your domain specific search engine so that it allows multiple query words separated by blank spaces. As before the key to this is understanding how Google processes advanced searches. You will need to parse the query string and splice together the appropriate url using a suitable PHP String function and a forEach loop.
Uploading Work to Personal Web Page:
Upload your work in this lab to your personal page.
|