Categories
JavaScript Tips & Tutorials PHP Tips & Tutorials

How to execute / run PHP code inside JavaScript files?

Static files of JavaScript would survive most applications but sometimes the ability to include PHP code inside JavaScript scripts and generate the content of the script files on the fly by PHP is a better option. How does one do that?

The simplest solution is to just include PHP code inside the <script></script> section of your HTML templates / web pages because chances are, the file extension is .php:

<script>
var jsVar = "<?php echo $phpVar ?>";
</script>

Even if it’s not ending in .php, such as in .html or .htm, you can configure your server to parse all .html or .htm files for any PHP code, though with a little extra server burden.

Another solution for this is to make your server parse all files ending in .js. Just create a .htaccess if it doesn’t exist in the directory in which you wish to include and run PHP code inside all .js files. Add these lines at the end of .htaccess:

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js

<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

Now you can add all the PHP code you can inside any JavaScript files with a .js extension, the server will automatically parse and run them when the client side requests the file.

Categories
SQL / MySQL Tips and Tutorials

MySQL logic operators: How to use AND, OR together in WHERE clauses in one query?

AND and OR are common logic operators across all programming languages and in MySQL, they mean the same thing. A statement (a.k.a, a condition in any WHERE clauses such as id > 100) can be either true or false. A group / combination of statements can be true or false, depending on their own values (true or false) and the logic operators that have joined them.

AND and OR each can join 2 statements.

AND – both statements must be true

For example, in this MySQL query:

SELECT * FROM student WHERE age > 10 AND grade < 4

All students who are graded less than 4 and older than 10 will be selected. The logic operator AND commands that both of the conditional statements (age > 10 and grade < 4) must be true for the whole statement (age > 10 AND grade < 4) to be true. You can add more conditions and ask them all to be true to narrow down the selection:

SELECT * FROM student WHERE age > 10 AND grade < 4 AND sex = 'female'

Which effectively selects all the female students who are older than 10 and have a grade of lower than 4 because the AND operators mean that all 3 statements must be true for the whole, joined statement (age > 10 AND grade < 4 AND sex = ‘female’) to be true.

OR – at least one statement must be true

Consider this example,

SELECT * FROM student WHERE grade > 4 OR grade < 2

This simply selects all the students who have a grade higher than 4 or lower than 2. All the students who satisfy one or more of these statements are selected. Students who satisfy none of these statements are neglected. You can as well use more than one OR in a series:

SELECT * FROM student WHERE grade > 4 OR age < 9 OR surname = 'Bush'

So that any student who has a grade higher than 4 or is less than 9 years old or has a surname of Bush will be selected.

How to use AND and OR together?

Consider the following example,

SELECT * FROM student WHERE grade > 2 AND grade < 5 OR age > 8 AND age < 12

As AND has a higher priority (effectively so in almost all programming languages) than OR, it will be evaluated before any OR statements. Therefore, this query will select all those students whose grade is higher than 2 and lower than 5 or those whose age is older than 8 and younger than 12.

You can add parenthesis but because AND has a higher priority in parsing the logic statement, it’s the same:

SELECT * FROM student WHERE (grade > 2 AND grade < 5) OR (age > 8 AND age < 12)

However, as parenthesis has the highest priority, if you add them in a different way such as this:

SELECT * FROM student WHERE (grade > 2 AND grade < 5 OR age > 8) AND age < 12

It would give completely different results. Firstly, grade > 2 AND grade < 5 must be true or age > 8 must be true, secondly, age < 12 must be true. For example, these students will satisfy this WHERE clause:

  1. grade = 4, age = 7
  2. age = 9
  3. grade = 3, age = 11

These students don’t satisfy this WHERE clause:

  1. age = 14
  2. grade = 5, age = 7
Categories
PayPal & Ecommerce

This payment cannot be completed and your account has not been charged. Please contact your merchant for more information.

Very rarely you may encounter this error after you have clicked the “Pay Now” button on PayPal to send payment to someone / merchant in purchase of something:

This payment cannot be completed and your account has not been charged.  Please contact your merchant for more information.

paypal error: This payment cannot be completed and your account has not been charged.  Please contact your merchant for more information.

At first, I blamed my website payment integration on Convert Hub and thought that something about my merchant account (business account) at PayPal might have gone wrong.

However, I later found out that it’s actually because of my personal account which I used to pay to my merchant account. My personal account of PayPal was somehow limited at that time and could not send money by any means.

After providing necessary documents to verify my legal ownership of the account, its full access was restored and I tried a test payment via the “Buy Now” button at here. There was never any errors any more and the payment was successful.

Categories
My Personal Reviews

Prova.fm, the Design Contests Platform – From Outsource to Crowdsource

When you need something done, design contests are a much better way to get the quality and diversity you need than traditional design outsourcing because you get multiple submissions from various designers who are after the bounty prize. It may be a little more expensive than when you hire just one designer, but the quality and diversity you get will definitely justify it. Not to mention the time you will save for communications with the one designer as well as later revisions.

Prova.fm is just such a contest platform that enables you to get what you need without spending an arm and a leg. You can get all the talents working for you on a single project by launching a contest. Be it print, web, audio or video, you can get all the input in just a few days and then decide which one you’d like to accept as the winner so that the designer can claim the prize you are paying. It’s perfect for a logo or advertisement banner design.

Categories
Client Side Coding Books CSS & HTML Tips Rich Internet Application

Best and Newest HTML5 Books (and Some CSS3 Books)

HTML5 and CSS3 are becoming a reality. To be better prepared and armed with the latest technologies of the web so that we stay on the cutting edge when creating stuff, I have found some of the best book titles at Amazon about HTML 5 and CSS 3. Note that several of these books have not been released yet. You can pre-order and receive them when they are out.

Web Developers / Designers’ Books:

  1. Best HTML Books
  2. Best CSS Books
  3. Best JavaScript Books
  4. Best PHP Books
  5. Best MySQL Books
  6. Best Linux Books
  7. Best Apache Books (mod_rewrite Books)
  8. Best Web Hosting Books

HTML5: Up and Running

html 5 up and running

Here’s a godsend online book for learning HTML5 written by Mark Pilgrim as well.

HTML5: Designing Rich Internet Applications (Visualizing the Web)

HTML5 Designing Rich Internet Applications (Visualizing the Web)

Beginning HTML5 and CSS3: Next Generation Web Standards

Beginning HTML5 and CSS3 Next Generation Web Standards

Pro HTML5 Programming: Powerful APIs for Richer Internet Application Development

Pro HTML5 Programming Powerful APIs for Richer Internet Application Development

Introducing HTML5 (Voices That Matter)

Introducing HTML5 (Voices That Matter)

Beginning iPhone Web Apps: HTML5, CSS3, and JavaScript for WebKit

Beginning iPhone Web Apps HTML5, CSS3, and JavaScript for WebKit

HTML5 Now: A Step-by-Step Video Tutorial for Getting Started Today (Voices That Matter)

HTML5 Now A Step-by-Step Video Tutorial for Getting Started Today (Voices That Matter)

HTML5 Straight to the Point: Using HTML5 with CSS3 and JavaScript

HTML5 Straight to the Point Using HTML5 with CSS3 and JavaScript

CSS3: Visual QuickStart Guide (5th Edition)

CSS3 Visual QuickStart Guide (5th Edition)

Categories
Internet Tools Web Applications & Online Software

How to delete a Skype account so that it doesn’t appear in search?

Several of the Google results for ‘how to delete Skype account’ suggest that you can’t literally delete any Skype account once created. However it really bothers me that more than one account under one email address would definitely confuse people who have searched for you and are trying to figure out which one is the regular account of yours. Half of the chance is that he or she would end up with the wrong one.

So how can you delete extra Skype accounts so that you have only one user account under your email address?

The answer is you can’t. But you can change the registered email address of the extra accounts. Just sign in to your Skype account and go to https://secure.skype.com/account/personal/change-email to perform the change. After the change, people will see no more unwanted accounts under the original email. Problem solved.

Categories
Apache Web Server Tutorials & Tips PHP Tips & Tutorials SQL / MySQL Tips and Tutorials

How to bring down / optimize memory usage in your unmanaged Linux VPS box and avoid OOM (Out Of Memory) errors?

The other day I was very upset about some extraordinary down times of my unmanaged VPS box at Linode. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to get the problem sorted out myself. After a few tweaks and observations for a week, so it seems that I have successfully optimized my VPS server to take on more traffic with less resources such as RAM.

The problem almost always lies in where the user is free to feed stuff to your website or program. Sometimes Convert Hub spikes in memory usage and forces my box to use swap that relies on disk I/O to work. This happens when someone uploads an ultra large picture to be processed or converted. While I may restrict the size of the picture that is allowed to be uploaded, I may also do the following settings to optimize the entire LAMP environment so the other websites enjoy it as well.

Apache 2 Low-Memory Optimization

Use this command to identify the MPM you are using:

apache2 -V | grep 'MPM' # for Debian-based systems
httpd -V | grep 'MPM' # for Fedora/CentOS systems

Find and change these settings in your Apache 2 configuration file (usually found at /etc/apache2/apache2.conf):

StartServers 1
MinSpareServers 3
MaxSpareServers 6
ServerLimit 24
MaxClients 24
MaxRequestsPerChild 3000

Switch to Lighttpd or Litespeed if possible.

MySQL Low-Memory Optimization

Same as above, find and change these settings of the MySQL configuration file (may be at /etc/mysql/my.cnf) accordingly:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K

PHP Low-Memory Optimization

Find your PHP configuration file (php.ini) and modify the PHP script memory limit to 32M or less (default is 128M):

memory_limit = 32M