Thursday, July 30, 2009

AJAX request

var xmlhttp;

function showSpecial()
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ('Browser does not support HTTP Request');
return;
}
var url='ajax.php';
url=url+'?special=1';
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById('special').innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject('Microsoft.XMLHTTP');
}
return null;
}


in ur ajax.php

echo "test";

it will print out "test" inside the:
test

Wednesday, July 29, 2009

My own PHP query builder

say you have this array of column name and its value:

$arr = array('str' => 9, 'agi' => 10, 'name' => 'foo')

use this query builder to create your insert or update query:

function query_builder($arr, $table_name, $type, $clause="") {
if ($type == "insert") {
$q = "$type into $table_name";
$keys = "";
$values = "";
foreach ($arr as $key => $value) {
$keys .= "`$key`, ";
$values .= "?, ";
}
$keys = substr_replace($keys,"",-2);
$values = substr_replace($values,"",-2);
$q .= "($keys) values($values)";
}
else if ($type == "update") {
$q = "$type $table_name set ";
foreach ($arr as $key => $value) {
$q .= "`$key` = ?, ";
}
$q = substr_replace($q,"",-2);

$q .= " where $clause";
}
return $q;
}

example:
$q = query_builder($arr,"table_1","insert");
or
$q = query_builder($arr,"table_1","update","id = 123");

$values = array_values($arr);

//fyi, my DB uses PDOStatement->execute
$db = new DB();
$db->run($q, $values);

Tuesday, July 28, 2009

Get all filenames in a directory PHP

$string="";
$fileCount=0;
$filePath=$PATH.'/var/www/public_html/'; # Specify the path you want to look in.
$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) {
if (eregi("\.php",$file)) { # Look at only files with a .php extension
$string .= "$file
";
$fileCount++;
}
}
if ($fileCount > 0) {
echo sprintf("List of Files in %s
%sTotal Files: %s
",$filePath,$string,$fileCount);
}

Friday, July 24, 2009

FBJS: adding id and onclick attribute

// create new div element
var d = document.getElementById('some_element');
var newdiv = document.createElement('div');

newdiv.setId('newid');

var content = ''an anchor html tag with id='test'"; // can't really put html code in blogger for some reason lol
newdiv.setInnerXHTML(content);
d.appendChild(newdiv);

// add an onclick attribute inside id 'test'
document.getElementById('test').addEventListener('click', function() {foo();} );

=================

Wednesday, July 15, 2009

SQL Join

  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables
The INNER JOIN keyword return rows when there is at least one match in both tables.

Monday, July 13, 2009

Removing last char

PHP:
$string = "asdf"
$string = substr_replace($string ,"",-1);

JS:
str = "asdf";
str = str.slice(0,-1);

Thursday, July 9, 2009

AJAX in FB + setTimeout for auto refresh

this is just a simple example of how this JS function, awaits for data coming from PHP script called chat.php and put everything that gets printed there into a div tag with id 'chat':

function chat_refresh() {
var refresh = 1;
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.ondone = function(data) {
document.getElementById('chat').setInnerFBML(data);
}
ajax.requireLogin = 1;
var params = {'refresh' : refresh};
ajax.post("chat.php?refresh", params);
setTimeout(chat_refresh, 1000*10);
}

lets say in chat.php?refresh

you print "hello world";

then "hello world" will get printed in between
before:

after:
hello world
tags in the HTML page.

note:
setTimeout(chat_refresh, 1000*10);
is a function that will automatically call chat_refresh every 10 seconds.

date time difference function in PHP

function dateDiff($dt1, $dt2, $timeZone = 'GMT')
{
$tZone = new DateTimeZone($timeZone);
$dt1 = new DateTime($dt1, $tZone);
$dt2 = new DateTime($dt2, $tZone);

$ts1 = $dt1->format('Y-m-d');
$ts2 = $dt2->format('Y-m-d');

$diff = abs(strtotime($ts1)-strtotime($ts2));

$diff/= 3600*24;
//$diff/=60 if you want to check minutes difference


return $diff;
}

mysql date time and PHP + JS

This is how you get current date time in php:

$dateTime = new DateTime( "now", new DateTimeZone(date_default_timezone_get() ) );
$dt = $dateTime->format("Y-m-d H:i:s");

and this is how to compare it with a row in DB:

$q = "SELECT * FROM ht_chat_logs where TIMESTAMPDIFF(SECOND, time, '$dt') < 0 order by time asc";

it will return all the rows that were created after $dt.

==================================

And this is how you do it in JS:

var currentTime = new Date()
var last_activity = currentTime.getFullYear() + '_' + (currentTime.getMonth() + 1) + currentTime.getDate() + ' ' + currentTime.getHours() + ':' + currentTime.getMinutes() + ' ' + currentTime.getSeconds();

it's gonna return date time in the same format as the one we did in PHP and it's also the same format of mysql timestamp.