Archive for the ‘WebDev’ category

How to use Google Charts API in your Secure, HTTPS webpage

November 9th, 2009

So you love Google Charts API and want to spice up your app with lots of pretty bar charts, Venn diagrams, Google-o-meters and 2D bar codes (QR Codes) – only problem is, you require that users connect to your site using a secure connection (https://yoursite.com/whatever). Well, as you are probably aware, the Google Charts API doesn’t ex

actly support SSL connections… directly.

Have no fear! If you are willing to give up a little bit of your own bandwidth, you can work around this problem. We are going to do this using a simple PHP script and if you want to get really fancy, Memcached to keep the redundant API calls to a minimum.

Let’s get started.
» Read more: How to use Google Charts API in your Secure, HTTPS webpage

Memory-tight, multi-threaded PHP Daemon

November 17th, 2008

Came across this nifty project on Google Code. It’s a multi-threaded, object-oriented PHP daemon which is very nicely written and easy to use.

http://code.google.com/p/phpmultithreadeddaemon/

Here’s their example of how to implement it. Everything needed to run the daemon is in the single included file “class.MTDaemon.php” – cool!


<?php

error_reporting(E_ALL);

require_once(include/class.MTDaemon.php);

class MTTest extends MTDaemon {

public function getNext($slot)
{
$this->lock();
$num = $this->getVar("num");
if ($num == null) $num = 1;
if ($num > 100) {
$this->unlock();
return null;
}
$this->unlock();

$rand = rand(0, 5);
echo "Next for slot " . $slot . " : " . $rand . "\n";
if ($rand == 0) return null;
else return $rand;
}

public function run($next, $slot)
{
$rand = rand(3, 10);
$this->lock();
$num = $this->getVar("num");
$this->setVar("num", $this->getVar("num") + 1);
$this->unlock();
echo "## Iteration #" . number_format($num) . " in " . $rand . "sec" . "\n";

sleep($rand);
return 0;
}

}

$mttest = new MTTest(2);
$mttest->handle();

?>

MySQL ON DUPLICATE KEY INSERT

December 18th, 2007

Have you ever wanted to write a single query that would update fields in a table – but you can’t be 100% sure the record exists yet for you to update? For example, you might have a table that holds configuration data for your application. There will be one record for each user in your system. You could use their “UserID” as the primary key (that is crucial to making this work).

Well, instead of doing this:

<?php
$sql = "SELECT COUNT(UserID) FROM configuration WHERE UserID='SomeUser'";
$result = mysqli_query($db,$sql);
if ($result && mysqli_num_rows($result)>0) {
$aResult = mysqli_fetch_array($result);
$iRecordExists = ($aResult[0]>0?1:0);
}

if ($iRecordExists>0) {
//do an update
$sql = "UPDATE configuration SET someField='someValue' WHERE UserID='SomeUser'";
mysqli_query($db,$sql);
}
else {
//do an insert
$sql = "INSERT INTO configuration SET someField='someValue', UserID='SomeUser'";
mysqli_query($db,$sql);
}
?>

You could just do this:


<?php
//insert the user's configuration field - if the record already exists - update instead
$sql = "INSERT INTO configuration SET UserID='SomeUser', someField='someValue' ON DUPLICATE KEY UPDATE someField='someValue' ";
mysqli_query($db,$sql);
?>

Simply put, the query will attempt to insert the configuration record first. If it finds that the specified UserID already has a configuration record in the table, it will simply update the existing record according to the values you include after “ON DUPLICATE KEY UPDATE”. You can include more than one field to update as well.

[Update: As Paul questioned in the comment below, the WHERE clause is not correct (in my original post). The trick is, you have to include the primary key as part of the insert statement - such as UserID in the example above.]

Why I switched to a new IDE for PHP development

November 27th, 2007

I’ve been using Dreamweaver for about 10 years in some form or fashion. It actually started when I purchased Allaire HomeSite 3.0 in 1997. Macromedia purchased Allaire in 2001 and eventually merged HomeSite with their Dreamweaver product. Dreamweaver 8 is what I’ve been using for the past 2 years; that is, until I found PHP nirvana!

I have tried other IDE’s off and on for the past 5 years – trying to get more of a Visual Studio experience. I was really jealous of the code-completion and hinting features that were available in VS. Dreamweaver does ok at basic function hints for PHP, but nothing I found did a great job of combining all the site management features of Dreamweaver with the intelligent code parsing (ie. reading my classes and adding them to the code-completion hints). I’ve tried Eclipse with its PHP plug-in, UltraEdit, Zend Studio, phpDesigner and probably some others that I’m not listing.

PhpED Workspace

» Read more: Why I switched to a new IDE for PHP development

On-the-fly Form Validation with JavaScript Regular Expressions

November 19th, 2007

I needed to check a text input field on a form to allow only specific values as the user typed in the field. The regular expression only allows characters a-z, numbers 0-9, the “dash” symbol and the underscore. Not sure why, but I had to break out the check for a space into a second regex.

Here's the JavaScript function:
<script language="javascript" type="text/javascript">
function testField(oField) {

	var illegalChars = /[^a-zA-Z0-9_-]/;
	var spaceChars = /\s/;
		// allow only letters, numbers, dashes and underscores
	if (illegalChars.test(oPrefix.value) || spaceChars.test(oPrefix.value)) {
		alert("The prefix may only contain letters, numbers, dashes and underscores.");
		oPrefix.value = oPrefix.value.substring(0,oPrefix.value.length-1).toUpperCase();
	}
	else {
		oPrefix.value = oPrefix.value.toUpperCase();
	}

}
</script>

And here's the HTML form that calls the script through the "onKeyUp" action.

<form name='frmTest' method="POST" action="somepage.php">
Text: <input type='text' name='someFieldName' value='' maxlength="5" style="width:65px;"  onkeyup="testField(this)">
</form>