Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, 20 March 2007

Multiple nodes in PHP

Several of you have asked about looping round elements in XML. This is obviously desirable because, although only six places are asked for in the data, the code should not have to change when a new place is added.

Heres how to do it - [Caution - this code is unchecked]



//assume places.xml contains multiple places, each with a name and description

$placesxml = simplexml_load_file("places.xml");

$places = $placesxml->xpath("//Place");

// $places is now an array of XML elements, each a Place element

foreach ($places as $place) {

//$place now points to each Place in the XML document in turn
// so we can use object references to access the elements within a Place

print $place->name, $place->description;

// or even XPath expression as well

// and of course, if the are repeated elements in Place, such Link, with elements
//url and text, we can use a inner loop to work with these

foreach( $place->Link as $link) {
print "<a href='$link->url'/>$link->text</a>"
}
}

Friday, 2 February 2007

Workshop 2 - SimpleXML in PHP

Using the xpath function in SimpleXML in PHP is a bit tricky, so here is how to do the decoding:

Create an XML file like this. called bbcCodes.xml

and these PHP statements do the lookup:

$name = $_REQUEST["name"];

$places = simplexml_load_file("bbcCodes.xml");

$codes = $places->xpath("//Place[name='$name']/code");

print $codes[0];

Here it is running -

This PHP script uses the xpath function which returns an array of SimpleXMLElements (since this match will usually produce a sequence of elements) so you need to pick out the first one (assuming there is only one match)

Tuesday, 23 January 2007

Workshop 1 Term 2 - RSS and PHP

A voice message (2 min 16 secs)

In this workshop we will continue the work looking with PHP and the SimpleXML class by using this approach to transform data from an RSS feed (a weather feed from the BBC).

You will also compare three sources of data - from the Weather Channel,. Yahoo and the BBC to identify differences in both structure and content of these data sources, and explore the reasons for these differences.

You will also be introduced to the notion of namespaces and the basics of location data, in preparation for work with Google Earth in the next workshop

Wednesday, 13 December 2006

Week 12 - term one wrapup

There will be a lecture as usual on Friday. In this lecture, I will provide some general feedback on the assignment whilst it is fresh (burnt in?) in your minds. I will also go over the normalisation exercise which was left incomplete 3 lectures ago.

I will then introduce an exercise for the tutorial which uses PHP to process XML files and XML from a remote weather site. I think you will find this exercise simple but surprisingly powerful.

Finally we will have a poll to judge the best of the photo album sites. Make sure you bring your mobile phones because we will be using a SMS-enabled application to do this. There will a small prize for the best site.

Friday, 10 November 2006

Week 7 - Browser- Server interaction with PHP

The slides for this lecture describe how to use forms and sticky forms with PHP. Here are the full set of examples. Note that in this code, the parameters are referenced directly as PHP variables, rather than using the $_REQUEST array in the album code. This approach is usable but depreciated ( i.e. you are advised not to use it). Most (All?) commercial hosting sites will require the more wordy but more secure approach.


I have also adapted a tutorial from the WebMonkey site and added some advice on how to use PHP at home, paying special attention to how to secure your server which is very important.

Tuesday, 31 October 2006

Week 6 - 3-tier and PHP

We now move on to looking at how a web application can be developed using PHP and MySQL. The example we provide is some basic code for a photo album. This includes example code for the basic operations

Powerpoint Slides

see the PHP entry in this Blog for information and resources on PHP

Examples of PHP scripting

Friday, 20 October 2006

PHP links

PHP in CEMS

The current version in CEMS is PHP5.

There are three servers with slighly different PHP configurations.
  • www.cems.uwe.ac.uk - a PHP script will run either on web02 for students or web03 for staff. PHP on these servers has error reporting turned off. For students, www is only accessible if a request has been approved to permit access.
  • isa.cems.uwe.ac.uk - error reporting is turned on. You should use this server for all development. isa is only available within UWE
  • stocks.cems.uwe.ac.uk is an older server and runs PHP4
You can check the PHP configuration: isa, www, stocks

Hints
  • Put all php scripts in a suitable subdirectory of your public_html account on the UNIX server (in the nas folder)
  • To execute a php script, use a URL of the form
    • http:isa.uwe.ac.uk/~meonunix/DSAW1/addMember.php
  • Use the 'isa' host for all development.
  • You need a line editor like PFE32 so that when an error references a line number, you can find that line in the editor. (we need a better editor - suggestions?)
  • Beware of the slight time lag between editing a script via Windows and the same changed script being executed on the relevant UNIX server.
  • Use the $_REQUEST array to access both get and post variables. Use method="get" when testing - it's much easier to detect errors if you can see the values being passed from the browser on the command line. You can also bookmark a URL so a test can be repreated without having to enter the data again. You will also need to use 'get' when a deep link is created, for example in the Album code to fetch a a JPEG.
  • You can get a php script to display nicely using a suffix of .phps. This can help spot missing quotes or brackets. In my examples I create a symbolic link in Unix to the .php file itself. Beware of exposing passwords to your MySQL account if you do this - place connection details in a separate file which can be placed above the web accessible pages in public_html.
Links
Books
  • Larry Ullman, PHP and MySQL for Dynamic Web Sites, Peachpit Press ISBN 0321336577
  • Luke Welling and Laura Thomson, PHP and MySQL Web Development, Sams Publishing. ISBN 0672326728
  • Leon Atkinson, Core PHP Programming, Prentice Hall , 2001

Sunday, 15 October 2006

Coursework 1

The specification for the first coursework, a group project, is here and hard-copies were handed out in lectures some weeks ago.

Groups have been created.

Sample PHP code to demonstrate how to do the basic operations is provided, as is guidance on the use of PHP and MySQL in CEMS.