Christophe Verbinnen

Christophe Verbinnen

This user hasn't shared any profile information

Home page: http://www.djpate.com

Jabber/GTalk: djpate@gmail.com

Posts by Christophe Verbinnen

Using rowCount with the sqlite driver of PDO.

0

If you are reading this post, you’re probably facing an issue with sqlite and pdo that prevents you from using the rowCount method on a PDOStatement instance.

Apparently the new API of sqlite does not allow pdo to get this information. At work we faced this issue so I wanted to share it the web :)

In order to do this we need to overwrite the default PDOStatement. To do this we need to start by extending PDO and Using the ATTR_STATEMENT_CLASS flag in order to use our tweak statement class.

PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).

Database.class.php

class Database extends PDO
{
	function __construct(string $dsn,$username = null,$password = null,array $driver_options = null)
	{
		parent::__construct($dsn, $username, $password, $driver_options);

		if($this->getAttribute(PDO::ATTR_DRIVER_NAME) == "sqlite") {
			$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array( 'Statement', array( $this )));
		}
	}

}

Statement.class.php

class Statement extends PDOStatement
{

	/**
	 *
	 * @var \Pdo
	 */
	private $_pdo;

	/**
	 *
	 * Constructor
	 *
	 * @param \Pdo $pdo
	 */
	protected function __construct($pdo)
	{
		$this->_pdo = $pdo;
	}

	/**
	 * Returns the number of rows affected by the last SQL statement
	 *
	 * @link http://www.php.net/manual/en/pdostatement.rowcount.php
	 * @return int the number of rows.
	 */
	public function rowCount()
	{
		// In case of sqllite, we don't have the row count
		// So we have to re run the statement in order to get the row count
		// And to avoid to move the current cursor
		$statement = $this->_pdo->query($this->queryString);
	        return count($statement->fetchAll());
	}

}

As you can see we use the count() function on the sqlite workaround, using this on very large dataset can be a really bad idea. We could create a count query here instead but It would’nt work on all query(complex group by’s etc..). If you are only using simple queries you can rewrite the query on the go to issue a count instead (if you need inspiration ).

Since we check for the driver on the PDO constructor will not be used on other drivers than sqlite.

Have fun.

Share

Related Posts:

How to generate a word docx from a template with PHP ?

21

Hey folks,

A lot of clients of mine asked for docx generation in the past.

The only solution that I knew of in PHP is the SOAP client phplivedocx.

first of all It was kinda slow and I didn’t want to relly on a service and more importantly It was missing the “nested block” feature.

So I decided to create my own class !

Have fun & fork me if you need another feature !

http://github.com/djpate/docxgen

DOCXGEN

Features

  • Create valid docx based on a template
  • Nested blocks on infinite levels

How to create your template

Simply open up word 2007+

If you want to map a single field you can just use #NAME# but you could use anything you like since it’s just a search & replace

To create a block

[start blockname] your content [start somenestedblock] [end somenestedblock] [end blockname] 
  • blockname should be unique
  • blockname has to match w+ reg ex (a-zA-Z_)
  • fields mapped in block has to be unique

Please check out full template in the repo

How to setup

require("phpDocx.php"); $phpdocx = new phpdocx("mytemplate.docx"); 

How to assign values

$phpdocx->assign("#TITLE1#","Hello !"); // basic field mapping $phpdocx->assignBlock("members",array(array("#NAME#"=>"John","#SURNAME#"=>"DOE"),array("#NAME#"=>"Jane","#SURNAME#"=>"DOE"))); // this would replicate two members block with the associated values $phpdocx->assignNestedBlock("pets",array("#PETNAME#"=>"Rex"),array("members"=>1)); // would create a block pets for john doe with the name rex $phpdocx->assignNestedBlock("pets",array("#PETNAME#"=>"Rox"),array("members"=>2)); // would create a block pets for jane doe with the name rox $phpdocx->assignNestedBlock("toys",array("#TOYNAME#"=>"Ball"),array("members"=>1,"pets"=>1)); // would create a block toy for rex $phpdocx->assignNestedBlock("toys",array("#TOYNAME#"=>"Frisbee"),array("members"=>2,"pets"=>1)); // would create a block toy for rox 

How to save

$phpdocx->save("somefile.docx"); 

More info

Why this pclzip ?

I’m using pclzip for the zip process because the zip utility provided with php can cause issue with office

What’s the licence ?

GPL

Anything else ?

I’m using three function from the TBS library so congrats to them

Share

Related Posts:

How to compile Spawner the Sql data generator on Ubuntu

1

A little while ago I discovered spawner.

Spawner is a generator of sample/test data for databases. It can be configured to output delimited text or SQL insert statements. It can also insert directly into a MySQL 5.x database. Includes many field types, most of which are configurable

The problem is that the binary that’s available on sourceforge didnt work for me anymore so I decided to compile it myself so I decided to give out some pointers…

first of get the source via SVN

svn co https://spawner.svn.sourceforge.net/svnroot/spawner spawner

now install lazarus & some libs

sudo apt-get install lazarus-ide fpc-source fp-units-i386

Now launch lazarus & open up the program.lpr file from trunk.

Now just click on Run -> Compile All

You’ll find the binary in the bin directory in trunk.

Have fun !

Share

Related Posts:

  • No Related Posts

How to make cross-domain ajax request using jsonp & jquery

1

As you may know AJAX request cant be done across domain (even across subdomain). This is a security feature but sometimes It can pretty usefull to make call between domains.

Luckily for us AJAX allows us to make cross domain request for javascript contents. That is how JSONP works, basicly instead of passing plain json you pass json inside a javascript function.

I’m going to show you how you can do this very easily using jQuery.

first of all make the call using jquery.

$.getJSON("http://someurl/somepage.php?jsoncallback=?",function(data){
  alert(data.msg);
});

As you can see we have to set jsoncallback=? in the url for this to work.

Now In your php file.

<?php
	$array = array("msg"=>"hello"); //some bogus data
	echo $_REQUEST['jsoncallback']."(".json_encode($array).")";
?>

The output will be something like :

jquery65456456456({“msg”:”hello”})

As you can see It now looks like a javascript function.

That’s all there is too it !

Share

Related Posts:

How to setup Xdebug on ubuntu 10.10

2

Hello,

I’ve been trying to figure out how to actually set up Xdebug on my php configuration but I never managed to get the “Styled” version of Xdebug.

I finally figured out why so I decided to share it :)

First install xdebug with pecl

sudo pecl install xdebug

Then create a xdebug config file and add a link to your .so file

sudo nano /etc/php5/apache/conf.d/xdebug.ini # then add zend_extension = /usr/lib/php5/20090626+lfs/xdebug.so

Now here is the part that I couldn’t figure out.

In your php.ini file you need to enable display_errors & html_errors.

sudo nano /etc/php5/apache/php.ini # then look for display_errors & html_errors and set it to On.

finally restart apache

sudo /etc/init.d/apache2 restart

You’re all set !

Share

Related Posts:

jTag : A jQuery plugin to tag pictures & more !

57

Howdy,

A little while ago I published an article on how to create a facebook like tagging system.
Even thought It worked fine it was a bit messy and kinda hard to impletement.

I then started to work on a jQuery plugin that make that feature a lot easier to integrate on your solutions.

What does it look like ?

Click on the picture to start tagging

I made more demos to explain all the options of the script.

Where do I get it ?

You can get the source code on github or git clone git://github.com/djpate/jTag.git

(more…)

Share

Related Posts:

How to write a complete OAuth Provider in PHP5

73

Today I’m going to talk about a subject that doesn’t have a lot of coverage on the web.

There is a lot of tutorials about how to use OAuth authentication for twitter services or google & such but there is not a lot of information on how to actually make your own oauth provider.

Now if you are reading this post you probably allready know why Oauth is good and what his purpose is but you can check out some info here.

On this post I’m going to use an basic example case and we will go from nothing to actually handling api calls.

You can download the full source code of a working provider from here. It includes the provider described here as well as a basic client to help you test it out…

(more…)

Share

Related Posts:

Animated scrollto effect jQuery plugin

41

Howdy,

Since my post about this effect has a lot of succes I decided to make a little plugin to simplify it even more.

First of all go check out the Demo.

(more…)

Share

Related Posts:

gmail, yahoo & live messenger contact inviter script

20

Howdy,

you guys have probably noticed that most of the contact inviter script out there were broken now because most of the new api’s only support Oauth protocol instead of the good ol’ login / password authorization.

It’s great because now you don’t have to give the those website your login information but It is a bit more complicated for us developers to set it up so I’ve decided to create a small set of classes that will allow you to easily implement such a feature on your website.

First off all, you can check out the simple proof of concept by checking out the demo here and getting the script on gitHub here.

Some people who may know my blog will notice that I am in no way a designer so It doesnt look amazing but it works and that’s what this article is about ! ( if someone feels like he could make it look better you can get the code from github. see at the end of the article for more info).

Ok so now the fun stuff.
(more…)

Share

Related Posts:

Using JQuery Validation plugin with tooltips

2

Today I’ll share with you a little tweak that took me a while to figure out.

I wanted to tweak the excelent jQuery Validation plugin to display errors in tooltips.

Here is my solution using Qtip

DEMO

So first off you got to setup Qtip and the validation plugin on your website.

then you have to use the *undocumented* errorPlacement option on the validation plugin

jQuery.validator.setDefaults({
    errorPlacement: function(error, placement) {
		$(placement).qtip({
			content: error.text(),
			show: { when: { event: 'none'}, ready: true },
			hide: { when: { event: 'keydown' } },
			position: {
		      corner: {
				 target: 'topRight',
		         tooltip: 'bottomLeft'
		      }
		   },
		   style: {
			  border: {
				 width: 1,
				 radius: 10
			  },
			  tip: true,
		      name: 'red'
		   }
		});
	}
});

Of course you can modify this to use another tooltip system…

Have fun !

Share

Related Posts:

Christophe Verbinnen's RSS Feed
Go to Top