Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Improving the structure of your early PHP programs

When you first coded in PHP, you probably wrote a different script to handle each form in a series - it's the natural way when you're early in the learning process, but it can lead to repeated code that's hard to follow, and some really horrid complicated conditionals.

On Saturday and Sunday, I demonstrated the restructuring of a "classic" series of scripts into a more robust, more efficient, and more maintainable piece of code - all in a single application (and in a single file in this case). Duplicate code was removed, variables in the session simplified, and by using a "current page number" scheme, a whole host of complex conditionals got shrunk out of existence. I can't publish the final code (as the code on which it was based, thus my derivative, isn't my copyright) but I can talk about it ... and I can point you to a sample of a similar structure - its here

Plan out your application. If you want a system into which people have to log in, or you don't want then to start halfway through, you'll write a single piece of code. Start off your design with a state diagram, showing (in circles) each of the pages that the user may see, then linking them with lines which represent the actual program chunks (your PHP isn't the stuff in the circles - it's the lines BETWEEN them!)


Write the code all in a single top level file - i.e. at a single URL. The code will have a $_SESSION variable which holds the current page so that it knows what to run, and each time the page is called up the actions performed will be:
• a) Validate form inputs / finish up from previous page
• b) Prepare for next page
• c) Complete the template for the next page, send it out

With a small demonstration such as the one we did over the weekend, all the code and even the template can go in a single file - however, as the application grows and as you want to reuse code in other applications you'll want to split it into four files / logical groups:
• 1. The Template for the response page (easily edited with Dreamweaver or similar)
• 2. Web helper routines to help with generation of sticky forms, handling user inputs which include awkward characters such as ' " and <, etc
• 3. "Business" or program logic which relates to the application and calculations and validations that it needs
• 4. Top level code - i.e. the two switch statements that control the whole program's operation.

With a still larger system (let's say you get up to 20 or 30 pages!), you can then load business logic from multiple files, using a 'just in time' technique which will be efficient, rather that loading all the code for every page each time.


Although a 'normal' computer program runs from top to bottom, a web application written as I have described above will run a number of times ... and the way it is structure means that some of the code that's further down the file will be run before code that's higher up, but on a later page. The note in green on the board says "Confusing" - which it might be for the first half hour. Once you're used to it, it's great.



(written 2009-05-25, updated 2009-05-26)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3919] What is a web framework? - (2012-11-10)
  [4010] Really Simple Rails - (2013-02-17)
  [4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
  [4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)

H302 - PHP - MVC, 4 layer model and templating
  [1634] Kiss and Book - (2008-05-07)
  [1716] Larger applications in PHP - (2008-07-22)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [2174] Application design in PHP - multiple step processes - (2009-05-11)
  [2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
  [3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
  [3956] Zend / layout of MVC and other files in an example application (PHP) - (2012-12-16)
  [4314] PHP training - refreshed modern course, backed up by years of practical experience - (2014-11-16)

H115 - Designing PHP-Based Solutions: Best Practice
  [123] Short underground journeys and a PHP book - (2004-11-19)
  [237] Crossfertilisation, PHP to Python - (2005-03-06)
  [261] Putting a form online - (2005-03-29)
  [340] Code and code maintainance efficiency - (2005-06-08)
  [394] A year on - should we offer certified PHP courses - (2005-07-28)
  [426] Robust checking of data entered by users - (2005-08-27)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [572] Giving the researcher power over database analysis - (2006-01-22)
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [945] Code quality counts - (2006-11-26)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [1052] Learning to write secure, maintainable PHP - (2007-01-25)
  [1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [1182] Painting a masterpiece in PHP - (2007-05-10)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1321] Resetting session based tests in PHP - (2007-08-26)
  [1323] Easy handling of errors in PHP - (2007-08-27)
  [1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1482] A story about benchmarking PHP - (2007-12-23)
  [1487] Efficient PHP applications - framework and example - (2007-12-28)
  [1490] Software to record day to day events and keep an action list - (2007-12-31)
  [1533] Short and sweet and sticky - PHP form input - (2008-02-06)
  [1623] PHP Techniques - a workshop - (2008-04-26)
  [1694] Defensive coding techniques in PHP? - (2008-07-02)
  [1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
  [2430] Not just a PHP program - a good web application - (2009-09-29)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
  [3820] PHP sessions - a best practice teaching example - (2012-07-27)
  [3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
  [4069] Even early on, separate out your program from your HTML! - (2013-04-25)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)


Back to
On the road, at a Service area thats a cut above the others
Previous and next
or
Horse's mouth home
Forward to
England, Scotland, Northern Ireland
Some other Articles
Always use su with minus. And where do programs come from?
Bright Colours
Running straight from the jar, but not from a tar
England, Scotland, Northern Ireland
Improving the structure of your early PHP programs
On the road, at a Service area thats a cut above the others
New Example - cacheing results in PHP for faster loading
A two day gap - first time in five years?
Melksham Business Map - Chamber of Commerce, Tourist Information and Town Council
Excellent product, excruciating customer service. 3 Mobile Broadband
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2199_Imp ... grams.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb