I have been using PHP, Javascript, Jquery with codeignitor since 2009. I used to use and make many custom functions for development purpose of which some are noteworthy and may be useful for others also. I would like to share and discuss such custom functions with all enthusiasts.
Friday, September 11, 2015
CSS -classes in template
seperator-dash
code-hint-inline
hint-span
validationError
demobox
htmlbox
scriptbox
titlebox
Wednesday, September 9, 2015
Complete script for login with google and fetch details of users
Complete script for login with google and fetch details of users
HTML
<a id="customBtn" class="gmail_a">
<img src="<?php echo ROOT_URL_BASE;?>image/google.png" /></a>
</a>
Javascript
var googleUser = {};
//Replace with your google client id
var clientId = 'xx-xx.apps.googleusercontent.com';
var startApp = function() {
gapi.load('auth2', function(){
//Retrieve the singleton for the GoogleAuth library
// and set up the client.
auth2 = gapi.auth2.init({
client_id: clientId,
cookiepolicy: 'single_host_origin',
// Request scopes
scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
});
attachSignin(document.getElementById('customBtn'));
});
};
function attachSignin(element) {
console.log(element.id);
auth2.attachClickHandler(element, {}, function(googleUser) {
gapi.client.load('plus', 'v1', signedIn);
}, function(error) {
alert(JSON.stringify(error, undefined, 2));
});
}
function signedIn(){
gapi.client.plus.people.get({userId: 'me'}).execute(function(resp){
handleEmailResponse(resp);
})
}
function handleEmailResponse(resp) {
var primaryEmail;
for (var i=0; i < resp.emails.length; i++) {
if (resp.emails[i].type === 'account')
primaryEmail = resp.emails[i].value;
}
parseGoogleUserDetails(resp);
}
function parseGoogleUserDetails(GoogleResponse) {
console.log(GoogleResponse);
//The response contains many information of users
var primaryEmail;
for (var i=0; i < GoogleResponse.emails.length; i++) {
if (GoogleResponse.emails[i].type === 'account')
primaryEmail = GoogleResponse.emails[i].value;
}
var first_name = GoogleResponse.name.givenName;
var last_name = GoogleResponse.name.familyName;
var gender;
if (typeof GoogleResponse.gender != 'undefined') {
gender = capitalizeFirstLetter(GoogleResponse.gender)
}
var city;
if (GoogleResponse.placesLived.length > 0 )
for (var i=0; i < GoogleResponse.placesLived.length; i++) {
if (typeof GoogleResponse.placesLived[i].primary){
if (GoogleResponse.placesLived[i].primary === true)
city = GoogleResponse.placesLived[i].value;
}
}
var formData = {
'socialMedia': 'google',
'socialMediaId' : GoogleResponse.id,
'email' : primaryEmail,
'first_name' : first_name,
'last_name' : last_name,
'gender' : gender,
'city' : city
};
createSocialAccount(formData)
}
Tuesday, September 8, 2015
Facebook API
How to get user email and birthday from Facebook API Version v2.4
I found answer in this thread of stack overflow: http://stackoverflow.com/questions/31554854/how-to-get-user-email-and-birthday-from-facebook-api-version-v2-4
This was the answer I get the solution from: http://stackoverflow.com/a/31556390/345721
Monday, September 7, 2015
Mysql tips and tricks
User records table: How to get all ids of peoples who have matching year and month only
First thing you have to achieve is to
"get all date and month of 'dob' column"
This can be achieved by grouping rows based on date_month extracted using the format construct.
SELECT
DATE_FORMAT(dob, '%m-%d') AS date_month
FROM bday
GROUP BY DATE_FORMAT(dob, '%m-%d');
As per your question, Next is
"peoples who have matching year and month only"
ie, group the above results with condition having more than one row. This can be achieved by adding the condition "HAVING count(id) > 1"
Now the query becomes
SELECT
DATE_FORMAT(dob, '%m-%d') AS date_month
FROM bday
GROUP BY DATE_FORMAT(dob, '%m-%d')
HAVING count(id) >1);
This query returns all date_month where more than one person's dob falls.
Now your ultimate aim is to get
" id of those peoples who have matching year and month only "
This can be achieved by wrapping this results in a sub query. ie, you have to fetch all ids with date and month falls in the set of results extracted using the previous query
SELECT
id, DATE_FORMAT(dob, '%m-%d') AS date_month
FROM bday
WHERE
DATE_FORMAT(dob, '%m-%d') IN
(
SELECT
DATE_FORMAT(dob, '%m-%d') AS date_month
FROM bday
GROUP BY DATE_FORMAT(dob, '%m-%d')
HAVING count(id) >1) ;
)
Query and result:
MySql "CONCAT" usage example
$sql = "SELECT
email,
username,
CONCAT('https://mysiteurl/profile/',user_id) as profile_url
FROM `engine4_user`
LIMIT 5000
into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ";
mysql update a column with an int based on order of another field
SET @rownumber = 0;
update mytable set Moneyorder = (@rownumber:=@rownumber+1)
order by MoneyOrder asc
or to do it in a single query you can try
update mytable set Moneyorder = (@rownumber:=@rownumber+1)
order by MoneyOrder asc
update mytable
cross join (select @rownumber := 0) r
set Moneyorder = (@rownumber := @rownumber + 1)
order by MoneyOrder asc
Click the following link to find a good article on how to get first n results from each category: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
PHP - Mysql: Get last error
mysql_error
PHP - Mysql: determine which database is selected?
function mysql_current_db() {
$r = mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r,0);
}
$r = mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r,0);
}
Wednesday, September 2, 2015
How to replace Microsoft-encoded quotes in PHP
Scenario: I need to replace Microsoft Word version of single and double quotations marks (“ ” ‘ ’) with regular quotes (' and ") due to an encoding issue in my application.
$search = array(
chr(145),
chr(146),
chr(147),
chr(148),
chr(151)
);
$replace = array(
"'",
"'",
'"',
'"',
'-'
);
return str_replace($search, $replace, $value);
Reference: http://stackoverflow.com/a/1262060/345721
$search = array(
chr(145),
chr(146),
chr(147),
chr(148),
chr(151)
);
$replace = array(
"'",
"'",
'"',
'"',
'-'
);
return str_replace($search, $replace, $value);
Subscribe to:
Posts (Atom)