Tuesday, June 21, 2011


<?php

class db {
private $connect = "";

function __construct(){
if($connect=$this->connect=new mysqli("localhost", "root", "china", "phpobj")){
echo "connected";
}
}

function sayHello(){
echo "hello";
}

function query($string){
return($this->connect->query($string));
}

}
class ShopProduct {
private $id=0;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;

public function setID($id){
$this->id = $id;
}

public static function getInstance($id, &$db){
$db->sayHello();
$res= $db->query("select * from products where id='$id'");
if(!$res->num_rows){
echo "nothing found";
exit;
}
if(empty($res)){
echo "empty";
}
if($res->num_rows){
$row=$res->fetch_array();
}
$var = $row['type'];
echo $var;
if($row['type']=="book"){
$product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages']);
} else if ($row['type']=="cd"){
$product = new cdProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']);
} else {
$product = new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price']);
}
$product->setId($row['id']);
return $product;
}

function __construct($title, $firstName, $mainName, $price){
$this->title=$title;
$this->producerFirstName=$firstName;
$this->producerMainName=$mainName;
$this->producerPrice=$price;
}

function getProducer(){
return "{$this->producerFirstName} . {$this->producerMainName}";
}

function getSummaryLine(){
$base = "{$this->title} ({$this->producerMainName}, ";
$base .= "{$this->producerFirstName})";
return $base;
}



} //end class ShopProduct

class CdProduct extends ShopProduct {
public $playLength;

function __construct($title, $firstName, $mainName, $price, $playLength){
parent::__construct($title, $firstName, $mainName, $price);
$this->playLength = $playLength;
}

function getPlaylength() {
return $this->playLength;
}

function getSummaryLine(){
$base = "$this-.title ($this->producerMainName, ";
$base .= "$this->producerFirstName )";
$base .= ": page count - $this->numPages";
return $base;
}
} //end class CdProduct

class BookProduct extends ShopProduct {
public $numPages;

function __construct($title, $firstName, $mainName, $price, $numPages){
parent::__construct($title, $firstName, $mainName, $price);
$this->numPages = $numPages;
}

function getNumberOfPages(){
return $this->numPages;
}

function getSummaryLine(){
$base = "$this->title ($this->producerMainName, ";
$base .= "$this->producerFirstName)";
$base .= ": page count - $this->numPages";
return $base;
}
}


$db = new db();
$obj = ShopProduct::getInstance(1, $db);
print_r($obj);
?>







<?php
function date_diff2($date1=0, $date2=0, $debug=FALSE){
$val1 = is_numeric($date1) ? $date1 : strtotime($date1);
$val2 = is_numeric($date2) ? $date2 : strtotime($date2);

$sec = abs($val2 - $val1);
// **DEBUG**
if($debug){
printf("

Date 1: %s ... Date 2 %s

", date('r', $val1), date('r', $val2));
}

$units = getdate($sec);
//**debug**
if($debug){
printf("
%s
", print_r($units, true));
}

$hours = $units["hours"] - (date('Z') / 3600);
$days = $units["mday"];
while($hours>23)
{
$days++;
$hours-=24;
}

$output = array();
$epoch = getdate(0); // the Unix epoch in the server's local time zone
$output["components"] = array("years" => $units["year"] - $epoch["year"],
"months"=>--$units["mon"],
"days"=>--$days,
"hours"=>$hours,
"minutes"=>$units["minutes"],
"seconds"=>$units["seconds"]
);
$output["elapsed"] = array("years"=>$sec / (365*24*60*60),
"months"=>$sec /(30*24*60*60),
"weeks"=>$sec / (7*24*60*60),
"days"=>$sec / (24*60*60),
"hours"=>$sec / (60*60),
"minutes"=>$sec / 60,
"seconds"=>$sec
);

$output["order"] = $val2 < $val1 ? -1 : 1;
return $output;

} //end function

$arrived = mktime(11, 30, 0, 6, 9, 2002);
$departed = mktime(17, 20, 0, 6, 22, 2002);

$holiday = date_diff2($arrived, $departed, TRUE);

//display the entire $holiday array
printf("
%s
", print_r($holiday, TRUE));

$components = $holiday["components"];

$output = array();

foreach($components as $period=>$length){
if($length>0){
$output[]="$length $period";
}
}

printf("

My holiday in Aukland began on %s, and lasted %s.

", date('l, jS F Y', $arrived), implode($output, ', '));
?>

No comments: