Thursday, March 14, 2019

Using JS Chosen library for multiselect dropdown


JS Chosen library
https://harvesthq.github.io/chosen/

Example:
https://stackoverflow.com/questions/30190588/html-select-multiple-as-dropdown

JS and CSS used:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.jquery.min.js"></script>
<link href="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.min.css" rel="stylesheet"/>

Initialization
        $(".chosen-select").chosen({
          no_results_text: "Oops, nothing found!"
        })   

Set default value

        var str_array = "9999,90";
        var dataarray=str_array.split(",");       
        $("select[name='department1']").val(dataarray).trigger("chosen:updated");   


php post
 $.ajax({
        type: "POST",
            //$('select[name="Status"]').val() is cfp_appt_status
        data: {'department1': $('select[name="department1"]').val()},
        async: true,   
   
...........................
 Process data example:
        $pad_id = $this->input->post('department1');
        $con =1;
        foreach ($pad_id as $pad_id0)
            {
                    if($pad_id0=='9999') $con =0;
                    if($pad_id0==$entry['pad_id']) $con =0;
            }
            if($con==1) continue;

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