I just got one of those Borders reward cards and I asked how long it would take me to win a car...she said a LONG time. Well, I think if I had used one for every time I went there and read up on PHP, I would have that car by now. A Caddy.
Sometimes, you just need a hint. Sometimes, you Google like crazy to find things that sound like they might work (like "variable variables"). Here's a link to some key manual pages. The folks who leave comments there are little buddhas.
Main documentation in English : http://www.php.net/manual/en/
Array functions : http://www.php.net/manual/en/ref.array.php
Variable variables : http://www.php.net/manual/en/language.variables.variable.php
Nothing beats working through the examples!
As for the page system, I went bit OOP nuts, but from what I could GLEAN out of Profesional PHP 5... :glare: ...and the PHP Cookbook... 😃 ...it was enough to figure out that what I wanted to do was set up a system whereby pages are controlled by one class and inputs to the page (like headers, content) were "pulled in" from other classes by the page controller then spit out all together. ALSO, my GateKeeper controls access and session management, which if there's no logging in, I can simply disable within the "openGate" function, rather than having to recode everything.
What this allowed me to do was make changes in one place and have them flow through, without having to recode. I think I'm on my 20th iteration, but it's fun and it's my own stuff, so it's ok. One class that I absolutely encourage you to write in two levels (parent, child) is a data validation/cleaning class. The parent class contains all the obvious stuff that doesn't change, no matter what type of application it is:
function cleanPhone()
{
$D = trim($this->value);
$D = ereg_replace( "[^0-9]", "", $D ); // Strip out non-numerics
$this->value = ereg( "^([0-9]{3})([0-9]{3})([0-9]{4})$", $D, $itemParts ) ? $itemParts[1] . "-" . $itemParts[2] . "-" . $itemParts[3] : "000-000-0000";
RETURN $this->value;
}
and the child class contains anything specific to that project, or overwrites to check, for example, international phone numbers.
BTW, don't accept $GET or $POST variables for input, --always clean them first and check the data type. Sometimes it's easy:
$var = (int) $_GET['p'];
//or if you want to do something else with the raw value
$var = $_GET['p'];
$useVar = intval($var);
I try not to call the variable I use the same thing that would show up in a page on viewsource, because I'm paranoid and I use viewsource ALL the time. Also, investiate $_REQUEST: it's your friend.
BTW, I know there's a controller design pattern or a factory type thing, but, like I wrote before, I don't pay much attention to what they are called, b/c the names confuse me. Too much like English.
There's an old school guy out there, Tony Marston, who does lovely explanations of design patterns as well. I just visited there and he's a TON of fantastic new articles,--which he updates regularly. http://www.tonymarston.net/php-mysql/index.html
Anyway, I also wrote a bunch of classes to do things like figure out what to do with arrays, so managing the output of any kind of list is easy, but they ALSO now manag the output of form elements. I've done forms EVERY way imaginable over the last several years, from hardcoding the pages, to templates, to wrapping them in functions, to now having built a base class which sets up things like text boxes and such to classes which are used to construct multi-select check boxes (righteous!) from arrays. A child class of that then controls what they look like, or over writes the parent function when something wierd is needed. To do a form, I set up the structure using regular HTML/JavaScript, then figure out which form elements I need and drop in an array with item names. the class functions do the rest of the work.
Right now, the only problem I really have is that my hosting company still uses PHP and MySQl <5, when I've got the latest versions (well, almost) on my dev computer. Still, I've figured out how to do pretty much every I need to do with PHP 5, then back working only what needs doing with PHP 4, so for now, it's ok.
Last hint for the AM: code your variables consisently, then you can use them as "metadata" to run checks using string functions. If you decide in advance that all form variables that start with "ID_" have to be integers not equal to zero, you can run them all through the same check without caring which ID it might be.