Context help in your web applications

Categories ASP.NET, PHP

This article explains how you can implement context help in your PHPRunner or ASPRunner.NET application. Maybe you need to display some hints for users or a link to a more detailed page in the manual. If you ever checked PHPRunner or ASPRunner.NET live demo you probably noticed how it looks there:

We, of course, do not want to add those messages manually to every page. Instead, we will store all those messages in the database and display the relevant message based on what page we currently on.

1. Table to store messages

The following script is for MySQL:

CREATE TABLE `messages` (
  `id` int(11) NOT NULL,
  `pageName` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL,
  `pageType` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL,
  `value` text COLLATE utf8mb4_bin NOT NULL,
  `project` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

ALTER TABLE `messages` ADD PRIMARY KEY (`id`);
ALTER TABLE `messages` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Here is how sample data in the messages table looks.

2. AfterAppInit event.

$page = preg_replace('/(\/|\.php)/i', '', substr($_SERVER["PHP_SELF"], 
   strripos($_SERVER["PHP_SELF"],"/")) );
$sym_pos = strripos($page,"_");
$selectData["pageName"] = $sym_pos?substr($page,0,$sym_pos):$page;
$selectData["pageType"] = $sym_pos?substr($page,$sym_pos+1):"";
$selectData["project"] = 1;
$record = DB::Select("messages", $selectData )->fetchAssoc();
$_SESSION["messages"] = "";
if( $record ) $_SESSION["messages"] = $record["value"];

Here we parse the current page URL to find the table name and page type ("list", "add" etc). We could have just stored the file name like "customers_list.php" but we'd like to use the same table for PHPRunner and ASPRunner.NET and ASPRunnerPro demos so we doing it this way.

The project field is not required if you only have a single project. We have multiple live demos so the following tells that we are working with Liev Demo 1.

$selectData["project"] = 1;

Once the message is retrieved we store it in the session variable $_SESSION["messages"] so we can later use it anywhere on the page.

3. Displaying the message

The easiest option is just to display it in the header. This is what we do in those live demos. Another option is to display it from the code snippet.

<?php 
print $_SESSION["messages"];
?>

4. Sample message HTML code

Here is HTML of the hint that is seen on the screenshot.

<DIV class="col-md-8 col-md-offset-2 alert alert-success info big-info">
<DIV class="col-md-6">
<ul>
<li>Preview with products</li>
<li>Columns grid layout on the List page</li>
</ul>
</DIV>
<DIV class="col-md-6">
<ul>
<li>Custom template - without search panel</li>
<li>Highlighting search results</li>
</ul>
</DIV>
</DIV>

You can do something else, i.e. display a link to the page in the manual.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *