February 16 2016

WordPress Header error wp-includes/pluggable.php

Tagged Under : ,

Wordpress
When using wp_redirect() you will facing Header error “PHP Warning: Cannot modify header information – headers already sent by …… wp-includes/pluggable.php on line 1228”

To solve this problem you need to change the PHP.ini value. In PHP.ini find

February 16 2016

WordPress Ajax Example

Tagged Under : , ,

Wordpress
Below is the basic example of how to use AJAX in WordPress. It show you how to using javascript pass the variable to PHP function and then return back to javascript.

Javascript
var ajaxurl = '<?php echo admin_url() ?>admin-ajax.php';
jQuery(document).ready(function () {
	jQuery.ajax({
		url: ajaxurl,
		data: {
			//This action must same with the PHP function name
			"action": "ajax_request",
			"params": "wordpress ajax"
		},
		success:function(data) {
			//This is the result return from PHP function
			console.log(data);
		},
		error: function(errorThrown){
			//This is the error return
			console.log(errorThrown);
		}
	});
});

June 28 2015

PHP some variable fail to post data to server

Tagged Under :

php
This was my first time facing this problem. When I post more than 1000 variable value to server, it only show me 1000 variable received and the rest was missing.

To solve this problem you need to increase max_input_vars number in PHP.ini to 3000 or 5000. It was because the max_input_vars default value was 1000. If you post more than the value server will ignore the rest.

if your server uses the Suhosin patch or after change the max_input_vars number the problem still same. you can try add
suhosin.post.max_vars = 3000 
suhosin.request.max_vars = 3000
to increase the maximum number of post variable.

April 14 2015

Paypal rest API add discount on the total amount

Tagged Under :

On Paypal rest APi, it don’t have a specifies discount function for the payment. And you are not allow minus the discount price from the total amount, because Paypal itself will add all the items total price when you submit. If the total price are different with the amount you set. It will return an error for you.

The simple way to set the discount price in Paypal APi. you can set the discount as an item. For example to set the discount.
$item3 = new Item();
$item3->setName('Voucher Discount')
    ->setDescription('Voucher Discount')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setPrice('-10');
Remember you must set the price as ‘-10’ no -10. If the latter´╝î it will become 0 and then the total items prices will different with the Total amount you set inside the Transaction. Once it different Paypal will return an error for you.

February 04 2015

Jquery simple dynamic dropdown options with PHP array

Tagged Under : ,

jquery
Dynamic dropdown options always use in our Application. It can be based on user first selection to return a new listing to second dropdown options by AJAX.

Below is the example PHP array script:
$option = array();
$option[] = array('name' => 'fruits', 'value' => 'f');
$option[] = array('name' => 'vegetables', 'value' => 'v');
$option[] = array('name' => 'pizza', 'value' => 'p');

And now we need the Jquery script to build the dropdown select. Below script are allow to reuse again when you need to replace the dropdown option again.

March 29 2014

PHP how to insert Chinese character into MySQL

Tagged Under : ,

php
The way to insert Chinese character into MySQL table. You need set the table structure charset to utf8.
CREATE TABLE IF NOT EXISTS `contents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
After that, add the following code as below:
mysql_connect(HOST, USER, PASSWORD);
mysql_select_db(DBNAME);
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT=utf8");
mysql_query("SET CHARACTER_SET_RESULTS=utf8");

February 19 2014

PHP Convert a String to Variable

Tagged Under :

php
Have 2 methods to do it.

Method 1:
echo eval('return $'. $string . ';');

And another method as below:
$counter = 1;
$button1 = "Hello PHP";
$printbutton = ${'button' . $counter};

echo $printbutton
The Method 2 example will print out Hello PHP

February 19 2014

Install php5 on Miscrosoft IIS

Tagged Under :

php
Installing PHP itself is quite straightforward; the difficulty lies in configuring IIS to use it! There are a number of programs contained within the standard Win 32 distribution, for example it contains two versions of the PHP interpreter that we can use with IIS. The first is a DLL (Dynamic Link Library) that contains an ISAPI module for IIS. This module will run as a persistent interpreter in IIS, one that does not need to be called externally, so in theory should provide the best performance.

In reality, I have found the ISAPI module to be unstable with PHP 5 and IIS on Windows, often causing access violations. These issues will most likely be resolved in time, and may be by the time that you are reading this.

The second version, a standard C executable, is the one that we will be using. This runs under IIS as an external CGI (Common Gateway Interface) call, similar to the way Perl/CGI works, so as a result is not as quick as the ISAPI module, but it is very stable. In the trade-off between speed and stability, we should always favour stability for a live production server.

Installing Steps
-Create a folder called PHP on the C drive root
-Unzip the contents of the zip file you downloaded for PHP 5 into C:\PHP
-In C:\PHP, rename php.ini-dist to php.ini 
-You may want to add C:\PHP onto the system path variable, as you did for MySQL in the previous section
That’s it! Now onto configuring PHP 5.

October 26 2013

php get first and last date of the month

Tagged Under :

php
My application always need get first and last month of the date when user given a date to my system. it look like very trouble but actually quite easy only.

From here I show you how to get first and last of the date in current month.
//last day of the month
date("Y-m-t");
//first day of the month
date("Y-m-1");
Why using t in last day of the month, it is because in PHP date function “t” will return you Number of days in the given month.

And now we try get first and last date of the month from the last month.

August 11 2013

Tidesdk display phpinfo and php exntersion

Tagged Under : ,

TideSDK
In here, I am showing you how to use TideSDK to display the phpinfo() and available php extension in your application.

First, use the TideSDK Developer to create a new application. After that, open the index.html file and edit the source.

In this example, I use JQuery to call the phpinfo(). So we need include the JQuery as well.

Include the JQuery inside the head.
<head>
<script src="jquery-2.0.1.min.js"></script>
</head>
We need include another PHP class to call the phpinfo() script to display the information. include the PHP file in head as well.
<head>
<script src="jquery-2.0.1.min.js"></script>
<script type="text/php" src="info.php"></script>
</head>