Incorporating PHP code into Movable Type (dynamic pages)
Dec 12th, 2005 by Karen
I spent most of today working on adding some simple PHP code to the UH Libraries Movable Type templates so that if a blog didn’t have categories then the categories section wouldn’t shown up in the sidebar and if a blog had categories then the categories section would show up. This may seem like a really simple silly thing, but because we aren’t letting bloggers control the look and feel of their blogs at all. They can’t hide the categories on the sidebar if they don’t have them. I started out working on this using the brute force MySQL/PHP method. Ie query the database and see if the given blog had any catagories and if it didn’t then skip the category part of the template. I encountered two distinct problems with this methodology though. The first problem was that I couldn’t seem to get a PHP include file containing my database connection code to work without placing it in the blog’s directory. Since I would be reusing this code for every blog, this was a huge pain. I tried changing the PHP ini file but no luck. While I could have included the connection information in the template itself and have it code work, this wouldn’t be very secure. It also make your code more difficult to maintain over time and you can end up going to several places to update database connection information.
The second problem I encountered was using the PHP include function. This doesn’t seem to work properly when using dynamic templates in Movable Type. Instead of the PHP getting processed it merely appears in the screen. I can’t seem to figure out why this is and yes Movable Type fans I am using the full <?php tag ! Other PHP code renders properly when inserted into the template. Its just if I include a php file that has additional php code. Has anyone else encountered this on Movable Type 3.2 when trying to use php in dynamic templates?
Update
Problem two appeared to be due to the fact that I forgot to wrap my include code in PHP tags. Whoops! At least, that is one less thing I have to troubleshoot tomorrow.
In the end I used a tutorial and code from Movalog, which explains how to query the database using EZSQL. Its pretty nifty and if I wasn’t PHP and SQL savvy already would simplify my life a great deal. Unfortunately, a part of me wants to do it the old fashioned way. Mostly because it isn’t just data from the Movable Type database that I need to incorporate into the blogs, but rather data from the database the drives the Libraries website. So tomorrow it will be back to the drawing board to see if I can figure out how to insert data from a database into our blogs using regular PHP.

