Friday, July 31, 2009

numbers from 001 to 999

the numbers from 001 to 999

echo "<?php for(\$i=1; \$i<=999; \$i++){ echo sprintf('%03d',\$i).chr(10);} ?>"|php


The \ has to go before the $ character because $i can refer to the command line environment variable $i, so we escape it with \$i in order to pass it straight through to the php parser.

Thursday, July 30, 2009

output formatting like command line sql

--usage1----------

$sql = '';
$sql.="SELECT fieldtype, count(1) as count ";
$sql.="FROM mytable ";
$sql.="GROUP BY fieldtype ";
$sql.="ORDER BY fieldtype ";
echo report_append("summary of data", $sql);

--output1---------
|fieldtype|count|
|0 |9 |
|1 |397 |
|2 |965 |
|3 |554 |
|4 |683 |
|5 |1352 |
|6 |1390 |
|7 |237 |
|8 |1372 |


--usage2----------
$q='';
$q.="SELECT
(case
when fieldtype=1 then 'Apple'
when fieldtype=2 then 'Banana'
when fieldtype=3 then 'Blackberry'
when fieldtype=4 then 'Cherry'
when fieldtype=5 then 'Grape'
when fieldtype=6 then 'Orange'
when fieldtype=7 then 'Peach'
when fieldtype=8 then 'Pear'
else '' end) as typename, fieldtype, count(1) as count
FROM mytable d GROUP BY fieldtype ORDER by fieldtyped;
";
report_append("summary of data", $q);

--output2---------
|typename  |fieldtype|count|
| |0 |9 |
|Apple |1 |397 |
|Banana |2 |965 |
|Blackberry|3 |554 |
|Cherry |4 |683 |
|Grape |5 |1352 |
|Orange |6 |1390 |
|Peach |7 |237 |
|Pear |8 |1372 |

--code------------

function select_all($sql)

{
$rows = array();
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$rows[] = $row;
return $rows;
}
function report_append($title,$sql)
{

$rows = select_all($sql);

$headersize=array();
$header=array();
foreach($rows[0] as $colname=>$col)
$header[]=$colname;
foreach($rows[0] as $colname=>$col)
$headersize[] = strlen($colname);
foreach($rows as $row)
foreach($headersize as $colnum=>$col)
$headersize[$colnum] = max( $headersize[$colnum] , strlen($row[$header[$colnum]]) );

$h='';
foreach($header as $colnum=>$colname)
$h.="|".sprintf("%-".$headersize[$colnum]."s", $colname);
$h.= "|\n";
foreach($rows as $row)
{
foreach($headersize as $colnum=>$col)
$h.="|".sprintf("%-".$col."s", $row[$header[$colnum]]);
$h.= "|\n";
}

$return='';
$return.="---------------------------------------------------------\n";
$return.="$title\n";
$return.="---------------------------------------------------------\n";
$return.=$h;
return $return;
}

Monday, July 20, 2009

Crontab daily gui app launcher, and taskkill

10 17 * * 1-5 killall pidgin
8 10 * * 1-5 if ! ps -C pidgin > /dev/null; then export DISPLAY=:0 && pidgin; fi

Friday, July 10, 2009

common pitfalls when going from html to xhtml

common pitfalls when going from html to xhtml
-------------------------------------------------
<img /><br /><input /> #good, bad: <img><br><input>
<i><b>hi</b></i> #good, bad: <b><i>hi</b></i>
<a href=""></a> #good, bad: <a href=''></a>
<a href="a=b&amp;c=d"></a> #good, bad: <a href="a=b&c=d"></a>
&#160; #good, bad: &nbsp;