Tuesday, September 18, 2007

php Script to fetch RSS info

/**
* Class name: RSS
* Author : LeadStar Team
* website : http://www.leadstar.com.cn/
* CopyRight : LeadStar Team
*/
if (defined('_CLASS_RSS_PHP')) return;
define('_CLASS_RSS_PHP',1);

class RSS {
//public
var $rss_ver = "2.0";
var $channel_title = '';
var $channel_link = '';
var $channel_description = '';
var $language = 'zh_CN';
var $copyright = '';
var $webMaster = '';
var $pubDate = '';
var $lastBuildDate = '';
var $generator = 'RedFox RSS Generator';

var $content = '';
var $items = array();

/**************************************************************************/
// function name: RSS
// function description: construct
// parameter: $title
// $link
// $description
/**************************************************************************/
function RSS($title, $link, $description) {
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubDate = Date('Y-m-d H:i:s',time());
$this->lastBuildDate = Date('Y-m-d H:i:s',time());
}
/**************************************************************************/
// function name: AddItem
// function description: add a new node
// parameter: $title
// $link
// $description $pubDate
/**************************************************************************/
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array('title' => $title ,
'link' => $link,
'description' => $description,
'pubDate' => $pubDate);
}
/**************************************************************************/
// function name: BuildRSS
// function description: generate rss xml file content
/**************************************************************************/
function BuildRSS() {
$s = "
\n\n";
// start channel
$s .= "\n";
$s .= "channel_title}]]>\n";
$s .= "<1ink>channel_link}]]>\n";
$s .= "channel_description}]]>\n";
$s .= "{$this->language}\n";
if (!empty($this->copyright)) {
$s .= "copyright}]]>\n";
}
if (!empty($this->webMaster)) {
$s .= "webMaster}]]>\n";
}
if (!empty($this->pubDate)) {
$s .= "{$this->pubDate}\n";
}

if (!empty($this->lastBuildDate)) {
$s .= "{$this->lastBuildDate}\n";
}

if (!empty($this->generator)) {
$s .= "{$this->generator}\n";
}

// start items
for ($i=0;$iitems);$i++) {
$s .= "\n";
$s .= "items[$i]['title']}]]>\n";
$s .= "<1ink>items[$i]['link']}]]>\n";
$s .= "items[$i]['description']}]]>\n";
$s .= "{$this->items[$i]['pubDate']}\n";
$s .= "
\n";
}

// close channel
$s .= "
\n
";
$this->content = $s;
}
/**************************************************************************/
// function name: Show
// function description: print out the RSS xml file content
/**************************************************************************/
function Show() {
if (empty($this->content)) $this->BuildRSS();
echo($this->content);
}
/**************************************************************************/
// function name: SaveToFile
// function description: save the RSS content to a file
// parameter: $fname the file name to be saved
/**************************************************************************/
function SaveToFile($fname) {
$handle = fopen($fname, 'wb');
if ($handle === false) return false;
fwrite($handle, $this->content);
fclose($handle);
}
}

Saturday, September 8, 2007

Ajaxian.com 2006 Survey Results

Ajaxian.com 2006 Survey Results

The results of our second annual Ajaxian.com survey, prepared by Richard Monson-Hafael from the Burton Group, are in. And the winner is… Prototype, the most popular Ajax framework, by a considerable margin: 43% of you use it. Script.aculo.us is next, at 33%, confirming observations that many of made of the popularity of that duo.

The full results of our framework survey follow:

Framework Survey Results

Note that multiple responses per participant were allowed; we’ve also thrown out any result with less than 3% of responses in the above graphic.

We also asked you about the server-side platform you’re using. The big winner here was PHP, with 50% of you using it:

Platform Survey Results

Some other interesting factoids:

  • 25% of you eschew frameworks and work with XMLHttpRequest directly (wow!)
  • 11% of you are using JSON to transfer data; unfortunately, we didn’t ask enough questions to determine how this compares with XML or other data formats
  • 3% of you are still using Microsoft’s “classic” ASP framework; five of you (~0.6%) are using C++ (+2 points for increased performance, -100 for increased complexity?)
  • 2% of you wrote in to say that you’re using Adobe’s Flex toolkit (hey, those banner ads are working out…); 2% also indicated that they use the Flex/Ajax bridge. Unfortunately, the survey software we used doesn’t let us correlate these entries, so we can only say that 2%-4% of you are using Flex in some way
  • One participant uses Delphi (how’s that working out for you?), and another is using LISP (can we hire you?).

Tuesday, August 28, 2007

