Bleadof’s world of tinkering

July 3, 2006

Work and ajax

Filed under: PHP,Programming,Tinkering — Bleadof @ 11:07

Well, I was asked to do a suggesting text-box for one field in the UI so that it would be easier to type in the info which apparently is the one they make most of the errors. I did it with some script.aculo.us love. It’s nice that they have pretty good docs.

So basically what you have to do is include


<head>
...
<script src="scriptaculous/lib/prototype.js" type="text/javascript"></script>
<script src="scriptaculous/src/scriptaculous.js" type="text/javascript"></script>
...
</head>

Then add


<label for="signum">Signum</label>
<input id="signum" name="item[signum]" type="text" />
<div id="signumhint" class="signumhint"></div>
<script type="text/javascript">
// <![CDATA[
new Ajax.Autocompleter("signum","signumhint","AjaxController.php?get=signum", {})
// ]]>
</script>

Then add process the request, this is from my AjaxController.class.php draft


function handleRequest($post, $get) {
if(count($post) <= 0 && count($get) <= 0) {
return null;
}
if($post['item']['signum'] != null) {
$signums = $this->getSignums($post['item']['signum']);
}
$this->makeUnorderedList($signums);
$this->printList($signums);
}
function makeUnorderedList($results) {
$unorderedList = Array();
$unorderedList[] = '<ul>';
foreach($results as $result) {
$unorderedList[] = '<li>'.$result.'</li>';
}
$unorderedList[] = '</ul>';
return $unorderedList;
}
function printList($unorderedListArray) {
foreach($unorderedListArray as $item) {
print $item;
}
}

You also need to edit your css so that the hints div will look nice. It’ll be populated data. In here the hints field css looks like this:


.signumhint {
position: absolute;
font-size: 0.8em;
background-color: white;
left: 8em;
z-index: 100;
}
.signumhint ul {
list-style-type:none;
margin:0px;
padding:0px;
}
.signumhint ul li.selected { background-color: #ffb;}
.signumhint ul li {
list-style-type:none;
display:block;
margin:0;
padding:0;
height:32px;
cursor:pointer;
}

And voila, you’re good to go.

Ajax screenshot

June 12, 2006

PHP4 OOP and references

Filed under: PHP,Programming,Tinkering — Bleadof @ 13:06

So today I decided check PHP4 OOP quirks. So now I know for sure how my code is going to behave. I wasn’t really sure how the PHP4 behaves with references so I did a test.

class ReferenceTest {

  var $reference;

  function ReferenceTest() {
    $this->reference = new Reference();
  }

  function getReference() {
    return $this->reference;
  }

  function &getReferenceReference() {
    return $this->reference;
  }
}

class Reference {

  var $var;

  function Reference() {
    $this->var = "";
  }

  function setVar($var) {
    $this->var = $var;
  }

  function getVar() {
    return $this->var;
  }

  function &getVarReference() {
    return $this->var;
  }

}

$rt =  new ReferenceTest();
$ref = $rt->getReference();
$ref->setVar("bar");
$ref2 =& $rt->getReferenceReference();
$ref2->setVar("baz");
$ref3 =& $rt->getReferenceReference();
$varRef =& $ref3->getVarReference();
$varRef = "baf";
$ref3->setVar("foo");

print "ref:".$ref->getVar()."<br/>";
print "ref2:".$ref2->getVar()."<br/>";
print "ref3:".$ref3->getVar()."<br/>";

Do you know what is the output? Here’s the anwser.

April 27, 2006

PHP4 and error handling…

Filed under: PHP,Programming,Tinkering — Bleadof @ 00:04

I’m working on a project at the moment. Doing stuff for company called Boogie Beat Oy. I’m doing a whole rewrite for their system-of-a-thingie… I decided to start using proper OOP-style. Pretty interesting thing to do with a language which I’ve used only for procedural programming. So I stumbled upon some of the problems which PHP4 has with objects and error handling.

set_error_handler(“yourErrorHandlerFunction”)

I’m used to do objects with Java andJava’s way of handling errors which is quite nice and flexible.

In PHP4 the built in way of raising errors is:

trigger_error("Error message", E_USER_ERROR)

This is not the actual problem but the:


function my_errror_handler($errno, $errstr, $errfile, $errline, $errcontext) {
switch($errno) {
case E_USER_ERROR:
...
}
}
$old_error_handler = set_error_handler("my_error_handler");

Which would be just brilliant if you could say…


$old_error_handler = set_error_handler("$object->errorHandler");

…and when you can’t. You’re basically bound to do like this:


require('Object.class.php');
$object = new Object();
function __errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
global $object;
$object->errorHandler($errno, $errstr, $errfile, $errline, $errcontext);
}
$oldErrorHandler = set_error_handler("__errorHandler");

Which is kind of sucky way of doing things… Basically what I’ve done is that my errorHandler will store all of the errors in an Array and I can just ask for the errors when I need to get them and it’ll return an Array of the errors. So I can easily foreach the errors in a Smarty template and not have to worry about the errors been print out anywhere or anytime they are triggered.

Took me some time to figure out a way to point to the function of this errorHandler class I wrote. This implementation is a hack but it works! It makes it easy to localize the errors if wanted because you can modify the errorHandling as you wish…

Here’s few documents where you can find more information about the error handling in PHP4

Oh well, that’s about it… Probably more about this later…

Powered by WordPress