Wednesday, December 31, 2014

knockout.js video tutorial




knockout.js
main site: http://knockoutjs.com
tutoril: http://learn.knockjs.com
blog: http://knockmeout.net

knockout.js video tutorial

Thursday, December 18, 2014

jQuery, combine different functions together




For example I have input button
<input type='button' class='previewButton' value='preview' />
I have general function for input method
    $resultTable.find('input').change(function() {
}
But change is not applied to button, as button reacts to click. To call change function for button:
   $resultTable.find('input.previewButton').click(function(event) {
              $(this).change();

}

Wednesday, December 17, 2014

Special characters pronunciation link




 Special characters pronunciation:
http://catb.org/jargon/html/A/ASCII.html

Monday, December 15, 2014

MySQL, left join only first row




MySQL, left join only first row:
                LEFT JOIN     fmcontract_rate_table AS rt
                    ON rt.ApptCategory =(SELECT ApptCategory  FROM fmcontract_rate_table WHERE
                    ApptCategory=hasRanks.Rank AND offered.Term >=StartTerm 

                   AND offered.Term  <EndTerm
                    ORDER BY rt.StartTerm LIMIT 1
                    )

Friday, December 12, 2014

JavaScript: test an empty string




To test an empty string  a in JS:
if (a == null || a=='')
Better way:
if (!a)
Because in javascript, an empty string, and null, both evaluate to false in a boolean context.

Thursday, December 11, 2014

jQuery, selct and click radio button



For radio buttons in HTML
     <input type="radio" name="test" id="test1" />test1<br/>
     <input type="radio" name="test" id="test2" />test2<br/>
     <input type="radio" name="test" id="test3" />test3</br>
To get selected value:
$('input:radio[name=test]:checked').val();
 
To perform an action when click the radio button

$(function(){

$('input:radio[name=test]').change(
    function(){
        alert('changed');   
    }
);          

});
To set the first button checked:
$('input:radio[name=test]:nth(0)').attr('checked',true);
or
$('input:radio[name=test]')[0].checked = true;

JavaScript, convert object to number, replace part of string, trim




In JavaScript, Number function is used to  convert different object values to their numbers:

For example
var x4 = "9";
var x5= "9";
alert(x4+x5);
alert(Number(x4)+Number(x5);

 alert(x4+x5) produces 99, while alert(Number(x4)+Number(x5) produce 18.

In JS, to replace part of string
var str = "Visit Vancouver!";
var res = str.replace("Vancouver", "Seattle");


This just replace first occurrence of  Vancouver, to replace all Vancouver to Seattle
var res = str.replace(/Vancouver/g, "Seattle");


JS function trim will remove whitespace from both sides of a string:
Example:
var str = "       Hello World!        ";
alert(str.trim());



HTML choose br, br/, br / or /br in line break?





HTML choose <br>, <br/>, <br /> or </br> in line break?
In HTML (up to HTML 4): use <br>
In HTML 5: <br> is preferred, but <br/> and <br /> is also acceptable
In XHTML: <br /> is preferred. Can also use <br/> or <br></br>
Notes:
  • <br></br> is not valid in HTML 5, it will be thought of as two line breaks.
  • XHTML is case sensitive, HTML is not case sensitive.
  • For backward compatibility, some old browsers would parse XHTML as HTML and fail on <br/> but not <br />

Diffferece between span and label in HTML




What is difference between label and spna in HTML? for example:
<span id="myid"></span>
or
<label id="myid"></label>

The <span> tag is used to group inline-elements in a document.  such as
<p>This car is  <span style="color:blue">blue</span> </p>
 
 A label is used in combination with a form input element. In some browsers the label can be clicked to activate the input element.
 

Span

The <span> tag is used to group inline-elements in a document.
The <span> tag provides no visual change by itself.
The <span> tag provides a way to add a hook to a part of a text or a part of a document.

Label

The <label> tag defines a label for an element.
The <label> element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the <label> element, it toggles the control.
The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together.
 

Wednesday, December 10, 2014

MySQL date created and date modified




In MySQL table, we often have date created, date modified, creator and modifier.
We can set  both date created and date modified type as TIMESTAMP.
For date created, we can set the default CURRENT_TIMESTAMP.  As a result, when you insert a row into the table, the current date and time are automatically inserted into the column.

For Date modified , we can (in PHP)
 SET  DateModified = NOW(),

For Creator and modifier, we can use SESSIOn from PHP such as
  Creator      = {$this->sanitizeInput($_SESSION['sms_userName'])}, 

MYSQL: merge update and insert using replace




MySQL REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

So REPLACE know when to insert a row or update a row
EXAMPLE: id is primary key. semester. stakeholder_id and courses_id are composite key.
         $c = new Connection();
  $sql = "REPLACE  contract_si_ta_percourse
                  SET semester = {$this->sanitizeInput($arr['term'])},
                               ContactHour = {$this->sanitizeInput($arr['contacthour'])},
                                       courses_id = {$this->sanitizeInput($arr['course_id'])},
                       stakeholder_id  = {$this->sanitizeInput($arr['stakeholder_id'])}
                           ";  
       $result = $c->query($sql);


If we do not use replace, and use insert and update, the code will become very complicate.

          $c = new Connection();
  $sql = "SELECT id FROM  contract_si_ta_percourse
                  WHERE semester = {$this->sanitizeInput($arr['term'])} AND
                                       courses_id = {$this->sanitizeInput($arr['course_id'])} AND
                       stakeholder_id  = {$this->sanitizeInput($arr['stakeholder_id'])}
                           ";  
       $result = $c->query($sql);

if($c->getNumRows($result) <= 0){
         $sql = "INSERT  contract_si_ta_percourse
                  SET semester = {$this->sanitizeInput($arr['term'])},
                               ContactHour = {$this->sanitizeInput($arr['contacthour'])},
                                       courses_id = {$this->sanitizeInput($arr['course_id'])},
                       stakeholder_id  = {$this->sanitizeInput($arr['stakeholder_id'])}
                   ";  

}else {
               $sql = "UPDATE  contract_si_ta_percourse
                              SET ContactHour = {$this->sanitizeInput($arr['contacthour'])}

                           WHERE
                          semester = {$this->sanitizeInput($arr['term'])} AND
                               courses_id = {$this->sanitizeInput($arr['course_id'])} AND
                                       stakeholder_id  = {$this->sanitizeInput($arr['stakeholder_id'])};

}
    $result = $c->query($sql);

Wednesday, December 3, 2014

Run codes online using ideone.com





 Ideone is an online compiler and debugging tool which allows you to compile source code and execute it online in more than 60 programming languages (Java, C++, Pyhon, PHP, JavaScript  and more)
http://ideone.com/


fixed iphone 5s can not slide to power off




My iphone 5s screen was frozen. I hold the power button for a few seconds  and screen
showed "slide to power off". But I can not slide!!
There is another solution without slide to power off:
Reboot (holding the power and home button simultaneously)