This is a discussion on Cleaning up my messy code within the PHP General forums, part of the PHP Programming Forums category; Here is what I would do (this advice and a buck will buy you a cup of coffee): First, I'...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Here is what I would do (this advice and a buck will buy you a cup of
coffee): First, I'd take my existing code, look over it and ask, Where am I doing the same thing over and over? Code that is repetitive is a prime candidate for using functions or objects. Objects are easier to maintain for me. So if your making code on each page to access a MySql database, you can then decide to make a class to access your database or take a look at some of the existing classes like PEAR:DB or ADODB that do this for you. Then I'd look at a template system or use XSLT and CSS (my preference - ultimate flexibility - WML, RSS,etc...). So I made a class to return an xml string from a database query (<record><fieldname>fieldvalue</fieldname></record>). Then each page of the site becomes a matter of reading an xslt file into a variable, sending a query and returning an xml string and transforming the xml into html The advantage is each page of the site has code that looks like this: <?php // includes require('../includes/clsmakexml.php'); require('../includes/clsReadFile.php'); // sql query $sql .= " SELECT CS.FirstName, CS.LastName, CS.Firm, CS.CustomerID, CS.Bio, "; $sql .= "CI.City, CI.State, CI.ZipCode, CI.PhoneNum, CI.FaxNum, Ci.email, CI.url "; $sql .= "\rFROM Customer CS "; $sql .= "INNER JOIN ContactInfo CI ON CS.CustomerID = CI.CustomerID "; $sql .= "\rWHERE CS.CustomerID = " . $_GET['CustomerID']; // convert query to xml string via makexml class $xmldoc = new makexml("mssql"); $xmldoc->xmlstart('attys'); $xmldoc->xmlmaker($sql); $xmldoc->xmlEnd(); // read xsl template file via readFile clss $fileR = new readFile(); $fileR->processFile("../includes/header.inc"); $fileR->processFile("bio.xsl"); $fileR->processFile("../includes/footer.inc"); // prepare xsl transformation $xmldoc->xslstr = $fileR->contents; $xmldoc->xslresult = 'result.xml'; $arguments = array('/_xml' => $xmldoc->xmlstr,'/_xsl' => $xmldoc->xslstr); // transform the xml into html $xh = xslt_create(); $result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments); if ($result) { print $result; } else { print "Sorry, the xml could not be transformed by the xsl into"; print " the \$result variable the reason is that " . xslt_error($xh) .. print " and the error code is " . xslt_errno($xh); } xslt_free($xh); ?> This isn't bulletproof but makes code easy to read and maintain for me. If I need to add a new feature, I just add a new function, method, or class. If I need something different at the presentation level I just change the XSLT or , more likely, the CSS. On Monday, September 29, 2003, at 02:22 PM, Chris wrote: > I am working on a fairly large scale (for myself anyway) project using > PHP > and MySQL. I am victim of teaching myself to program, not separating > presentation from my code-- all the things that lead to masses of > spaghetti > code so atrocious even I can't figure out what I was doing an hour ago. > > I'm not looking for an IDE or code generator so much as some practical > advice for organization and framework when developing a larger app. I > know > of PHP Fusebox, having programmed with Cold Fusion fusebox for a > while, but > it seems like that might be too much. Maybe I just need a sensical, > practical approach to application layout. What do you all do? How can I > learn to be a better PHP programmer in this regard? > > c > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > |