<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Registers a PHP function for use as an SQL collating function</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sqlite3.createaggregate.html">SQLite3::createAggregate</a></div> <div class="next" style="text-align: right; float: right;"><a href="sqlite3.createfunction.html">SQLite3::createFunction</a></div> <div class="up"><a href="class.sqlite3.html">SQLite3</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="sqlite3.createcollation" class="refentry"> <div class="refnamediv"> <h1 class="refname">SQLite3::createCollation</h1> <p class="verinfo">(PHP 5 >= 5.3.11)</p><p class="refpurpose"><span class="refname">SQLite3::createCollation</span> — <span class="dc-title">Registers a PHP function for use as an SQL collating function</span></p> </div> <div class="refsect1 description" id="refsect1-sqlite3.createcollation-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>SQLite3::createCollation</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span> , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span> )</div> <p class="para rdfs-comment"> Registers a PHP function or user-defined function for use as a collating function within SQL statements. </p> </div> <div class="refsect1 parameters" id="refsect1-sqlite3.createcollation-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">name</code></em></span> <dd> <p class="para"> Name of the SQL collating function to be created or redefined </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">callback</code></em></span> <dd> <p class="para"> The name of a PHP function or user-defined function to apply as a callback, defining the behavior of the collation. It should accept two strings and return as <span class="function"><a href="function.strcmp.html" class="function">strcmp()</a></span> does, i.e. it should return -1, 1, or 0 if the first string sorts before, sorts after, or is equal to the second. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-sqlite3.createcollation-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-sqlite3.createcollation-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2157"> <p><strong>Example #1 <span class="function"><strong>SQLite3::createCollation()</strong></span> example</strong></p> <div class="example-contents"><p> Register the PHP function <span class="function"><a href="function.strnatcmp.html" class="function">strnatcmp()</a></span> as a collating sequence in the SQLite3 database. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">(</span><span style="color: #DD0000">":memory:"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE test (col1 string)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a1')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a10')"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES ('a2')"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">createCollation</span><span style="color: #007700">(</span><span style="color: #DD0000">'NATURAL_CMP'</span><span style="color: #007700">, </span><span style="color: #DD0000">'strnatcmp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$defaultSort </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT col1 FROM test ORDER BY col1"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$naturalSort </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"default:\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$defaultSort</span><span style="color: #007700">-></span><span style="color: #0000BB">fetchArray</span><span style="color: #007700">()){<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'col1'</span><span style="color: #007700">], </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #DD0000">"\nnatural:\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$naturalSort</span><span style="color: #007700">-></span><span style="color: #0000BB">fetchArray</span><span style="color: #007700">()){<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'col1'</span><span style="color: #007700">], </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> default: a1 a10 a2 natural: a1 a2 a10 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-sqlite3.createcollation-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member">The SQLite collation documentation: <a href="http://sqlite.org/datatype3.html#collation" class="link external">» http://sqlite.org/datatype3.html#collation</a></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="sqlite3.createaggregate.html">SQLite3::createAggregate</a></div> <div class="next" style="text-align: right; float: right;"><a href="sqlite3.createfunction.html">SQLite3::createFunction</a></div> <div class="up"><a href="class.sqlite3.html">SQLite3</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>