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.

Thursday, May 3, 2007

PHP and CURL (make post between two sites more secure)

We can also use CURL to post data to a form. The default method of sending form data with CURL is in GET, but in this example we will use POST to submit a search term to an imaginary form.

[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";

// Initialize the CURL library
$cURL = curl_init($cURL);

// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);

// Execute, saving results in a variable
$strPage = curl_exec();

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?>[/php]