Wednesday, December 14, 2011

Class File PHP

I sent a few hours and came up with a Class file that would represent my counter:

id = $id;

//get recs from db
$rec = $this->getRec();
$totalCount = $this->getCounterTotal();

//update instance vars
$this->counterName = $rec->name;
$this->counterValue = $totalCount;
}

private function getRec(){

$rec = mysql_query("
SELECT id, name
FROM tablename
WHERE id = $this->id
");

return mysql_fetch_object($rec);
}

private function getCounterTotal(){
//select and count recs equal to id
$rec = mysql_query("
SELECT COUNT(id) AS count, date
FROM tablename
WHERE counterid = $this->id AND active
");

$row = mysql_fetch_object($rec);
return $row->count;
}

// accessors
public function getCounterName() {
return $this->counterName;
}

public function getCounterValue() {
return $this->counterValue;
}

//mutators
public function incrementCounter() {

//update db
mysql_query("
INSERT INTO tablename
(counterid)
VALUES
($this->id)
");

//var_dump($this->id);
//break;

}


public function resetCounter(){
//get recs for the counterid
mysql_query("
UPDATE tablename
SET active = 0
WHERE counterid = $this->id
");

//var_dump($this->id);
//break;
}

public function getAllRecs(){
$recs = mysql_query("
SELECT c.name AS name, a.date AS date, a.schoolid AS schoolID, a.active AS active

FROM tablename a
join tablename c on c.id = a.counterid

ORDER BY date DESC
");

return $recs;
}

}

?>

While building my class file, I used a test.php file along with it to test it. A common practice when building bus. classes. Once the class was complete, I completed the DB table.

I ended up creating two tables:

tablename
tablename_associations

The tablename includes an id and a name for the counter.

Most of the data storage is in the associations table, where i am storing the id, the date of the request and a boolean col representing if the col is active in the current count, since it can be reset.

Next, I build an administration page that would allow whoever we gave access to it to reset each of the counters. Also on the admin. page is a report of each of the requested counts: see below






Now came the trickiest part of all. The problem I ran into next, was my new learning of the week. When using AJAX, which is essentially the XMLHTTPRequest object , that you CAN NOT remotely call a page on another server. This is know as the same server policy. This was a big problem since the server that was serving up the counter requests was NOT THE SAME as the server that was hosting the class(s) and db that were storing the counter. I had no way to reconcile this since I did not own the client server, only the serving server.

Hmmmm, what to do next??

This post got me started to what my option would be, which was basically two

1 - create or use a proxy server to fool the client and server into thinking they were the same domain
2 - use a different javascript notation called JSON, that would allow you to inject a script.

Neither of these options seems too trivial, after about a day of reading up on the two, i choose option 2., JSON.

Here is the JSON enabled call from the client side:

function getCounterJSON(param){
$.getJSON('http://domainName/path/view.php?callback=?','counter=' + param ,function(res){
document.getElementById(param).innerHTML = '&nbsp&nbsp&nbsp' + res.counterValue + ' views';
});

}

Here is the view.php page and its callback function.

//VERY sneaky JSON javascript object notation callback function - working around the security *blocks when ajaxing from one domain to another - which is illegal.
{
echo $_GET['callback'] . '(' . "{'counterValue' : $counterValue}" . ')';
}

Here is the rest of the view.php page preceding the callback function.

//CounterBase creates the objects $marks, $attendance, $progress
include_once 'CounterBase.php';

//check to see if someone is requesting to update a specific counter
$counter = $_GET['counter'];
$counterValue = 0;

//var_dump($_GET['counter']);
//break;

if ($counter == 'marks') {
$counterValue = $marks->getCounterValue();
}elseif($counter == 'attendance'){
$counterValue = $attendance->getCounterValue();
}elseif($counter == 'progress'){
$counterValue = $progress->getCounterValue();
}

No comments: