| |||||||||||
| |||||||||||
Simple wiki demonstration
Putting it all together example from a Well House Consultants training course
More on Putting it all together [link]
Source code: jndex.php Module: H310
<?php
/* This is the main (and only) code page needed for our simple demonstration wiki, as used internally for our "todo" list. */ // Following two lines check Well House Internal "Single Login System" // So that page is only presented to staff members who are logged in. // Replace these two lines by "$staff = 1;" for a public access system, // but BE AWARE of the implications! include "$_SERVER[DOCUMENT_ROOT]/../include/stafflogin.v8"; $staff = pwtest(); /* ------------------------------------------------------------------ */ // Function to apply Bulletin Board tags to raw HTML function apply_bb_codes($raw_html) { # General formatters $cooked_html = preg_replace('/\[i\](.*?)\[\/i\]/','<em>\1</em>',$raw_html); $cooked_html = preg_replace('/\[b\](.*?)\[\/b\]/','<strong>\1</strong>',$cooked_html); $cooked_html = preg_replace('/\[u\](.*?)\[\/u\]/','<u>\1</u>',$cooked_html); # A couple of colours $cooked_html = preg_replace('/\[color=(maroon|red|purple)\](.*?)\[\/color\]/', '<span style="color: \1;">\2</span>',$cooked_html); # Links $cooked_html = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/', '<a href="\1" target="bullzeye">\2</a>',$cooked_html); $cooked_html = preg_replace('/\[url\](.*?)\[\/url\]/', '<a href="\1" target="bullzeye">\1</a>',$cooked_html); # Local Link $cooked_html = preg_replace('/\[site\](\w+?)\[\/site\]/', '<a href="\1.htm">\1</a>',$cooked_html); # Other formatters $cooked_html = preg_replace('/\[hr\]/','<hr />',$cooked_html); return $cooked_html; } // ---------- The labels we want to apply to Javascript helpers $js_helpers = array( "[bold]" => array( "[b]" , "[/b]" ) , "[underline]" => array( "[u]" , "[/u]" ) , "[italic]" => array( "[i]" , "[/i]" ) , "[maroon]" => array( "[color=maroon]" , "[/color]" ) , "[red]" => array( "[color=red]" , "[/color]" ) , "[purple]" => array( "[color=purple]" , "[/color]" ) , "[hyperlink]" => array( "[url]" , "[/url]" ) , "[locallink]" => array( "[site]" , "[/site]" ) ); // --------- Making up those Javascript helpers (if you are using this facility // you need to use the "helpers.js" file; you will find a note of the source // further down in this file function bar() { global $js_helpers; $template = <<<BUTTON <a href="javascript:void(0);" onclick="surroundText('%start%', '%end%', document.forms.update_todo.editor); return false;">%text%</a> BUTTON; $set = " • "; foreach (array_keys($js_helpers) as $button_text) { $fill = preg_replace('/%start%/',$js_helpers[$button_text][0],$template); $fill = preg_replace('/%end%/',$js_helpers[$button_text][1],$fill); $fill = preg_replace('/%text%/',$button_text,$fill); $set .= $fill . " • "; } return $set; } // -------------------------------------------------------------------------- // This demonstration is a single page editor (NOT using $_SESSION) but it does // use a template to allow the look and feel of the data it presents to the user // to be altered by a graphic artist rather than a programmer. Note that the // $fill array is set up to contain all the elements that will be needed to do // that filling in. if ($_REQUEST[pagename]) { $current_page = $_REQUEST[pagename]; } else { $current_page = "index"; } $fill[current_page] = $current_page; $fill[year] = date("Y"); $fill[company] = "Well House Consultants"; # Connect to database mysql_connect("127.0.0.1","databaseusername","databasepassword"); mysql_select_db("databasename"); # Try to create the table; it probably exists already and errors, # but we supress the error message. (I do it this way so that # I have an easy install procedure, and details of what my table # looks like within my PHP $query = "create table todo_wiki (". "wid int primary key not null auto_increment, ". "stuff longtext, ". "illustration text, ". "pagename text, ". "changed timestamp)"; @mysql_query($query); # If a submission has been made, save it! if ($_POST[editor] and $_POST[act] == "Save" and $_POST[sq] == "17") { # Must get rid of prior data for this page! $rs = mysql_query( "Select wid from todo_wiki where pagename = \"$current_page\""); $row = mysql_fetch_assoc($rs); if ($row[wid]) { $query = "delete from todo_wiki where wid = $row[wid]"; mysql_query($query); } $query = "insert into todo_wiki (pagename, illustration, stuff) values (". "\"".$current_page."\", ". "\"".htmlspecialchars(stripslashes($_POST[illus]))."\", ". "\"".addslashes(trim(stripslashes($_POST[editor])))."\"". ")"; mysql_query($query); } # Read the version that is [now] saved # Don't need to double check against injections due to magic quotes! $rs = mysql_query("select * from todo_wiki where pagename = \"$current_page\""); $row = mysql_fetch_assoc($rs); if ($row) { $stuff = $row[stuff]; if (! $staff and $_POST[edit_person] != "Admin_password") { $stuff = "This is a Well House Consultants Internal Use Page"; } $illus = $row[illustration]; $fill[picture] = $row[illustration]; $fill[updated] = $row[changed]; } else { $stuff = "Page is presently empty"; $fill[picture] = "tramm3"; $fill[updated] = "[not applicable]"; } # Just set the file content to chuck out! $fill[report] = nl2br(apply_bb_codes(htmlspecialchars($stuff))); $fill[date] = date("H:i \\o\\n l jS F Y"); # Make up edit form OR user validation request ... if ($_POST[editing] == 1 and ($_POST[edit_person] == "Admin_password" or $staff)) { $edit_area = bar() . '<br /><br />'; $edit_area .= '<textarea name="editor" rows="10" cols="60">'. htmlspecialchars($stuff). "</textarea><br />". '<input type="hidden" name="sq" value="17">'. "Picture: <input name=\"illus\" value=\"". htmlspecialchars($illus) . "\"><br />" . '<input type=submit name="act" value="Save"> '. '<input type=submit name="act" value="Cancel">'; } else { $edit_area = 'Code (if not logged in): '. '<input type="password" name="edit_person" size="10" />'. '<input type="hidden" name="editing" value="1" />'. ' and <input type=submit value="Edit!" />'; } $fill[edit_area] = $edit_area; /* Javascript to be loaded is for "surroundtext" which we have learned from SMF forum which we have at http://www.wellho.net/ask/Themes/default/script.js?fin11 and have renamed for our own internal use at helpers.js */ $html = file_get_contents("todo4.htp"); foreach (array_keys($fill) as $field) { $html = preg_replace("/%$field%/",$fill[$field],$html); } print ($html); ?> Learn about this subject
This module and example are covered on the following public courses:
* PHP Techniques * Intermediate PHP - weekend course / hobby / club / leisure users * PHP Programming * Learning to program in PHP Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Putting it all together" training module. You'll find a description of the topic and some
other closely related examples on the "Putting it all together" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |