Posts tagged PHP

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 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:

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:

Dealing with Monster.com SOAP with PHP5

0

Recently I had to set up a system that would post a job offer to monster.com web service using SOAP.

Once I made the script to generate the XML I started looking into the SOAP class that PHP5 provides but I soon started to feel the headache comming !

So I justed wanted to help out If some of you were facing the same issue : FORGET that soap class and use a basic CURL !

Here is my code snippet

$headers=array("Accept: text/xml","Content-Type: text/xml");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://208.71.198.74:8443/bgwBroker");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // your soap xml that you generated earlier
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$return = curl_exec($curl);
curl_close ($curl);

And there you go ! you just have to handle the return info.

I hope that this will help someone one day !

Share

Related Posts:

How to add image tagging the facebook way !

34

Updated post here

Today I want to show you how to make a image tagging function on your website.

It works about the same way than facebook does (or a least the way it worked some times ago because I deleted my facebook account)

Basicly you take an image and you “tag” people or stuff in it by marking what you want to tag with a rectangle.

This example is just a proof of concept and will give you a good idea on how to make it better for your website.

This script requires jquery and jquery-ui with draggable and resizable function(it’s included in the source).

note : I didnt have time to test it on IE so let me know if it works for you…

Here is the Demo and the Source.

(more…)

Share

Related Posts:

How to : Create a simple url shortener script is a few minutes

27

Hello all,

First let’s explain the basics of what we are trying to do.
We’ll build a script that will replace any type of url to a tiny url so it’s easier to post or be untraceable.

For example this *long* google maps url

http://maps.google.fr/maps?f=q&source=s_q&hl=fr&geocode=&q=white+house&sll=46.75984,1.738281&sspn=10.178118,28.54248&ie=UTF8&z=15&iwloc=A

could become

http://your.site/v/12345

you can check it out here : http://www.djpate.com/portfolio/shortURL/create.php

and download the source code here

This is not very hard to do but it’s nice tutorial for beginners.

(more…)

Share

Related Posts:

How to : Pdf thumbnails with php

0

Hello,

I’ve just worked on something for a project of mine and I wanted to share this code with you.

It’s nothing new but good to know…

(more…)

Share

Related Posts:

form submit via Hidden iframe (A.K.A Fake Ajax)

0

Today, I wanna share with you a way to submit forms using a ajax look & feel altought it’s not really ajax.

The question you might ask is :

why should I use this method instead of real ajax ?

Well they’re is numereous answer to that but one good reason it that is doesnt require any type of ajax libs and you can start using it even if you never used ajax before.

(more…)

Share

Related Posts:

Mysql Class for php5 using singleton

2

Hello again,

I thought i’ll share another one of my little personal scripts with you guys.

Here is the mysql class I use on most of my project, nothing fancy but does the work for what I need most of the time.

(more…)

Share

Related Posts:

Go to Top