PHP

Building a nested array from a flat one

This post is largely because I want to remember hos to do this, but maybe it’ll be good for someone else out there.

To change an array like this:
[php]
Array (
[0] => Array (
[RECORD_PARENT] => 0
[RECORD_DESC] => Root #1
[RECORD_ID] => 1
[LEVEL] => 1
)
[1] => Array (
[RECORD_PARENT] => 0
[RECORD_DESC] => Root #2
[RECORD_ID] => 2
[LEVEL] => 1
)
[2] => Array (
[RECORD_PARENT] => 0
[RECORD_DESC] => Root #3
[RECORD_ID] => 7
[LEVEL] => 1
)
[3] => Array (
[RECORD_PARENT] => 7
[RECORD_DESC] => Under Root #3
[RECORD_ID] => 8
[LEVEL] => 2
)
)
[/php]

Into a nested parent/child array, use this:

[php]
$ret = array(‘root’=>array());
$ref[0] =& $ret[‘root’];

foreach($retv as $row) {
$p=$row[‘RECORD_PARENT’];
$i=$row[‘RECORD_ID’];

$ref[$p][$i] = array(‘data’=>$row,’ch’=>array());
$ref[$i]=&$ref[$p][$i][‘ch’];
}
echo ‘

'; print_r($ret); echo '

‘;
[/php]

voila!

ZendCon08 and the PHP Community

Day four of this year’s Zend/PHP Conference and Expo has finally come – we all wish it could have been just a bit longer. There was tons of stuff jammed into this year’s event, both in the sessions and activities outside the conference hours. These four days went too quickly for me as it was one of the very rare times that I can sit with the people I talk about and with every day online and just have a beer and shoot the breeze. I have the user group at home that’s fun to go to and I always learn a lot there, but there’s just something different about getting to meet up with developers you know, ones you don’t and people you might never get to see (or not for a long time) because of where they’re from.

See, that’s what these events are really about (or should be about). Yes, the sessions are good – it’s nice to listen to people go one about scalability and security techniques, but what’s really at the heart is the community that the conference inspires. You can walk down the halls and see it. People getting introduced to other (“oh, you should so meet…”) and groups just standing around and talking about things, PHP related and not. It’s was even there at breakfast when people would get their abnormally small coffees, a bagel and sit down with people they may not even know and start talking. During these past four days, we were all equals – it wasn’t about what you know so much as the fact that you were willing to sit down and share a part of you and your experiences with another person.

Unfortunately, there’s only so much time and so many resources to go around and we all have to head home sometime. For some it’s just up the road but others are quite a bit further away than that (and even some, further than that). The people we met and discussions we had start fading away a little bit and those business cards in our pocket become more of “that person”.

I wish there was a way that we could keep this kind of connectivity going even when we’re home in our familiar houses and offices. It’s just not that easy, unfortunately, but the effect can be lessened. Stay in touch with those people! Send them emails asking how that project they mentioned to you is going! Get them in contact with other people you know who could help them out.

It doesn’t matter if you came to the conference and really only met one or two people – stay in contact with them. These conferences (and really the same things applies to user group meeting too) are not just things to come to and sit in rooms all day, isolated in your thoughts. You can take online classes if that’s all you’re looking for. Conferences/meetups/coffee with other developers is about the networks – building those links between you and other people that love the language you love and that can be a huge help if you might need it.

Don’t feel left out if you didn’t get to ZendCon this year – it’s just one of the many many great conferences happening this year. Can’t make it to a conference? Look for a local user group! There’s bound to be one in your area (and if there’s not, look at starting one up). You don’t even have to make it out to events if you don’t want to. I recommend coming over to the irc.freenode.net IRC network and hopping in on the #phpc channel. There’s tons of great people in there and it’s just fun to talk with other developers about anything and everything.

It’s all about the community.

Why do you use PHP?

A little while back I asked this question on Twitter. I was interested in how other people’s first experiences with PHP compared with my own. I got some great responses – from funny to completely honest. Here’s the list:

  • Skoop: I didn’t choose PHP, PHP chose me (nice, very zen.)
  • Andriess: low entry barrier, and the AWESOME community
  • Felixdv: great community, open-source spirit and low barrier but powerful if needed (as of PHP5 ๐Ÿ˜‰ )
  • DragonBe: in PHP I found my likings, where as Perl left me hanging about..
  • Rmehner: Mainly because of deployment issues in the beginning. PHP was everywhere available. (In the beginning I had often web projects)
  • lvtrll: The options at the time were PHP or ASP… which would you choose? =P
  • ijansch: it was the logical choice (or, to be more precise: it gets the thing done, and quickly.)
  • njames: its “FREE” no need for mucking about with licences for VS or IIS or W2K or MSSQLSRVR etc etc #php
  • weierophinney: out of necessity.
  • padraicb: At the time (1999) PHP happened to coincide with the activities of friends online – seemed natural I’d help them cut and paste ๐Ÿ˜‰
  • iephp: I used to do Java and Perl and a friend of mind told me PHP was easy and fun. I tried it out, turned out well ๐Ÿ˜‰
  • akrabat: Wasn’t that much choice back in PHP3 days. Perl / CGI didn’t appeal and the only other choice on my host was PHP…
  • jlleblanc: someone suggested it in college and it was easy/inexpensive to get into.
  • sweatje: My Unix admin pointed me towards LAMP when I was looking for a free alternative to IIS/ASP for a home business accounting system
  • chartjes: My first job out of college needed a web site and had no money to pay for licenses for Windows server (this is 1998)
  • ramsey: Switched from ASP to PHP b/c I didn’t want to learn ASP.NET, Tomcat was a bitch, & PHP had everything we needed built in (& more)
  • calevans: because upgrading from NT to Server 2000 was going to cost me $15,000 + hardware.

I think I got them all, but if you didn’t get to contribute, definitely leave a comment with your first introductions to this great language of ours!

Ivo Jansch’s “Guide to Enterprise PHP Development”

I’ve been working through this book for the past few days and let me just say from the get-go, this book would be any asset to pretty much anyone on the PHP development food chain. It’s not a book for programmers telling you how to survive in an enterprise environment (there’s some of that, but that’s not the focus). The book gives the reader one of the most valuable things anyone in a corporate environment can have – how it all works. It might not be 100% the same as things around your office, but Ivo does a great job of giving everyone involved in the development of web apps (and it doesn’t have to be PHP!) a better idea of how all the pieces fit together.

The flow of the book follows a typical project flow with topics like:

  • Gathering requirements
  • the Planning stages
  • which of your Building Blocks you’ll need to get the job done
  • Developing the application
  • Pushing it out to the public and keeping it maintained for its users

For each, there’s lots of great tips crammed in with suggestions and recommendations based on some of his previous experiences.

There’s little to no code in the book, so don’t buy it expecting tutorials on writing code in an enterprise environment. What you will find here, though, is a better idea of where that code you’re writing will fit in the bigger picture.

Let me also reinforce that this book is not just for developers. Honestly, anyone involved with the creation of web-based applications can benefit from it. Managers, developers and testers will all find bits in here that they can pick up and hang on to for current and future projects.

I also like that he included the part there at the end about the different development methodologies. It’s a nice addition that can help open up the reader/developer’s eyes to new ways of doing things (Agile is especially fun).

I’d definitely recommend that you pick up this book if you’re going to be doing development in anything more than a one man shop. Actually, strike that – I’d recommend it for anyone that wants to get more familiar with the management side of development and how their code works in.

More information: php|architect’s Guide to Enterprise PHP Development

Subdomain Setup with Solar

So here was my situation – I wanted to have a subdomain off of my main site, but I didn’t want to have to create a whole new docroot with an entire new Solar framework application in it. Besides being “yet another install” of the same sort of thing, it would also be a pain to keep up more than one codebase that does a lot of the same things.

This got me to thinking that there had to be a way to convince Solar that it could use the same code with the subdomain without issue. Sure enough, it could – and it was actually really easy. Here’s my situation:

I wanted to have the main domain’s stuff still work (www.mydomain.com) just like it always has but I wanted the subdomain to point to one controller out of the main application (in this case, the Solar_App_Foo controller) when the subdomain was called (foo.mydomain.com). Solar makes it dead simple – it’s just a change in your config file.

Since it’s PHP driven, you can do fun things like checking to see what the value of $_SERVER[‘HTTP_HOST’] is right there in the definition:

$act=($_SERVER['HTTP_HOST']=='foo.mydomain.com') ? 'foo' : 'index';
$config['Solar_Controller_Front']['default']=$act

That’s all there is to it – the ternary check looks for the subdomain and sets the default controller to our Solar_App_Foo instead of the Index the rest of the site calls. And, since it’s just part of the same site, all of the links and other functionality work just fine. Plus no extra code to maintain!

Hope this helps to anyone else out there trying to work with subdomains with Solar. Thanks to the crew in #solarphp on Freenode for the help!

Enterprise PHP (the Magazine)

Opening my mailbox yesterday welcomed me with a brown paper envelope addresses to “Herr Cornutt” and postmarked. I was confused since I wasn’t expecting any bit of German to be coming my way. I was happily surprised, though, when I opened it to find the free issue of Enterprise PHP, one of the newest offerings into the PHP magazine category.

The publication is from the IT Republik folks and is a nice looking magazine. It comes in at about 50 pages but it has a good ad to story ratio so you don’t feel slighted. Stories include:

  • Test ’em all! (by Sebastian Bergmann)
  • Worlds apart… (by Patrick Lobacher covering PHP intagration and SAP systems)
  • an interview with Jens Paul Berndt about decisions regarding PHP
  • a case study of the Fiat Group SpA’s use of PHP in their systems
  • and the cover story, “Java is not PHP”

The quality of the articles if good – it’s interesting to see their use of so much clip art as illustrations. I suppose its due to the different audience than some of the other magazines. It’s less about the development and more about the high-level thoughts and processes behind PHP in business – how it can function in their corporation (or small business!) and what sorts of considerations need to be made. The articles are good quality and keep consistent with the tone of the magazine. The quality of the printing is nice too (always a plus) and the thicker pages make the “enterpriseness” of it all feel even better.

Oh, and let’s not forget about two articles by Cal Evans of the Zend Developer Zone! One covers several of the popular PHP frameworks and the other about how PHP security has advanced in the past few years.

If you get a chance and want to check out something a bit different in PHP Magazines, head over and grab a trial issue of this newest addition to the PHP publication world. Here’s hoping we’ll see great things from them!

Simple Content Management (with Templates & Permissions)

I’ve been working on a sort-of content management system at work and I wanted to get some opinions on the structure of it. Here’s the basic summary:

I have a database table that keeps the content for me (title, content, date stamp, etc) – each item has a type. My goal was to have a system that would be flexible and allow me to store both hierarchal and date sorted information easily and all in one place. I set out with the intent to make it something I could potentially use for simple blogs, forums and even static content like FAQs.

Basically, everything is a child of something – there’s a starting point (the top parent) and all of the children from there down. Since it’s on Oracle, CONNECT BY is my best friend. I can point it at a parent and get back a recursive array of the values. So, if our top level was “blog” then its children might each be a “blogentry” with each of those having a collection of “blogcomment” content blocks.

You can see how the same sort of thing could apply itself to a forum layout (remarkably similar, actually). Parent/child all the way down, allowing for any number of nested levels of categories and topics.

Now comes the fun part – the PHP code.

I’m not quite done working all of the kinks out of it yet, but it’s close. Here’s the though process behind it, though. A fetch() function is called on the top-most parent to get it and its children’s data. This is passed to a display() call to be handled. Inside of the display() call is a bit of logic that starts looking at the types of the data. The TYPE column in our table is really the key to how the whole system works. The display() logic looks at the type of the first item of data passed in and loads in a Helper from a predefined directory (include_once, of course). These are named according to the type they help with – so HelperBlog helps with type “blog”, HelperBlogentry helps with “blogentry”, etc. These are loaded, a new object is made and the display() method is called on it.

The children are passed in to this method where, if the child class (HelperBlog or whatever) chooses, they can be iterated over. The fun thing is that since the class extends our main class (in my case DynContent), we can just call the parent::display() method with the child data and it will recurse down through each of the layers.

There’s more to it than just this (templating, permissions, etc) that I’m still working on, but it seems like it has potential. I’m curious as to if anyone else out there has approached this kind of idea in a similar way. I’d love to hear feedback/comments/whatever about the idea from anyone out there.

I don’t have the code posted anywhere yet, but if you have an interest let me know in the comments.

Book Review: Beginning PHP and Oracle (Apress)

The nice friendly people over at APress sent me a few new books the other day, one of which is “Beginning PHP and Oracle: From Novice to Professional” by W. Jason Gilmore and Bob Bryla. Of the three, I was most interested in this one as a possible resource to hand off to other people in our company (the Oracle developers, specifically) for them to get started with PHP. Thankfully I can say that, after going through the book, it looks like an excellent fill to bridge the gap between most Oracle developers and the world of PHP.

If you’re a PHP developer, pick up your copy of the book and follow my lead – set the book, spine down, on the table and stick your finger right in the middle. To your left is all of the PHP knowledge you’ve already learned and to your right is a wide open range of Oracle goodness just waiting for you to soak it all in. The first half of the book is an excellent introduction to PHP and can be handed to that special Oracle developer in your life who would like to get to know the language. The usual topics are there – the basic syntax, functions, arrays, object oriented programming, PEAR and lots more. If you’re just going in for the Oracle/PHP combo, you’ll find a lot more than you were asking for (which can be good and bad).

Things switch around at about the Chapter 26 mark where the first hints of Oracle start to show. This is where a lot of the Oracle developers out there can tune out a little more. The first few Oracle chapters deal with setting up and getting to know the Oracle environment, how to use views and transactions. Things get interesting when PHP jumps back in, though. PHP and Oracle developers alike can learn lots here.

Starting from Chapter 32 on, the rest of the book is devoted to the happy union of PHP making requests via the Oracle drivers to a local database (they use a local copy of Oracle Database XE in their examples). They include examples using transactions, generating a table of results with PEAR’s HTML_Table and using views and triggers in your application.

This book works well for both audiences – the PHP developer wanting to learn what all the fuss surrounding Oracle is about and the Oracle developer looking for a peek into the world of the web’s most popular web development language. There’s a little something here for everyone (there’s even a chapter on web services!) and it will be finding its way to the desks of several Oracle devs around here that have been bugging me to show them “that PHP thing” they’ve been hearing about.

Something a little more substantial – the Table of Contents:

  • Chapter 1 Introducing PHP
  • Chapter 2 Configuring Your Environment
  • Chapter 3 PHP Basics
  • Chapter 4 Functions
  • Chapter 5 Arrays
  • Chapter 6 Object-Oriented PHP
  • Chapter 7 Advanced OOP Features
  • Chapter 8 Error and Exception Handling
  • Chapter 9 Strings and Regular Expressions
  • Chapter 10 Working with the File and Operating System
  • Chapter 11 PEAR
  • Chapter 12 Date and Time
  • Chapter 13 Forms
  • Chapter 14 Authentication
  • Chapter 15 Handling File Uploads
  • Chapter 16 Networking
  • Chapter 17 PHP and LDAP
  • Chapter 18 Session Handlers
  • Chapter 19 Templating with Smarty
  • Chapter 20 Web Services
  • Chapter 21 Secure PHP Programming
  • Chapter 22 SQLite
  • Chapter 23 Introducing PDO
  • Chapter 24 Building Web Sites for the World
  • Chapter 25 MVC and the Zend Framework
  • Chapter 26 Introducing Oracle
  • Chapter 27 Installing and Configuring Oracle Database XE
  • Chapter 28 Oracle Database XE Administration
  • Chapter 29 Interacting with Oracle Database XE
  • Chapter 30 From Databases to Datatypes
  • Chapter 31 Securing Oracle Database XE
  • Chapter 32 PHPรขโ‚ฌโ„ขs Oracle Functionality
  • Chapter 33 Transactions
  • Chapter 34 Using HTML_Table with Advanced Queries
  • Chapter 35 Using Views
  • Chapter 36 Oracle PL/SQL Subprograms
  • Chapter 37 Oracle Triggers
  • Chapter 38 Indexes and Optimizing Techniques
  • Chapter 39 Importing and Exporting Data
  • Chapter 40 Backup and Recovery

Deploying PHP Applications?

So, a question for everyone out there – we’re looking to do a bit of an overhaul for our build and release system and I was wondering what kind of setups you all out there had for your releases?

I’ve seen all sorts of different things (including a version control->rsync to production push and a fully CruiseControled push for everything) but I wanted to hear back from you fellow PHPers out there as to the kind of stuff you’re using. We’re looking to try to keep it open sourceish stuff, so suggestions down that line would be best but we’re pretty open.

I don’t have much experience with a more formalized build process but we’re coming up against a need to separate out the responsibilities a bit more.

What do you use for your build (and deployment) process for your PHP applications and websites?

Keep PHP Alive! Grow a Beard!

Apparently, beards and programming languages have a direct correlation with each other, at least according to Tamir Khason. His latest list (a “take two” from this older post) reinforces the idea, pointing out lots of different languages and the people involved. Basically, the facial hair (beard, mutton chops, goatee, soul patch, whatever) of the major players involved is an indication as to how well the programming language is doing. Language in the “No Facial Hair” crowd include F#, IronPython and Prolog while the cool cats in the “Facial Hair Everywhere” group include C, Perl, Ruby and Python.

So, where does PHP fit on the list? Well, he points to this picture of Rasmus Lerdorf as a positive indicator for our beloved language, but there just might be enough other developers out there to counteract his effect.

For example:

Though thankfully, there’s one growing part of the PHP community that makes it so much better without even having to worry about the facial hair (thank goodness) – the PHP Women (coming soon to a conference near you!)

So, what’s the result? Does PHP pass the “Khason Test” for survival? Could the best way to support the community possibly be to let that facial hair grow? It’s too soon to tell, if you ask me – right now, though, I’d say the current follicle count is tipping in favor of PHP being around for a good long time…

(Oh, and in case you’re wondering – yes, I am a little on the scruffy side myself)