Monday, June 30, 2014

HTML5 add new data attribute




The data attribute in HTML5  must be prefixed with 'data-'. It should not contain any uppercase letters.

To access the data attribute in javaScript

<div id='mypages' data-spacing='10' data-line-width='10.0 cm'></div>
<script>
// 'Getting' data-attributes using dataset 
var mypages = document.getElementById('mypages');
var spacing = mypages.dataset.spacing; // 10;
// 'Setting' data-attributes using dataset
var linewidth = plant.dataset.lineWidth; // 'line-width' -> 'lineWidth'
</script>
Reference:
http://html5doctor.com/html5-custom-data-attributes/ 
 

Remove multiple slashes in web address in server




In .htaccess, add
 # remove multiple slashes anywhere in url

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L] 


http://localhost/test//ajax/ will be redirected to http://localhost/test/ajax/

Sunday, June 29, 2014

Debug Ajax in Firefox




In static websites, we can view source or inspector to do debugging. How about the dynamic websites using Ajax? We need to check if Ajax post data correcttly.
In Firefox, click Tools at top menu, then select
Web developer->network->console
then refresh website or select  input data or select options in forms related to ajar scripts.
You can see the error message if Ajax failed.

Thursday, June 26, 2014

Extract variables from an object stdclass in PHP using get_object_vars




PHP code to dump $fte
<?php 
echo var_dump($fte) ;
?>
Output:
 array(1) { [0]=> object(stdClass)#34 (1) { ["sumfte"]=> string(18) "369.74986600875854" } }
To extract value  369.74986600875854 from this stdclass, we need to use get_object_vars
<?php 
 $ftesum=get_object_vars($fte[0]); 
echo $ftesum['sumfte'];
?>
which returns
369.74986600875854

Wednesday, June 25, 2014

Create and use trigger in MySQL




Suppose we have users table:

CREATE TABLE users
(
user_id int NOT NULL,
salary int NOT NULL DEFAULT 0,
PRIMARY KEY (user_id)
)

We can create a trigger salary_sum
CREATE TRIGGER salary_sum BEFORE INSERT ON users FOR EACH ROW SET @sum = @sum + NEW.salary;

When we execute an insert statement on users table, the trigger salary_sum is excuted:


mysql> SET @sum = 0;
mysql> INSERT INTO users VALUES('andy',2000),('dave',2500),('terry',3000);
mysql> SELECT @sum AS 'Total salary inserted';
+-----------------------+
| Total salary inserted |
+-----------------------+
| 7500               |
+-----------------------+

MySQL foreign key constraint




Suppose we have users table:

CREATE TABLE users
(
user_id int NOT NULL,
user_name varchar(45) NOT NULL,
PRIMARY KEY (user_id)
)

We have another table  pictures,  user_id is foreign key which is primary key in table users.
Using foreign key to prevent  insert user_id in table pictures which is invalid in table users
CREATE TABLE pictures

pic_id  int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (pic_id),
 FOREIGN KEY (user_id) REFERENCES users(user_id)
)

We can use  naming of foreign key constraint, which can be used in drop foreign key
CREATE TABLE pictures

pic_id  int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (pic_id),
CONSTRAINT fk_pictures FOREIGN KEY (user_id) REFERENCES users(user_id)
)

Now we can drop the FOREIGN KEY constraint using the following SQL:
ALTER TABLE pictures DROP FOREIGN KEY fk_pictures

When the primary key is updated or deleted on parent table, what is the behavior of the child table?
 We have four options:
  RESTRICT | CASCADE | SET NULL | NO ACTION
CASCADE: automatically delete or update the child table when the parent table is deleted or rejected
SET NULL: the foreign key table is set NULL  child table when the parent table is deleted or rejected
RESTRICT:  Rejects the delete or update operation for the parent table
NO ACTION: the same as RESTRICT
EXAMPLE:
CREATE TABLE pictures

pic_id  int NOT NULL,
user_id int NOT NULL,
PRIMARY KEY (pic_id),
CONSTRAINT fk_pictures FOREIGN KEY (user_id) REFERENCES users(user_id)
ON DELETE CASCADE ON UPDATE CASCADE
)

 INSERT INTO `users`  VALUES
