ajax files

var ajaxResult=0,formaction;
var holder = "errorString";

function getvaldropdown(cid,hold)
{
holder = hold;
var url="ajax.php";
url=url+"?action=getCategoryValues&cid="+cid;
url=url+"&sid="+Math.random();
sendRequest(url);
}

function createRequestObject() {

var req;

if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {

alert('Problem creating the XMLHttpRequest object');
}

return req;

}

var http = createRequestObject();

function sendRequest(url) {
http.open('get', url);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {

if(http.readyState == 4 && http.status == 200){

var response = http.responseText;

if(response) {
document.getElementById(holder).innerHTML = response;
}
}
}

spry validation




Username is required. User can change it anytime.






Password is required. User can change it anytime.
Password needs to be more than 6 characters.



count no of days

function countdays($date)
{
$today = time();
$diff=$today -$date;
$days = floor($diff/86400);
$months=floor($days/30);
$day=floor($days%30);
return "Member for: $months months $day days ";
}

Is numeric

function IsNumeric(sText)

{
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;

}

pagination

include_once ('Pager/Pager.php');



$params = array(
'mode' => 'Sliding',
'perPage' => 10,
'delta' => 1,
'itemData' => $list
);
$pager =& Pager::factory($params);
$pageset = $pager->getPageData();



foreach($pageset as $data){

}




mail validate

function echeck(str) {

var at="@"
var dot="."
var IsMail=true;
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
IsMail=false;
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
IsMail=false;
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
IsMail=false;
}

if (str.indexOf(at,(lat+1))!=-1){
IsMail=false;
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
IsMail=false;
}

if (str.indexOf(dot,(lat+2))==-1){
IsMail=false;
}

if (str.indexOf(" ")!=-1){
IsMail=false;
}

return IsMail;
}

Get the time from specific date -JS

function countdays($date)
{
$today = time();
$diff=$today -$date;
$days = floor($diff/86400);
$months=floor($days/30);
$day=floor($days%30);
return "Member for: $months months $day days ";
}

Configuration File

//Site Configuration
define ('DOC_ROOT', 'C:/xampp/htdocs/defence/');
define ('HTTP_PATH', 'http://localhost/defence/');

//Database Configuration localhost
define ('DB_TYPE','mysql');
define ('DB_HOST','localhost');
define ('DB_USER','root');
define ('DB_PWD','');
define ('DB_DB','defence');

Sample Controller headers

session_start();
require_once("config.php");
require_once(DOC_ROOT."classes/dbcon.class.php");
require_once(DOC_ROOT."classes/user.class.php");
require_once(DOC_ROOT."classes/product.class.php");

$action = $_GET['action'];

Sample User Class

Class User{
private $db;
private $id;

function __construct($id=0){
$this->db = DBcon::getInstance();
$this->id=$id;
}


/*******functions goes here**********/

} // End of User Class
?>

Database Connection Class


require_once('DB.php');

class DBcon
{
private $instance;

function getInstance(){
if(isset($this->instance)){
return $this->instance;
}else{
$dsn = DB_TYPE.'://'.DB_USER.':'.DB_PWD.'@'.DB_HOST.'/'.DB_DB;
$this->instance = DB::connect($dsn);
if(DB::isError($this->instance))
{
header('Location: '.DB_ERROR_FILE);
exit();
}
$this->instance->setFetchMode(DB_FETCHMODE_ASSOC);
return $this->instance;
}
}

function lastId(){
return mysql_insert_id();
}

}

?>

Introduction

This blog is mainly for the php developers who ara willing to work in OO enviornment. You can find sample codes here.