
28 Apr Building a Joomla Page With Custom Php Code
{module [135]}Most of the FHA website consists of plain old articles: information, notices, solicitations, announcements and other information that’s created originally by residents, mostly representing the many groups and organizations active in Fearrington, and presented on our attractive Front Page News page, for which Dave Krepp, as the website content Manager is responsible.
However, ALL FHA directory functions, as well as the Bulletin Board, are almost entirely generated using a combination of custom PHP code (so far, written by me with some pieces cribbed from the ‘net. The custom PHP generates HTML code, and often has Javascript and CSS mixed in, so reading a page of my PHP is often a multi-lingual exercise.
Building a new page that uses custom PHP can be done in a number of ways, but I started using the method below, as it’s simple to understand and lets me use my favorite code editor (in NetBeans) to write and maintain.
If you look at the Bulletin Board article in Joomla’s Content>Articles editor you’ll find the following
First, note the top and bottom lines: “<?ρhρ” and “?>”. When a web server encounters the first of these, it stops sending the file it was serving to the browser and hands the remainder of the file to the PHP language interpreter, which processes all succeeding lines until a “?>” (or the end of the file) is encountered, and then returns control to the server to process the remainder of the file (if any). So all the statements between the “<?ρhρ” and the “?>” must be legitimate PHP language statements, as they are in the image above.
First, observe the characters “// Report all errors”: Any time you see “//” on a PHP line, you’ll recognize it as a comment, which conventionally will refer to the PHP statement preceding the comment, of to a block of code which the commented lines begins. When “//” is encountered on a line of PHP code, the “//” and the entire remainder of the line is ignored.
Comments can also appear on multiple lines if they begin with “/*” and end with “*/”. Comments delimited in this way can span multiple lines. Later you’ll see that the PHP template I wrote starts with one such multiple-line comment, as do all the pages generated with that template. Again, all material between “/*” and “*/” is ignored by PHP.
Blank lines can be used anywhere to improve readability and suggest structure. More about structure later.
So, excluding the beginning and ending “signal” lines, the blank line, and the comment-only (“/* … */” line, there are three PHP statements shown. You should look at the PHP manual to see in detail what each does*, but here’s a summary:
- The first two lines of “real” PHP code tell PHP to include error messages in the Error or Warning category in the output generated by the PHP program. Joomla normally keeps error reporting OFF to “improve the user experience”. Well and good, but I felt that errors need to be seen to be fixed, so I turn error reporting on for my code. If ever you see an error, you’ll know there is work to be done to fix it.
- The third line of PHP code says to include in this PHP code all the code in the file “/fhaphp/bb.php”: the code that displays the Bulletin Board.
This method of inserting custom PHP (and HTML and sometimes Javascript and CSS) into Joomla depends on a plug-in called “PHP Anywhere”. Its (the plug-in’s) function is to intercept any “<?ρhρ” it finds in a Joomla article, and execute that code (using a PHP “eval” statement). PHP Anywhere is rather simple-minded, and can’t handle large PHP files, nor can it do “mixed” files containing non-PHP (HTML, CSS, Javascript, e.g.) code. So the custom PHP code I’ve written just won’t work “inline” with PHP Anywhere. Hence the decision to put all this code into a separate library/directory (/fhaphp) on the website. Also, separating out the custom code allows me to maintain it using my NetBeans IDE editor, which, for code, is considerable smarter than the Joomla editor. Subsequent to the adoption of PHP Anywhere, a new plug-in called “Sourcerer” appeared, and this would allow intermixed code in other languages, but still, editing code in Joomla would be much harder than it is using NetBeans.
This article has shown how FHA customized PHP code (accessing the FHA directory, the Bulletin Board and other database-dependent facilities) are linked into Joomla. The next article will examine in detail the latest in an evolving “template” used as the starting point for any new PHP-coded pages on the website.
* Whenever I want to reference a PHP statement I google, e.g., “php require” and will usually find the PHP reference page in http://www.php.net near the top of the hits. For more complicated queries, start the line with “php” and type the question. You’ll often find good information and explanations at http://www.stackoverflow.com, which is a great resource for learning!