('1', 'jiansen'), ('2', 'andy')

INSERT INTO `pictures`  VALUES('1', '1')
will be OK
INSERT INTO `pictures`  VALUES('2', '3')
will fail due to that   user_id=3 is not in users table.
Reference:
http://www.w3schools.com/sql/sql_foreignkey.asp
http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html

Thursday, June 19, 2014

php, upload, script time out




PHP, upload a large file
error message:
 script time out
In php.ini, change 30seconds to 300 seconds
max_execution_time=30
to
max_execution_time=3000
Restart Apache.

Tuesday, June 17, 2014

FileMaker and EXCEL date format to MySQL




I export Filemaker database to EXCEL (.csv) and upload it MySQL in PHPMyadmin.
The date format in Filemaker and EXCEL is: dd/mm/YYYY,
while in mySQL: YYYY-mm-dd

It is difficult to change date format in Filemaker to MySQL.
It is easier to change the date format in EXCEL
1) Right click date column in EXCEL, go to Format Cells and then the Number tab.
2) Select Date
3)Change the Locale to English (U.K.), default is English (USA) and you can not find MySQL date format
4) The fifth item on the list 2001-03-14 is the format for MySQL.

Friday, June 13, 2014

php: short_open_tag in php.ini



In php.ini
 short_open_tag = On
the short form (<? ?>)  is allowed.
If
  short_open_tag = Off
you must use the long form of the PHP open tag (<?php ?>).

The reference:
http://php.net/short-open-tag

EXCEL return to the next line in the same cell




To  return to the next line in the same cell in EXCEL:
hold down the ALT key, and then press enter.

Thursday, June 12, 2014

Test PHP mail function




testmail.php to test PHP mail function:
<?php
ini_set('error_reporting', E_ALL);
?>
<html>
<head>
<title>PHP: Test mail function</title>
</head>
<body>
<?php
$email = "jiansen@example.com";
$subject = "Happy Birthday!";
$msg = "Wishing you all the best in life.";
echo $subject;
$headers = "From: jiansen@example.com";
$headers .= "Reply-To: jiansen@example.com";
if(mail($email,$subject,$msg, $headers)){
     echo "E-Mail Sent";
} else {
     echo "There was a problem";
}
?>
</body>
</html>

Send mail test




To test if sendmail working
sendmail -v jiansen@example.com < testmail.txt

testmail.txt is as following:
 Subject: test mail 
This is test.

In Redhat, configure postfix instead of configure  sendmail.


PHP mail.log directory permission




Redhat
In php.ini, set
mail.log = /var/log/mail/mail.log

create directory   /var/log/mail/
sudo mkdir   /var/log/mail/
touch  /var/log/mail/mail.log

change mail folder owner to apache
  sudo chown -R apache /var/log/mail
 sudo chmod 755 /var/log/mail/mail.log

httpd log
sudo vi  /var/log/httpd/error_log
 sudo vi  /var/log/httpd/access_log

Monday, June 9, 2014

Using bitlocker to protect your USB driver




1) bitlocker program comes with WIndows 7 Ultimate and enterprise, but not in basic
2) Insert your USB driver
3)In Windows 7, go to start menu, search bitlocker
4) You will see Bitlocker drive encryption under system and securities, run the program
5) You can see "turn on Bitlocker" near your USB driveico
6) Click  turn on Bitlocker and following instruction step by step and enter your password.
7) Done.

Thursday, June 5, 2014

Unix - find a string in any files under current directory and its sub-directories




To find a string in any  files under current directory and its sub-directories, for example, string "Logout"
 find .  -type f -exec grep -l "Logout" {} \;

EXCEL, merge multiple cells for title or descriptions




To merge multiple cells for title or descriptions in EXCEL
1) select multiple cells
2) Under top menu, Merge & Center, select "merge cells"
3) Type your text
3) To change background color of the text
Go to "Fill color" at the top menu, select color.