Saturday, July 26, 2014

Passing parameters in visible function in data-bind in knockout.js




Passing parameters in visible function in data-bind in knockout.js
 HTML code:
<div class="editor-field">
    <select data-bind="options: propertyTypeList, value: selectedPropertyType, optionsCaption: 'select property type...'"></select>
</div>
<div class="editor-label" data-bind="visible: isVisible('Area')">Area</div>
<div class="editor-label" data-bind="visible: isVisible('Bedroom')">Bedroom</div>


JS code:
function ViewModel() {
    var self = this;
    self.propertyTypeList = ko.observableArray(['Area', 'Bedroom', 'Other']);
    self.selectedPropertyType = ko.observable("");
    self.visibleFeatures = ko.observableArray([]);
    self.isVisible = function (type) {
        //Your logic here
        return self.selectedPropertyType() === type;
    };
};

ko.applyBindings(new ViewModel());

Friday, July 25, 2014

select element in options with different values from text using Knockout.JS




JS library Knockout  is based on MVC model:
http://knockoutjs.com/
To  select element in options with different values then text using Knockout.JS,
JS side:
    self.StudyLeaveValue1([{ value: 1, name: 'France' },    { value: 2, name: 'Germany' },
           { value: 3, name: 'Spain' }];


HTML side:
 <select data-bind="options: StudyLeaveValue1,optionsText: 'name', optionsValue: 'value',optionsCaption: 'Please choose an option'"></select>
Source code of above from inspector:

<select data-bind="options: StudyLeaveValue1,optionsText: 'name', optionsValue: 'value',optionsCaption: 'Please choose an option'">
<option value="">Please choose an option</option>
<option value="1">France</option>
<option value="2">Germany</option>
<option value="3">Spain</option>
</select>

Sunday, July 20, 2014

Compare difference of two folders in Linux




Compare difference of two folders in Linux, for example test and test.old directories, using -rq in diff

diff  -rq  test test.old

change permission of folder and its sub folders, for example test folder, using -R in chmod:

chmod  -R  755 test

Thursday, July 17, 2014

check duplication of a column value in MySQL




To find  the duplicate username in users table in MySQL:
 SELECT username, COUNT(*) c
FROM `users`
GROUP BY username

HAVING c> 1

 To get more detail information
SELECT firstname, lastname, username
FROM `users`
WHERE username IN

 (
 SELECT username
FROM `users`
GROUP BY username

HAVING COUNT(*) > 1
)


Wednesday, July 16, 2014

select distinct for composite key in MySQL using GROUP BY




For example,we have users table and containing firstname and lastname.
We can select distinct lastname using:
SELECT DISTINCT lastname FROM users
What happen if we want to see distinct of combination of firstname and lastname?
We can use GROUP BY as following
SELECT lastname, firstname  FROM users GROUP BY lastname, firstname

Monday, July 14, 2014

Get first 4 strings of the field in MySQL




I have a MySQL table  fmcontract_sims_courses, and I only want to select the first 4 strings of the field
 CourseDescription_original:
SELECT SUBSTRING(CourseDescription_original, 1, 4) FROM fmcontract_sims_courses

Copy this value to the column Section in  table fmcontract_course_assignment:
UPDATE fmcontract_course_assignment as a,  fmcontract_sims_courses as b  SET a.Section =
SUBSTRING(b.CourseDescription_original, 1, 4)  WHERE a.CRSEID = b.CourseKey

MySQL break string into two parts





I have a field CRSE containing information "COGS 101" in table
fmcontract_course_assignment
 I want to break the field in to two parts  based on space and put them into new two fields:
 CourseSubject, CourseNumber.
1) First I create  new two fields:
ADD `CourseSubject` VARCHAR(50) NULL DEFAULT NULL , ADD `CourseNumber` VARCHAR(16) NULL DEFAULT NULL ;

2) Update these two news field from  split of filed CRSE:

UPDATE `fmcontract_course_assignment`  SET
    `CourseSubject` = IF(
        LOCATE(' ', `CRSE`) > 0,
        SUBSTRING(`CRSE`, 1, LOCATE(' ', `CRSE`) - 1),
        `CRSE`
    ),
    `CourseNumber` = IF(
        LOCATE(' ', `CRSE`) > 0,
        SUBSTRING(`CRSE`, LOCATE(' ', `CRSE`) + 1),
        NULL
    );
 Note:
  • CRSE values without a space: it will add the whole string to memberfirst and sets CourseNumber  to NULL.
  • CRSE values that have multiple spaces: it will add everything before the first space to CourseSubject  and the remainder (including additional spaces) to CourseNumber.

Tuesday, July 8, 2014

Example of control flow to select and update in MySQL




In bus_leaves table, I have columns
LeaveStartDate and StartTerm
StartTerm has four digits, the first digit is 1, the second and third digit are from last two digits of year,
 the forth digit (last)  is equal to 1for moth between 1-4, 4 for month between 5-8, 7 for month between 9-12.
 LeaveStartDate is date type and has format 2013-05-01
I want to convert  LeaveStartDate to StartTerm
1. First using SELECT to see convert result:
SELECT CASE WHEN MONTH(DATE(LeaveStartDate))>= 1 AND MONTH(DATE(LeaveStartDate))<=4 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 1 WHEN MONTH(DATE(LeaveStartDate))>= 5 AND MONTH(DATE(LeaveStartDate))<=8 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 4 WHEN MONTH(DATE(LeaveStartDate))>= 9 AND MONTH(DATE(LeaveStartDate))<=12 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 7 END AS SenateEffectiveTerm FROM bus_leaves LIMIT 0, 25 

2. Update StarTerm:
 UPDATE  bus_leaves SET  StartTerm =CASE WHEN MONTH(DATE(LeaveStartDate))>= 1 AND MONTH(DATE(LeaveStartDate))<=4 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 1 WHEN MONTH(DATE(LeaveStartDate))>= 5 AND MONTH(DATE(LeaveStartDate))<=8 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 4 WHEN MONTH(DATE(LeaveStartDate))>= 9 AND MONTH(DATE(LeaveStartDate))<=12 THEN (YEAR(DATE(LeaveStartDate)) - 1900) * 10 + 7 END


Sunday, July 6, 2014

Hide and show a form in a link in jQuery



In jQuery to toggle a form from alink:
< !DOCTYPE html>
<html>
<head>
<script
src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript">
</script>
<script>
function revealnewpersondetails()
{
     $('#newpersondetails').toggle();
 }
</script>
</head>
<body>
<h1>Hide and show a form in a link in  JavaScript</h1>
 <a href="javascript:revealnewpersondetails()">To add a new person, please fill in his/her details</a>.
  <p></p>
 
  <div id="newpersondetails" style="margin: 5px 5px 5px 20px; padding: 1px;display:none ">
  <label style="display: block; float: left;">Surname(<font color="red">*</font>): <br>
    <input name="lastname" value="" size="8" maxlength="128" type="text"></label>
  <label style="display: block; float: left;">First name(<font color="red">*</font>): <br>
    <input name="firstname" value="" size="8" maxlength="128" type="text"></label>
  <label style="display: block; float: left;">Position <br>
    <input name="honorifics" value="" size="7" maxlength="128" type="text"></label>
  <label style="display: block; float: left;">Location: <br>
    <input name="room" value="" type="text"></label>
 <label style="display: block; float: left;">Email: <br>
    <input name="email" value="" size="17" maxlength="128" type="text"></label>
  <label style="display: block; float: left;">Home division: <br>
     <select name="deptid">
     <option value="1">Cardiac-Surgery</option><option value="2"> General-Surgery</option><option value="3">Neurosurgery</option><option value="4">Otolaryngology Head and Neck Surgery</option><option value="5">Pediatric-Surgery</option><option value="6">Plastic-Surgery</option><option value="7">Radiation Oncology and Developmental Radiotherapeutics</option><option value="8">Thoracic-Surgery</option><option value="9">Vascular-Surgery</option><option value="10">Others</option>     </select>
    </label>
    <input value="Add new person" style="display: block;clear:both;" type="submit">
    <input name="action" value="newperson" type="hidden">
   </div>
</body>
</html>



Thursday, July 3, 2014

Change row background color alternatively in CSS or jQuery




To Change row background c0lor alternatively in jQuery:
<script>
$(document).ready(function()
{
  $("tr:even").css("background-color", "lightblue");
});
</script>

In CSS
<style type="text/css">
 tr:nth-child(2n) { background-color: #FFEBCD; }
 </style>

Add an associative array in MySQL result row - PHP





I extract each row from MySQL in PHP, now I want to add an  associative array in each row.
PHP function  array_merge is used.
    public function get_salary() {
        $c = new Connection();

       $items = array("StartTerm" => 0,"EndTerm" => 1000, "sort"=>0, "EmploymentGroup"=>0 );
       while($row = $c->getArray($result)){

// add associate array $row and $items and return it to $results
            $results[] = array_merge($row, $items);    
               }   
        return $results;
     }   

part of code in models in PHP CodeIgniter