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))
Adding a newsfeed for your users to a multipage PHP application

As I wrote it, I realised this was turning into rather a long article, but never mind. It shows the major new components added to a "4 layer model" application I was working on the weekend before last to add in a newsfeed for logged in users to which they can contribute, together with a headlines-only display for none logged in members. The code may look longish, but it was written and tested very quickly.

If you are not familiar with the 4 layer model, start here or attend our PHP techniques workshop ... this is a more advanced article

In the particular multi page application I was modifying, page #2 is the main navigation menu, so the "finish processing stage 2" code at the top level will branch the user to many other pages in the system. Page #6 is where I added the news update:

case 2: // Navigation request made
  if ($_REQUEST[studinfo] == 1) $current = 3;
  if ($_REQUEST[studinfo] == 2) { $current = 4;
    $fill[topmsg] = "Please enter new user data"; }
  if ($_REQUEST[studinfo] == 3) $current = 6;
  if ($_REQUEST[studinfo] == 4) $xlevel = 2;
  break;


Adding data to the newsfeed is simply done by appending the data to a file, and I've checked when data is submitted that both a title and a body have been given (you'll see a comment requesting this when we come to the template form further down this example). I have used the string "::-::" as a separator / marker in the first line of each new news item in the hope that users will never add such a thing in their data ... and I'm fully aware that if I was writing something for "Joe Public" to enter data into rather that some benign and small club, this would be a bit of a hole; in such a case, I would store to a MySQL database, and within this code might have logic to delete all but the most recent 100 postings.

case 6: // Add to news feed request
  if (eregi("[a-z]",$_REQUEST[topbits]) and
    eregi("[a-z]",$_REQUEST[bottombits])) {
    $now = time();
    $fho = fopen("newsfeed.txt","a");
    fputs($fho,"$now::-::$_SESSION[username]\n");
    fputs($fho,"$_REQUEST[topbits]\n");
    fputs($fho,"$_REQUEST[bottombits]\n");
    fclose ($fho);
    }
  $current = 2;
  break;


In the 4 layer model, when you've finished handling the data submitted in the form on one page, you prepare for the next. I have added calls to a function I called newsfeed on all pages that I want to display a feed ... here is the example in the logic that prepares for a new news item, which reports a substantial amount of the old news to that the new article author can write in context. You will note that I'm putting the changing data into an array called $fill rather than generating the code directly - this is the element of the 4 layer model which allows me to separate the business logic from the look and feel - exactly what I need to do to allow for maximum future flexibility, and to allow me to pass the maintainance of code to programmers, and of the look and feel to graphic artists!

case 6: // Prepare for adding to news feed
  $fill[title] = "OurClub Adding to news feed";
  $fill[news] = newsfeed(2);
  break;


Here is the actual template ... for ease of understanding for the newcomer (i.e. to avoid ending up with a large number of files for just a demo piece) in this example, we wrote it into a "here string" ... in which we replace %-letters-% with whatever we have in the matching element of the $fill array:

$body_template[6] = <<<CLUB_TEMPLATE
<h1>Add a message to the newsfeed</h1>
Please give a headline and text of article. If you don't want to post
an article after all, just submit a blank page and it won't be added.
<form method=post>
Headline: <input name=topbits><br>
Text of article <textarea name=bottombits rows=20 cols=60></textarea><br>
When you are done ... <input type=submit></form>
<hr><b><u>The latest news feed!</u></b><br><br>%news%<br>
CLUB_TEMPLATE;


Finally, here is the 'business logic' which interprets all of the elements of the newsfeed and returns a string suitable for rendering. You'll note that even at a 'club level', I've added a number of coding / security checks - i.e. all the data the user has entered in the news goes through stripslashes and htmlspcialchars in order to render harmless any attempted injection attacks. What it does NOT do is provide any form of moderation - i.e. checking for news that breaks copyright, is libellous, adult, incites breaking of the law ... that remains a manual process.

function newsfeed($level) {
  foreach (file("newsfeed.txt") as $line) {
    $lp = explode("::-::",$line);
    if (count($lp) == 2) {
      $cur_news = $lp[0];
      $cur_repo = trim($lp[1]);
      $cur_lines = -1;
    } else {
      if ($cur_lines == -1) {
        $art_title[$cur_news] = $line;
        $art_autho[$cur_news] = $cur_repo;
        $cur_lines = 0;
      } else {
        $art_text[$cur_news] .= $line;
        $cur_lines++;
      }
    }
  } // End of feeder reader
  krsort($art_title);
  $response = "";
  $narts = 3;
  foreach (array_keys($art_title) as $tstamp) {
    $tsf = date('H:i \o\n l jS M Y',$tstamp);
    $an++;
    if ($level == 0) {
      $response = $art_title[$tstamp];
      break;
    } elseif ($level == 1) {
      $response .= "<br><b><u>$art_title[$tstamp]</u></b> by ".
        "$art_autho[$tstamp] at $tsf";
      if ($an > $narts) continue;
      $response .= "<br><br>";
      $response .= nl2br(htmlspecialchars(stripslashes("$art_text[$tstamp]")));
      if ($an > $narts * 3) break;
    } elseif ($level == 2) {
      $response .= "<br><b><u>$art_title[$tstamp]</u></b> by ".
        "$art_autho[$tstamp] at $tsf<br><br>";
      $response .= nl2br(htmlspecialchars(stripslashes("$art_text[$tstamp]")));
      if ($an > $narts * 3) break;
    }
  }
  if ($level == 1) {
    $response .= "<br><a href=\"?studinfo=4\">show more</a>";
    }
  return $response;
}



(written 2009-06-06, updated 2009-06-07)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [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)
  [4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [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)
  [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)


Back to
Melksham Traders - where do we go?
Previous and next
or
Horse's mouth home
Forward to
A (biased?) comparison of PHP courses in the UK
Some other Articles
How important is a front page ranking on a search engine?
Trowbridge - a missed opportunity? Melksham - into the breach?
CSS Style Diagrams - working out where attributes come from
A (biased?) comparison of PHP courses in the UK
Adding a newsfeed for your users to a multipage PHP application
Melksham Traders - where do we go?
Configuring httpd, or Tomcat, to run CGI scripts in Perl
Multiple web applications under Tomcat - what are the options?
Enjoying the summer weather
Past Delegate Offer - Summer Holiday / Weekend Break
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.info/mouth/2221_Add ... ation.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb