Friday, December 7, 2018

Design search in a manual in PHP





Design search in a manual in PHP
1) Instead of search in MySQL database
such as
SELECT * FROM tutorial WHERE MATCH(title,description) AGAINST ('left right' IN NATURAL LANGUAGE MODE);
I design search algorithm in PHP
2) Search box
     <form method="post" action="">
     <input class="" name="search" type="text" placeholder=""
     value="<?php echo htmlspecialchars($_POST["search"]); ?>" />&nbsp;&nbsp;<input id="Search_button" value="Search" type="submit">
     </form>

3) Replace search word with highlight background red text white , search title and content
    $pattern = htmlspecialchars($_POST["search"]);
    $replacement = '<span style="background-color:red;color:white;">'.$pattern.'</span>';
    $entry['title'] = eregi_replace($pattern, $replacement, $entry['title']);   

4) Using function  context_find($haystack, $needle, $context)
to  show searching Word with 10 surrounding Words in first occurance, add link "Read More"
add '&search='.$_POST["search"] in link. When the link is clicked, highlight of search word shown in the text.

5) Example code index.php
        <div id="userGuide_list">
            <ul class="paginatedList nav nav-list instlist" style='padding:10px 5px;' >
                <?php
                    $j=0;       
                    foreach($categories as $row){
                        $cat_each = '<li class="'.$row['NameAbb'].'"><h5 class="strong section">'.$row['Name'].'</h5><ul class="sub-list">';
                        $category = $UserManualController->loadTableOfContents($row['NameAbb']);
                        $item = "";
                        $i==0;
                        $extra = "";
                        foreach($category as $entry){
                            if(trim($_POST["search"])!=""){
                                $pattern = htmlspecialchars($_POST["search"]);
                                if (strpos($entry['title'], $pattern) !== false) $i= $i + 1;
                                $replacement = '<span style="background-color:red;color:white;">'.$pattern.'</span>';
                                $entry['title'] = eregi_replace($pattern, $replacement, $entry['title']);
                                $loadContent = $UserManualController->loadContent($entry['content_id']);
                                $extra = $UserManualController->context_find($loadContent['content'], $pattern, 10);
                                $extra = eregi_replace($pattern, $replacement, strip_tags($extra));
                                if(!empty($extra))
                                $extra ='<div>...  '.$extra.'  ...<a   href="'.SITE_BASE_URL.'/TRACSManual/tracs-manual-content.php?id='.$entry['content_id'].'&search='.$_POST["search"].'"> Read More ...</a></div>';
                            }
                       
                            if(trim($_POST["search"])=="" || strpos($entry['title'], $pattern) !== false|| strpos($loadContent['content'], $pattern) !== false)
                            $item  .= '<li><a   href="'.SITE_BASE_URL.'/TRACSManual/tracs-manual-content.php?id='.$entry['content_id'].'&search='.$_POST["search"].'">'.$entry['title'].'</a>'.$extra;
                        }
                        $item  .= '</ul></li>';
                        if(trim($_POST["search"])=="" || $item  != '</ul></li>' ||$extra!="" ){
                          echo $cat_each.$item;
                          $j=$j+1;
                        } 
                       
                    }
                    if($j==0) echo "No match found.";
                ?>
            </ul>
        </div>

6. Example code of search ten words
(https://stackoverflow.com/questions/22762797/php-show-searching-word-with-5-surrounding-words)
    function context_find($haystack, $needle, $context) {
       
        $haystack=' '.$haystack.' ';
        if ($i=strpos($haystack, $needle)) {
            $start=$i;
            $end=$i;
            $spaces=0;

            while ($spaces < ((int) $context/2) && $start > 0) {
                $start--;
                if (substr($haystack, $start, 1) == ' ') {
                    $spaces++;
                }
            }

            while ($spaces < ($context +1) && $end < strlen($haystack)) {
                $end++;
                if (substr($haystack,$end,1) == ' ') {
                    $spaces++;
                }
            }

            while ($spaces < ($context +1) && $start > 0) {
                $start--;
                if (substr($haystack, $start, 1) == ' ') {
                    $spaces++;
                }
            }

            return(trim(substr($haystack, $start, ($end - $start))));
        } else {
            return false;
        }
    }            

Thursday, November 22, 2018

PHP setting for PHP5.3 and PHP 7 in Ubuntu



1) check php version
 php -v
 PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

 with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies

Found PHP is updated from 5.3 to 7.

need to re-config  /etc/php/7.0/apache2/php.ini
 1) Change
 short_open_tag = Off
to
short_open_tag = On

2) restart server
 sudo service apache2 restart

MySQl, Error Number: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column



 MySQL version is  updatde from 5.5.36 to  5.7.24 in Ubuntu,
when I run MySQL script, the following message showed
"MySQl, Error Number: 1055  Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column"

As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY which means when you are grouping rows and then selecting
something out of that groups,
you need to explicitly say which row should that selection be made from.
To turn off warning message
1. In file  /etc/mysql/my.cnf
 add
 sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
 under
 [mysqld]
2. restart mysql server in
sudo service mysql restart Ubuntu


Reference:
https://stackoverflow.com/questions/34115174/error-related-to-only-full-group-by-when-executing-a-query-in-mysql

Install the PHP module curl Ubuntu




I have following error messge for my web server in  Ubuntu after CAS server upgrade
"You need to install the PHP module curl to be able to use CAS authentication."
The following steps is to install the PHP module curl at Ubuntu:
1)  Install CURL
sudo apt-get install curl
2) restart Apache
sudo service apache2 restart
3)  Install PHP CURL
sudo apt-get install php-curl
( note: do not run sudo apt-get install php5-curl
it will return message:
"Package 'php5-curl' has no installation candidate"
In Ubuntu 16.04 default PHP version is 7.0, if you want to use different version then you need to install
 PHP package according to PHP version:

    PHP 7.2: sudo apt-get install php7.2-curl
    PHP 7.1: sudo apt-get install php7.1-curl
    PHP 7.0: sudo apt-get install php7.0-curl
    PHP 5.6: sudo apt-get install php5.6-curl
    PHP 5.5: sudo apt-get install php5.5-curl
   
    or just run sudo apt-get install php-curl
)
4) restart Apache
sudo service apache2 restart   

Another notes:
remove php 5 package
a) check php5 package
dpkg -l | grep php5
dpkg --purge --force-all php5-curl
b) remove
sudo dpkg --purge --force-all php5-curl
sudo apt-get remove php5-*
sudo apt-get purge php5-*
sudo apt-get autoremove
dpkg -l | grep php5
c) check unix server OS and version
 uname -a
Linux facts2 4.4.0-139-generic #165-Ubuntu SMP Wed Oct 24 10:58:50 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
d) du -k
check space used
f) df -k
check space available
 

Wednesday, November 21, 2018

JS: reload current page and reload parent window



Current page  is opened in parent window through the link
" <a target=”_blank” href='SITASwapSection.php'> HERE</a>"
To reload current page is JS:
location.reload();

To  reload parent window
window.opener.location.reload();

More codes:
if(confirm('Are you sure to swap '+section1 + ' ' +stakeholder_name1+' with ' +section2 + ' ' +stakeholder_name2 +' in '+course+'? Click OK to proceed. Click Cancel to cancel')) {           
        $.post("../_ajaxParts/SITAAssign/SITAAssignPOST.php",  {funct:'updateSITAswap',
                                courses_offered_id1:courses_offered_id1,
                                stakeholder_id1:stakeholder_id1,
                                section1:section1,
                                assignedID1:assignedID1,
                                courses_offered_id2:courses_offered_id2,
                                stakeholder_id2:stakeholder_id2,
                                section2:section2,
                                assignedID2:assignedID2
                               
                        },  function(data){
                          formatJson(data,"div#notice", function(json){
                           
                               if(json.data['success']==1){
                               
                                 location.reload();
                                 window.opener.location.reload();

                                 formatJson('{"success":"Swap sections successfully."}', "div#notice", null);
                               }else if(json.data['success']==-1){
                                  alert("Can not swap due to different ranks");
                               }else if(json.data['success']==-2){
                                  alert("Can not swap due to empty contract");                                           
                               }else{
                                 formatJson(json.data['error'], "div#notice", null);
                               }
                            });

        });

       
}


 

Friday, November 16, 2018

Watch Halloween firework from balcony Richmond BC Oct 31 2018



I stayed at home but still have a nice view watching Halloween firework from balcony at Richmond BC on Oct 31 2018.

Thursday, March 15, 2018

Update PHP version from 5.5.11 to 5.5.38 due to security in Windows Server 2012



In PHPMyAdmin, my PHP version is 5.5.11 and needs to update  to 5.5.38 due to security issue in Windows Server 2012
First stop IIS web services. In Windows Server 2012
 Administrative tool->Service
stop
World Wide Web Publishing Service

1. Open the Web PI application from the following location on your filesystem.
C:\Program Files\Microsoft\Web Platform Installer\WebPlatformInstaller.exe
2. Go to Products
3. Search PHP,
4. Click PHP 5.5.38. click Add
5. Click Install
6. restart

World Wide Web Publishing Service

Tuesday, February 13, 2018

Notepad++ - Add C++ compiler





 1) One way to add  C++ compiler  is to use Pocket C++.
Pocket C++ can be downloaded from:
https://github.com/dacap/pocketcpp
After installation, You can use F9 key to compile C++ files, and Ctrl+F9 to execute the compiled program.
Recently I found there is popup windows to block
me compile C++ code.
2) second way is to install g++ compiler separately and
integrate in Notepad++.

a) Install g++ using Cygwin (http://www.edparrish.net/common/cygwin.php)
or using MinGW (https://nuwen.net/mingw.html)

b) Open the Plugin Manager in Notepad++.

Plugins > Plugin Manger > Show Plugin Manager

c) Find the NppExec plugin in the list and install it.

    Check the box
    Press the Install button

d) Open the NppExec Execute dialog.

NppExec > Execute...

e) Copy and paste the following script into the Commands box.
(assume g++ installed in C:\cygwin\bin\g++.exe. If using MinGW, replacing with MinGW g++ directory)

SET G++ = C:\cygwin\bin\g++.exe
NPP_SAVE // save current file
cd $(CURRENT_DIRECTORY) // go to directory of the current file
"$(G++)" -Wall -Wextra -Wpedantic -std=c++11 -o "$(NAME_PART)" "$(FILE_NAME)"


f) Save the script with a name like: C++ compile.

    Press Save button
    Enter the script name
    Press the Save button
   
g)To only compile, press the keys Ctrl-6 together or use the following set of menu commands:

 To both compile and run a program, press the keys Ctrl-7 together or use the following set of menu commands:

    Follow the menu to the NppExec Execute dialog

    Plugins > NppExec > Execute

    Select C++ compile and run script from the dropdown list.
    Press the OK button.
   
References:
https://github.com/dacap/pocketcpp
https://nuwen.net/mingw.html
http://www.edparrish.net/common/cygwin.php
http://www.edparrish.net/common/npp4c.html
https://stackoverflow.com/questions/27980134/how-to-compile-execute-c-code-from-within-notepad
http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/