Alan’s blog

December 22, 2009

Barcode reader

Filed under: Uncategorized — alan @ 6:40 pm

An example web page using the pic2shop barcode reading interface.

If you have an iPhone try it out at:

http://www.meandeviation.com/test/iphone/barcode.php
<?php
$code = $_REQUEST['ean'];
echo '<' . '?xml version="1.0" encoding="UTF-8" ?' . '>';
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test barcode reader</title>
</head>
<body>
<div>
<h1>test barcode reader</h1>
<?php
 if ( $code ) :
?>
<p>
The barcode was <?php echo htmlentities( $code ); ?>
</p>
<?php
 endif;
?>
<p>
<a href="pic2shop://scan?callback=http://www.meandeviation.com/test/iphone/barcode.php?barcode=EAN">read barcode</a>
</p>
<p>
This web page is for iPhone users only and needs
<a href=""http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=308740640&mt=8">pic2shop</a>
installed (it is free), which allows third party web apps and iPhone apps to use its bar code reading software as a form of local web service.
</p>
</div>
</body>
</html>

July 26, 2009

fix for Wordpress shortcode bug

Filed under: academic, web development — alan @ 3:56 pm

I’m starting to use shortcodes heavily in WordPress1 as we are using it internally on the DEPtH project to coordinate our new TouchIT book.  There was minor bug which meant that HTML tags came out unbalanced (e.g. “<p></div></p”).

I’ve just been fixing it and posting a patch2, interestingly the bug was partly due to the fact that back-references in regular expressions count from the beginning of the regular expression, making it impossible to use them if the expression may be ‘glued’ into a larger one … lack of referential transparency!

For anyone having similar problems, full details and patch below (all WP and PHP techie stuff).

(more…)


  1. see section “using dynamic binding” in What’s wrong with dynamic binding? [back]
  2. TRAC ticket #10490 [back]

Sorter

Filed under: Uncategorized — alan @ 6:36 am

The Sorter class is made for sorting records where each record is an associative array.  For example, suppose $files is an array of the following form :

$files[] = array( 'name' => 'fred.txt', 'ext' => 'txt', 'size' => 1234 );$files[] = array( 'name' => 'jane.jpg', 'ext' => 'jpg', 'size' => 412 );$files[] = array( 'name' => 'readme.txt', 'ext' => 'txt', 'size' => 592 );

You can sort by extension type and size as follows:

$sorter = new Sorter("ext,-size:n");    // sort by extension (alphabetic) and descending size$sorter->sort($files);

Basically it takes a comma separated list of field names (e.g. "region,city,area"), each of which can optionally be preceded by a “+” (default) or “-” to say whether it is an ascending or descending sort and suffixed with a type letter (e.g “:n”) to say whether the sort is alphabetic (”s” and default), numeric (”n”) or by date (”d”).  The date has to be in a format PHP recognises, but simple timestamps are numeric.

I think this should also work for arrays of numerically indexed arrays, so that "3,-2:d" sorts by column 3 and then column 2 as a date descending, but I’ve not tested this.

the code

class Sorter {
  var $keys;
  function Sorter($keyspec) {
      $this->keys = $this->parseKeys($keyspec);
   }
   function sort( &amp;$arr ) {
      return usort( $arr, array($this,'compare') );
   }
   function parseKeys($keyspec) {
      $keyspecs = explode(",",$keyspec);
      $keys = array();
      foreach ( $keyspecs as $onespec ) {
         if ( $onespec{0} == '-' ) {
            $onespec = substr($onespec,1);
            $dir = '-';
         } else if ( $onespec{0} == '+' ) {
            $onespec = substr($onespec,1);
            $dir = '+';
         } else {
            $dir = '+';
         }
         $parts = explode(':',$onespec);
         if ( $parts[0] ) {
            switch ( $parts[1] ) {
               case 'i': case 'n':
                  $type = 'n';  break;  // number
               case 'd':
                  $type = 'd';  break;  // date
               case 's': default:
                  $type = 's';  break;  // string
            }
            $keys[] = array( $parts[0], $dir, $type );
         }
      }
      return $keys;
   }
   function compare( $obj1, $obj2 ) {
      foreach( $this->keys as $key ) {
         list( $name, $dir, $type ) = $key;
         $fld1 = $obj1[$name];  $fld2 = $obj2[$name];
         switch ( $type ) {
            case 'n':
               $cmp = $fld1 - $fld2;
               break;
            case 'd':
               $time1 = strtotime($fld1);
               $time2 = strtotime($fld2);
               if ( $time1 === false ||  $time1 === -1 ||
                    $time2 === false ||  $time2 === -1 ) {
                        // false PHP 5.1, -1 older PHP
                  $cmp = 0;    // badly formed dates
               } else {
                  $cmp = $time1 - $time2;
               }
            case 's': default:
               $cmp = strcmp($fld1,$fld2);
               break;
         }
         if ( $cmp == 0 ) continue;
         if ( $dir == '-' ) $cmp = - $cmp;
         return $cmp;
      }
      return 0;
   }
}

July 20, 2009

What’s wrong with dynamic binding?

Filed under: academic, web development — alan @ 8:14 pm

Dynamic scoping/binding of variables has a bad name, rather like GOTO and other remnants of the Bad Old Days before Structured Programming saved us all1.  But there are times when dynamic binding is useful and looking around it is very common in web scripting languages, event propagation, meta-level programming, and document styles.

So is it really so bad?

(more…)


  1. Strangely also the days when major advances in substance seemed to be more important than minor advances in nomenclature [back]

July 3, 2009

a simple PHP record sorter class

Filed under: academic, web development — alan @ 11:51 am

Not for the first time I needed to sort arrays of arrays in PHP (structures like tiny DB tables).  I have previously written little wrapper functions round usort, but decided this time to make a small class. It is a simple, but generic utility, so popping it up in case useful to anyone.

The rest of this post has moved to a permanent page at:

http://www.alandix.com/blog/code/sorter/

Powered by WordPress