Nov 7, 2009

PHP Proxy Pass-Through using the “Foreach” Loop and Pointers

During my iteration #2 presentation I demonstrated the use of a “foreach” loop in my PHP proxy pass-through and wanted explain it a little further. I figured the best way would be to do it with a blog posting.

My proxy pass-through is passed a variable labeled “input”, this is variable passed from a javascript function that excepts the user input from a textbox. Currently this function isn’t terribly robust as it doesn’t check from non-letter characters, but is shown below:


Then the php file associates the value that was passed into the variable “$name_query” using the GET command. This variable is then used to load the XML file that is being parsed for our search request. Once the file has been loaded, the “foreach” loop searches through the requested XML file for the label “location” using a pointer command and assigns the results to a new variable named “$_location”. With each found result it then uses a pointer to get the locations child elements for the reviewlink, name, address and phone information while putting the results in a nicely formatted manner.

The loop continues through the results, stacking the result information nicely on the web page in it’s own empty DIV for the user to review. This is a rather simple and clean way to search and display results from an XML file using PHP and a loop. Below is the PHP code used:

And here is the final result as shown for iteration #2.

http://people.emich.edu/jaltmann/beer2.html

4 comments:

  1. James, It's nice to see alternatives to some of the other methods of doing a proxy pass-through and getting the data that you want. I like the pointer option and hope to experiment with that.

    ReplyDelete
  2. I guess that's what you were telling me about after class. That sounds interesting and I will definitely give it a shot. Great post!

    ReplyDelete
  3. This is a cool way to do it. I remember reading something about the simplexml stuff in php, but I'm not familiar with how it works. According to what you've posted here it looks pretty simple. I parsed through all my xml stuff in JavaScript, so it's cool to see it done here on the server side in PHP. Thanks for the info James!

    ReplyDelete
  4. This a really great idea, and I think I have to adopt it for my project. Thanks for the good description and the picture!

    ReplyDelete