Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Rediscover (and Structure) CGI with a Simple Link Technique : Page 3

CGI is the most widely used protocol for Web programming, but it lacks basic programming constructs such as function calls. You can work around these weaknesses by encoding function calls directly into Web links easily creating active links—and you can do it in PHP, Perl, JavaScript, and Java.


advertisement
Mult in PHP
PHP is very flexible, and this allows active links to be used with great succinctness.

Since this is the first you’ll be looking at, we'll spend some time looking at this appin some detail. Here's what mult() looks like:

function mult( $a, $b ) { $prod = $a * $b; print "<center> <h1> $a x $b = $prod </h1> </center≫"; }

Simple enough—it takes two numbers, multiplies them, and returns the result, formatted nicely with HTML.



Now, take a look at the multiplication table:

print( "<center>" ); print( "<table border=\"1\">" ); print( "<tr>" ); print( "<td>X</td>" ); for ($c=0; $c<10; ++$c) { print( "<td>$c</td>" ); } print( "</tr>" ); for ($r=0; $r<10; ++$r) { print( "<tr>" ); print( "<td>$r</td>" ); for ($c=0; $c<10; ++$c) { $link = alLink( "mult", $r, $c ); print( "<td> <a href=\"$link\">$r x $c</a> </td>" ); } print( "</tr>" ); } print( "</table>" ); print( "</center>" );

Again, it's pretty simple. It just iterates through all pairs of numbers and makes a link for each one.

Here's a closer look at the creation of the link:

$link = alLink( "mult", $r, $c );

alLink() is the function that creates an active link. This call is like the ideal example I showed above, except not quite as perfect. The arguments to alLink are as follows:
  • The function to be called—mult()
  • The first number
  • The second number
In other words, the line above is a clumsy way to specify a function call. It would be more elegant to say something like this:

$link = alLink( mult( $r, $c ) );

But instead of freezing the call, the above would invoke it right then and there, which is not what you want. If you can't have the syntax you love, you have to love the syntax you have.

That takes care of the easy parts—the construction of the link, and the routine that it invokes. Now, take a look at how the link invokes the routine. The muliplication table is generated in index.php. If you look at the source, you'll see that each link looks something like this:

http://hostname/devx/activelinks/php/alinvoke.php?_alinfo=a%3A3%3A%7B.....

I've left out a lot of junk at the end—the actual link is much longer. In this implementation, every link looks the same. They all send the browser to a PHP script called alinvoke.php, and there's always one parameter, called _alinfo, which is bound to a really long string of junk.

alinvoke.php contains mult(). It also contains the following line at the top:

require_once( "activeLink.php" );

activeLink.php contains the active link library. It also contains the call that invokes mult(). When the user clicks on the link, the browser is sent to alinvoke.php. This in turn reads in activeLink.php, which defines a bunch of functions and then calls alExtractAndApply(). This function takes the junk in the _alinfo parameter, decodes it into a function name and a list of arguments, and then calls the function, which is also defined in alinvoke.php.

This is what that means for the programmer: to call a function from an active link, you just put the function into alinvoke.php, and the build the link in your Web page. Or, if you don't want to put all of your code into one file, you can put it in another file, and use require() to pull that code into alinvoke.php. In other words, it's just like creating a regular function, except that it gets called when the user clicks on a link.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap