Saturday, November 29, 2014

Advanced CSS example




1. css inherited: Set the text-color for <span> elements to blue, except those inside elements with class="extra":
span {
    color: blue;
    border: 1px solid black;
}
.extra span {
    color: inherit;
}


2. CSS group selector,  each h1, h2 and p are independent
h1, h2, p {
    text-align: center;
    color: red;
}


3. CSS class

p.test1{ color: blue; } p.test2{ color:red; }

html
<p class="test1">test1 CSS code!</p> 
<p class="test2"> p.test2 CSS code!</p>

4. space between selector
.test div{
 color: blue;
}
Select any div element that is a child of any element with a class name of "test"
<p class="test">
<div></div>
</p>


5. ">" greater sign selector - direct descendant/child
<div>
  <p><b>John 1</b></p>
  <p><b>John 2</b></p>
  <b>John 3</b>
</div>
And in CSS
div b { color: red; } /* every John is red */
div>b { color: red; } /* Only John 3 is red */

Saturday, November 22, 2014

Javascript - get unique number from time




To get unique number from time
   var d = new Date();
    var n = d.valueOf();
which will return
1416718344714
in MySQL, we need to set the data type as  BIGINT( 20 ).

In jQuery ajax post,  we can get the last insert number from autoincrement from retrun message.
But it is sometimes diffcult to syncronize this number to the div I want to update.
In some application, we had better  create a   unique number from time and force
the last insert number as this unique number in ajax post.

Thursday, November 20, 2014

jQuery - sum over columns in a table or over input box




I have a table, there  is an input box in a column, beside there is default value near input box.
There are also default values inside input box. I want to add a summary row to show total.
When the input value is changed, the total row also needs to reflect the change.
below is the code:
$(document).ready(function() {
        var $resultTable = $("<table class='list'></table>");
        var $header = $("<tr><th>#</th><th>Saving</th>");
         $resultTable.html($header);
         for(var i = 0; i< 10; i++){
             var $row = $("<tr><td >"+(i+1)+"</td><td> <input type='text' class='inputch' name='saving' size ='6' value='"+i+"'>(<label class='inputch0'>"+i+"</label>)</td> </tr>");
          $resultTable.append($row);
       }
 var $row1 = $("<tr><td>Total</td><td><td class='totalch'> 0</td></tr>");
         var total = 0;total0=0;
   
        $resultTable.find('input.inputch').each(function() {
            total += Number($(this).val());
          });
        $resultTable.find('label.inputch0').each(function() {
              total0 += Number($(this).html());

          });
          $resultTable.find('td.totalch').html(total+' ('+total0+')'); 

         $resultTable.find('input.inputch').change(function() {
           var total = 0;total0=0;
          $resultTable.find('input.inputch').each(function() {
             total += Number($(this).val());
           });
           $resultTable.find('label.inputch0').each(function() {
               total0 += Number($(this).html());
           });
             $resultTable.find('td.totalch').html(total+' ('+total0+')');
        });

});
We use change in input box, the total will be changed only after input box lose focus.
If we want to see the immediate change, we can use keyup, i.e.
         $resultTable.find('input.inputch').keyup(function() {
OR
          $resultTable.find('input.inputch').on('input', function() {

Wednesday, November 19, 2014

Userul resources and free online books for PHP, MySql, jQuery, JavaScript





Most useful resource  for PHP, MySql, jQuery, JavaScript:
http://www.w3schools.com/

PHP programming:
 http://en.wikibooks.org/wiki/PHP_Programming

Eloquent JavaScript
http://eloquentjavascript.net/

JavaScript Guide:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

Speaking JavaScript:
http://speakingjs.com/es5/

DOM
http://domenlightenment.com/

jQuery fundamental:
http://jqfundamentals.com/

Learn jQuery:
http://learn.jquery.com/

Pro Git:
http://git-scm.com/book/en/v2

Learn Git:
https://www.git-tower.com/learn/ebook

Essential SQL:
http://www.essentialsql.com/getting-started/

SQL at StandFord:
https://class.stanford.edu/courses/DB/SQL/SelfPaced/courseware/ch-sql/seq-vid-introduction_to_sql/

SQL for Web nerds:
http://philip.greenspun.com/sql/

SQL Zoo:
http://sqlzoo.net/wiki/Main_Page

HTML and CSS:
http://learn.shayhowe.com/html-css/

Friday, November 7, 2014

PHP, separate string by any space or comma, lowercaser and timestamp in PHP and MySQl



PHP, separate string by any space or comma using regular expression
    // split the phrase by any number of commas or space characters,
       // which include " ", \r, \t, \n and \f
        $course_pieces = preg_split("/[\s,]+/", trim($course));

If you use explode, you can only separate by one space:
$course_pieces = explode(" ", trim($course));

In PHP, to convert to lower case:
        $course_subject = strtolower($course_pieces[0]);
        $course_number = $course_pieces[1];


While in MySQL, we use LOWER
for example
 $oplCondition = " AND  LOWER(FirstName) LIKE  LOWER('%".$firstname))."%')";

MySQL TIMESTAMP date type, corresponding to PHP function
date("Y-m-d  H:i:s")