MySQL query to change character set, connection encoding, collation etc.

August 7th, 2010 by Alex No comments »

This may become handy in case your database and website encoding is a bit different from what the server thought would be perfect.
Simply run this query after successful mysql_connect:

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

Don’t forget to adjust the encoding names according to your configs (because not every website and database uses utf8).

Additionally, it may be possible to fix your encoding bugs with a simple single query (works in every 3rd situation):

mysql_set_charset('utf8');

Performing multiple MySQL queries in PHP

August 7th, 2010 by Alex No comments »

Yes, i mean multiple. Usually this is used when you have for ex. big export file from phpMyAdmin with multiple CREATE TABLEs, INSERTs etc.
I came across the solution provided with some website i can’t remember
Basically, split the queries at the point where there are semicolons and query each split.

$sql = " 
CREATE TABLE IF NOT EXISTS `zcta` ( `zip` char(5) NOT NULL, `city` varchar(64) default NULL, PRIMARY KEY  (`zip`)) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
INSERT INTO `zcta` (`zip`, `city`) VALUES
('00211', 'Portsmouth'),
('00212', 'Portsmouth');
";

Something like that, now we split the string where the ; is and make sure it isn’t the value inside the query (this weird regexp does it all)

$queries = preg_split("/;+(?=([^'|^\\\']*['|\\\'][^'|^\\\']*['|\\\'])*[^'|^\\\']*[^'|^\\\']$)/", $sql); 
foreach ($queries as $query){ 
   if (strlen(trim($query)) > 0) mysql_query($query); 
}

Highlighting paragraphs like table rows with SimpleXML

June 5th, 2010 by Alex 1 comment »

I’ll show you how to highlight every odd or even paragraph with PHP5 and SimpleXML. What do i mean? Let’s say we have this html structure:

<p>Hi, this is the first paragraph</p>
<p>And here's the second one</p>

Now imagine we have 100 of these paragraphs, they are generated dynamically and we need to highlight every even paragraph (i.e. 2nd, 4th, 6th). This can be made very simple if we break it up with SimpleXML.
First, let’s wrap the paragraphs with <xml> tag to make sure we have one top level element (this is the XML requirement)

<xml>
<p>Hi, this is the first paragraph</p>
<p>And here's the second one</p>
</xml>

Now, let’s initialize our SimpleXML object (and let’s silence it with @ to make sure we don’t have any errors visible):

$content = "<xml><p>Hi, this if the first paragraph</p><p>And here's the second one</p></xml>";
$xml = @simplexml_load_string($content);

Now, to iterate through all paragraphs, we’ll use the xpath method of the simplexml object. Let’s not forget to set the counter to 0, we’ll use it to check if the row is even or odd. If we want to highlight odd rows, we need to set the counter to 1.

if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%2).'">'.$p.'</p>';
		$a++;
	}
}

As you see, we use the result of $a%2 to output 0 or 1 and, thus, generate a class for paragraph with row0 or row1 name. The output will be as follows:

<p class='row0'>Hi, this if the first paragraph</p>
<p class='row1'>And here's the second one</p>

As you see, this is very simple. If we want to make 3 different row colors we can iterate like this:

if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%3).'">'.$p.'</p>';
		$a++;
	}
}

This will output row0, row1 and row2 class names. We only need to define the appropriate class names in our stylesheets.

The full source may look like this:

$content = '<xml><p align="center">bla bla</p><div><span><p>bla bla</p></span></div><div><p class="somethingelse">aha</p></div></xml>';
$xml = @simplexml_load_string($content);
if ($xml) {
	$a = 0;
	foreach($xml->xpath('//p') as $p) {
		echo '<p class="row'.($a%2).'">'.$p.'</p>';
		$a++;
	}
}

If you are using WordPress and need to highlight paragraphs inside a post or page, you can write it like this:

$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
$content = '<xml>'.$content.'</xml>';
$xml = @simplexml_load_string($content);
...

Additionally, if we want to get only those paragraphs that have align=’center’ we can run the xpath like this:

foreach($xml->xpath('//p[@align="center"]') as $p)

XPath is a very simple and powerful way to access the DOM of HTML or XML document. Enjoy it :)

Advertisement

Hiqh quality PNG and ICO Japan Icons

June 4th, 2010 by Alex No comments »

An awesome set of high-quality Japanese Icons or Japan theme icons if you wish.

Download from any of the sources below:
RapidShare
LetItBit
DepositFiles

Converting video files to WebM format

May 30th, 2010 by Alex No comments »

For Windows users, the easiest way to convert the video will be to download the WebM DirectShow filters and use the makewebm tool inside the package.

To run the encoding process, simply run the makewebm in command line with the input video parameter:
makewebm myvideo.avi

It will start the process of encoding the video for you. If you receive an error about muxing audio or something like that, then probably you don’t have the Vorbis DirectShow filters installed. To do so, navigate to OGG page and download the codecs from their website.