getElementsByClassName My Eesy Edition

Take it easy

function getElementsByClassName(className, tag){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";

var elements = (tag == "*" && document.all)? document.all : document.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i
current = elements[i];
if(testClass.test(current.className)){
returnElements.push(current);
}
}
return returnElements;
}

getElementsByClassName Delux Edition

GET it FROM http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/

function getElementsByClassName(strClass, strTag, objContElm) {
strTag = strTag || "*";
objContElm = objContElm || document;
var objColl = objContElm.getElementsByTagName(strTag);
if (!objColl.length && strTag == "*" && objContElm.all) objColl = objContElm.all;
var arr = new Array();
var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
var arrClass = strClass.split(delim);
for (var i = 0, j = objColl.length; i < j; i++) {
var arrObjClass = objColl[i].className.split(' ');
if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
var c = 0;
comparisonLoop:
for (var k = 0, l = arrObjClass.length; k < l; k++) {
for (var m = 0, n = arrClass.length; m < n; m++) {
if (arrClass[m] == arrObjClass[k]) c++;
if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
arr.push(objColl[i]);
break comparisonLoop;
}
}
}
}
return arr;
}

Tuesday, May 22, 2007

Javascript for updating one windows from another

window.onload = initWindows;

function initWindows() {
if (document.getElementById("childField")) {
document.getElementById("childField").onchange = updateParent;
}
else {
newWindow = window.open("child.html","newWin","status=yes,width=300,height=300");
}
}

function updateParent() {
opener.document.getElementById("msgLine").value = "Hello " + this.value + "!";
}

Saturday, May 12, 2007

PHP DataTable Displaying and Paging



I have made a function to fetch data from database and show it in a HTML table. It's easy to use but hard to read. HTML text has been linked together.




function getResultAsTable ($actions, $result)
{
if ($rowcount =pg_num_rows($result)>0)
{
$resultHTML= "<table border="'1'" align="'center'">\n<tr bgcolor="'#e8eefa'">\n";

//Output the table header
$fieldCount = pg_num_fields($result);
for ($i=0; $i < $fieldCount; $i++)
{
$rowName = pg_field_name($result,$i);
$resultHTML .= "<th valign="'middle'">$rowName</th>\n";
}

$resultHTML .= "<th>Actions</th></tr>\n";

//output the table body
while ($row = pg_fetch_row($result))
{
$resultHTML .="\n";
for ($i=0; $i < $fieldCount; $i++)
{
$resultHTML .="<td>".htmlentities($row[$i])."</td>\n";
}

//Replace VALUE with proper primary key
$action = str_replace("VALUE", $row[0], $actions);
//Add action cell to the end of each row
$resultHTML .= "<td>$action</td>\n</tr>\n";

}

//Close table
$resultHTML .="</table>\n";
}
else
{
$resultHTML = "No Result Found";
}
return $resultHTML;
}


function pageLinks($totalpages, $currentpage, $pagesize, $parameter)
{
// Start at page one
$page = 1;
// Start at record 0
$recordstart = 0;

// Initialize page links
$pageLinks = '';
while ($page <= $totalpages) {
// Link the page if it isn't the current one
if ($page != $currentpage) {
$pageLinks .= "<a href="http://www.blogger.com/%5C"
parameter="$recordstart\">$page</a> ";
// If the current page, just list the number
}
else {
$pageLinks .= "$page ";
}
// Move to the next record delimiter
$recordstart += $pagesize;
$page++;
}

return $pageLinks;
}

Design Pattern in PHP Architecture

Singleton pattern defines a class that hs only a single global instance. There are an abundance of places where a singleton is a natural choice. A browsing user has only a single set of cookies and has only one profile. Similarly, a class that wraps an HTTP request has only one instance per request. If you use a database driver that does not share connections, you might want to use a singleton to ensure that only a single connection is every open to a given database at a given time.

One successful method for implementing singletons in PHP5 is to use a factory method to create a singleton. The factory method keeps a private reference to the original instance of the class and returns that on request.

class Singleton {
private static $instance = false;
public $property;

private function __construct () {}
public static function genInstance ()
{
if (self :: $instance === false) {
self :: $instance=new Singleton;
}
return self :: $instance;
}
}

$a = Singleton :: getInstance()
$b = Singleton :: getInstance()
$a ->property = "Hello world";

print $b ->property;

//Amazing! The result will be "Hello world"//

In fact, if set the constructor methods private, you can only make instance inside the class, if you try to instantiate outside the class, you will get a fatal error.