Computer


13
May 11

Frigging Huge PDFs

Turns out this is how you fix up a frigging huge pdf that probably came out of photoshop or some such beast with all of the layers and history that is mostly useless to you. It seems to work quite well, 230 MB down to 6MB.

Of course you can do some tweaking to reduce the size even more if you want to (take a look at the ghost script manual).

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf


26
Jan 11

PHP SQL Query Builder

As an excersise I have started building another php based sql query builder (separate from my stagnating php framework). So far I can do SELECT [columns] FROM table WHERE [conditions]

This is how it should be used (so far):
(for the examples, assume that $q is a query object)

$q->table('test')
	->column('test_2', 'test')
	->table('test_3', 'test_2');
//=> SELECT test_2 as test FROM test, test_3 AS test_2

You can also group conditions:

$q->table('table')
	->begin_and()
	->and_where('col_1', 1)
	->or_where('col_2', 2)
	->end_and()
	->or_where('col_3', 3, '!=');
 
//=>SELECT * FROM table WHERE ( col_1 = '1'  OR col_2 = '2' ) or col_3 != '3'

which is fun.

I have just tried this example and am pleased to say that you can put multiple groups at the start of the query:

$q->table('table')
	->begin_and()
	->begin_and()
	->and_where('col_1', 1)
	->or_where('col_2', 2)
	->end_and()
	->end_and()
	->or_where('col_3', 3, '!=');
 
//=>SELECT * FROM table WHERE ( ( col_1 = '1' OR col_2 = '2' ) ) OR col_3 != '3'

You can follow along here if you want, suggest improvements, criticize it to no end because I am doing it toally wrong, or “steal” my ideas and make your own (but give me some of the credit at least).


10
Mar 10

Using the jQuery-UI Autocomplete Widget

The latest version of jQuery-ui has an autocomplete widget. No more looking for one that will work (you get the point) with the version of jQuery that you have.

All you have to do is pull the latest one down from the jQuery-ui site and you are good to go.

First the jQuery to use the autocomplete plugin:

 
//we will be using this to cache the responses from the server
var ajaxCache = {};
 
//activate autocomplete on boxes that have the autocomplete class
$("input.auto_complete").autocomplete({
	source: function(request, response) {
		//what are we searching for
		var query_type = $(this).attr('element').attr('id');
		//the cacheterm that we use to save it in the cache
		var cachedTerm = (request.term + '' + query_type) . toLowerCase();
		//if the data is in the cache and the data is not too long, use it
		if (ajaxCache[cachedTerm] != undefined && ajaxCache[cachedTerm].length < 13) {
			//map the data into a response that will be understood by the autocomplete widget
			response($.map(ajaxCache[cachedTerm], function(item) {
				return {
					label: item.value,
					value: item.value
				}
			}));
		}
		//get the data from the server
		else {
			$.ajax({
				url: "/ajax/auto_complete.php",
				dataType: "json",
				data: {
					query_type: query_type,
					q: request.term
				},
				success: function(data) {
					//cache the data for later
					ajaxCache[cachedTerm] = data;
					//map the data into a response that will be understood by the autocomplete widget
					response($.map(data, function(item) {
						return {
							label: item.value,
							value: item.value
						}
					}));
				}
			});
		}
	},
	//start looking at 3 characters because mysql's limit is 4
	minLength: 3,
	//when you have selected something
	select: function(event, ui) {
		//close the drop down
		this.close
	},
	//show the drop down
	open: function() {
		$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
	},
	//close the drop down
	close: function() {
		$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
	}
});

The ajax/auto_complete.php would look something like this:

 
<?php
 
//make sure that we only allow valid query types
/**
 * @var array
 */
$validQueryTypes = array(
	'country',
	'city',
	'first_name',
	'last_name',
);
/**
 * @var string
 */
$column = (isset($_POST['query_type']) && in_array($_POST['query_type'], $validQueryTypes))? $_POST['query_type'] : null;
/**
 * @var string
 */
$q = isset($_POST['q'])? $_POST['q'].'%':null;
 
 
if($column && $q){
	switch($column){
		case 'country':
		case 'city':
			$q = new sQuery();
			$results = $q->from('addresses')
				//to make things simpler for the javascript, always select as value
				->column($column . ' as value')
				->where($column, $q, 'LIKE')
				->getAll();
 
			//SELECT $column as value FROM addresses WHERE $column LIKE '$q%';
			break;
		case 'first_name':
		case 'last_name':
			$q = new sQuery();
			$results = $q->from('users')
				->column($column . ' as value')
				->where($column, $q, 'LIKE')
				->getAll();
			//SELECT $column as value FROM users WHERE $column LIKE '$q%';
			break;
	}
 
	//$result is something like this:
	array(
		array('value'=>'Canada'),
		array('value'=>'America'),
		array('value'=>'Mexico'),
		array('value'=>'Netherlands'),
	);
 
	//then return it to the javascript
	echo json_encode($results);
}
exit;

Then the simplest part of the exercise:

 
City:
<input type="text" name="city" id="city" title="enter a city" class="auto_complete" />
 
Country:
<input type="text" name="country" id="country" title="enter a country" class="auto_complete" />
 
First Name:
<input type="text" name="first_name" id="first_name" title="enter a first_name" class="auto_complete" />
 
Last Name:
<input type="text" name="last_name" id="last_name" title="enter a last_name" class="auto_complete" />

And if they don’t have javascript enabled it doesn’t detract from the form (go progressive enhancement!).

Edit:

This post is pretty old, but just in-case you were wondering sQuery is this: https://github.com/SeanJA/ShoestringPHP/blob/master/library/classes/squery.class.php and there is also a better tested more standalone-y version here as a separate project: https://github.com/SeanJA/query-builder.


9
Apr 09

Smarty… again

Smarty again.To those who think that smarty prevents you in any way from writting your business logic into a template… watch this…

 
{php}
 
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}
 
if (!mysql_select_db('mysql_dbname', $link)) {
    echo 'Could not select database';
    exit;
}
 
$sql    = '"INSERT INTO unsafe_tabel (unsafe_var) VALUES('".$_GET['unsafe']."')";
 
$result = mysql_query($sql, $link);
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
 
{/php}

I think I might have just put stuff that actually belongs in the model into the view… and unescaped too… oh dear… (that would be template for you smarty folks). So, no, smarty does not disuade you from doing it, infact it seems that they encourage it a bit by providing you with the option to do so via the {php} {/php} tags, instead of forcing you to use {rdelim} {ldelim} all throughout your code instead (which would surely discourage most people). If I can do that, the people who’s site’s you’ve written can surely do that too… and mess up the nice templates that you have made for them (and their databases too). So, please think of another reason to use Smarty, and if you cannot, then why are you using it?


7
Apr 09

Why I use NetBeans

I am sure you know by now that IBM was set to aquire Sun Microsystems this week. This would have been bad seeing as most of their products directly compete/overlap with eachother. Fortunately they backed off at the last minute (or at least they did not offer enough money for them). While this might be a barganing tactic I surely hope that this is not the case. If they had bought them, it seems to me that it would have meant the death of many of the great Sun products.

NetBeans is one that would have been dead really quickly, sure it would have survived as a fork of the code (since Open Source projects never really die) but it would have been making much slower progress than it does now. This is because of Eclipse which is a good product in its own right… but as someone put it:

NetBeans is the Mac of the IDE world… it just works

If you want to use php in eclipse you have to work at setting it up, if you want to develop in php, download the php package, ruby? There is one  for that too. Since they are all professionally developed as part of the package there is no need to work to get it going no need to route aroud the net trying to find which plugins you need, which ones are still supported (which ones are supported well), which ones do not conflict with eachother… You want to use SVN? Which one do you use? Subclipse? Subversive? Does it work with your version of eclipse? Your repository? Will it corrupt it? Does it work with the latest version of subversion? Can it use the command line version if it doesn’t? (On a side note, subclipse probably works fine since it is supported by tigris).

So, if you have a choice at work, try it, if not, try it at home. It even has an eclipse project importer. You have nothing to lose, it is free after all.