<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xml:base="http://labs.boulevart.be">
<channel>
 <title>Boulevart Labs</title>
 <link>http://labs.boulevart.be</link>
 <description />
 <language>en</language>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/feedburner/GQhJ" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="feedburner/gqhj" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
 <title>PushState AJAX with PJAX and jQuery</title>
 <link>http://labs.boulevart.be/content/pushstate-ajax-pjax-and-jquery</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even" property="content:encoded"&gt;&lt;p&gt;PJAX or PushState AJAX is a small script that allows you to load HTML content without a full page reload. It's purely created for a better browsing experience. The source and a working example can be found at: &lt;a href="https://github.com/defunkt/jquery-pjax"&gt;https://github.com/defunkt/jquery-pjax&lt;/a&gt; The beauty is that it updates the browser location field - thus allowing you to navigate back and forth through your history while it's actually doing nothing more than AJAX requests. Which means fewer resources need to be (re)loaded in the browser. A demo is available at &lt;a href="http://pjax.heroku.com/"&gt;http://pjax.heroku.com/&lt;/a&gt; If you want PJAX to work with PHP, you'll need to take a look at the request headers that are sent. ("pjax on the server side") You can easily do this with &lt;code&gt;apache_request_headers()&lt;/code&gt;. This function call returns an array of all request parameters. Now it's just a matter of looking for the &lt;code&gt;X-PJAX&lt;/code&gt; header. You could verify that it's set to true but since the header is only sent when doing an AJAX request with PJAX, you can just check if it's set: &lt;code&gt;$reqHeaders = apache_request_headers();&lt;/code&gt;&lt;code&gt;if (isset($reqHeaders['X-PJAX'])) &lt;/code&gt;&lt;code&gt;  // do something here to only output the actual page content&lt;/code&gt;&lt;code&gt;else&lt;/code&gt;&lt;code&gt;  // do something here to output all HTML&lt;/code&gt; Enjoy your PushState AJAX browsing experience with jQuery, PJAX and PHP.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-category field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/ajax" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;Ajax&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/tags/ajax-php-javascript" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;ajax php javascript&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 29 Nov 2011 09:54:38 +0000</pubDate>
 <dc:creator>Tom</dc:creator>
 <guid isPermaLink="false">12 at http://labs.boulevart.be</guid>
 <comments>http://labs.boulevart.be/content/pushstate-ajax-pjax-and-jquery#comments</comments>
</item>
<item>
 <title>oEmbed &amp; PHP Basic Introduction</title>
 <link>http://labs.boulevart.be/content/oembed-php-basic-introduction</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even" property="content:encoded"&gt;&lt;p&gt;oEmbed is a format for allowing embedded content off a URL on third party sites. This simple API allows a website to display embedded content from for example Youtube. So you can only use the url of a Youtube video and get the video player back, it's that simple.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;No parsing of html is needed because you'll get the embedded code back from Youtube, so this should be OK, in many cases you only store the url in your db.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now we'll get the video player of a random video from Youtube, let's take this one: &lt;a href="http://www.youtube.com/watch?v=THE_hhk1Gzc"&gt;http://www.youtube.com/watch?v=THE_hhk1Gzc&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This is the entire code:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;        $url = "http://www.youtube.com/watch?v=THE_hhk1Gzc";&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;        $encoded_url = "http://www.youtube.com/oembed?url=".rawurlencode($url)."&amp;amp;format=json";&lt;br /&gt;
        $curl = curl_init($encoded_url);&lt;br /&gt;
       &lt;br /&gt;
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;
        $return = curl_exec($curl);&lt;br /&gt;
        curl_close($curl);&lt;br /&gt;
       &lt;br /&gt;
        $json_content = json_decode($return, true);&lt;br /&gt;
       &lt;br /&gt;
        echo $json_content['html']&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Let's break it down:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;        $encoded_url = "http://www.youtube.com/oembed?url=".rawurlencode($url)."&amp;amp;format=json";&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So here we define the url to call filling the url &amp;amp; format parameter with the needed url and format. So url being: "&lt;a href="http://www.youtube.com/watch?v=THE_hhk1Gzc&amp;quot;"&gt;http://www.youtube.com/watch?v=THE_hhk1Gzc"&lt;/a&gt; and the format "json".&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;        $curl = curl_init($encoded_url);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;
        $return = curl_exec($curl);&lt;br /&gt;
        curl_close($curl);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Next we initialise curl, because we'll use curl to do the call, we execute the call and store the returned data in a variable and close the connection.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;       $json_content = json_decode($return, true);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Next we decode the returned data, since it's json format we use the json_decode function. And finally we print out the player.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;        echo $json_content['html']&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This is a basic usage off oEmbed with PHP, off course it can be much more secure than this, but this a basic introduction and can be used for several services like Vimeo, Dailymotion, Blip.tv, Metacafé, Soundcloud and many more.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;More information about oEmbed can be found &lt;a href="http://oembed.com/" target="_blank"&gt;http://oembed.com/&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-category field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/tags/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;php&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/tags/json" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;json&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/tags/curl" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;curl&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/tags/oembed" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;oembed&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Sun, 12 Jun 2011 17:28:07 +0000</pubDate>
 <dc:creator>Avril</dc:creator>
 <guid isPermaLink="false">11 at http://labs.boulevart.be</guid>
 <comments>http://labs.boulevart.be/content/oembed-php-basic-introduction#comments</comments>
</item>
<item>
 <title>Save images from Url</title>
 <link>http://labs.boulevart.be/content/save-images-url</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even" property="content:encoded"&gt;&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;
&lt;div class="field-items"&gt;
&lt;div class="field-item even"&gt;
&lt;p&gt;Using Curl you can easily get a picture from an url. This technique isn't used very often but when you combine it with the new socialgraph API from facebook you can very easily create interactive facebook based applications. (example: a personalized holiday card with a facebook picture from your friends)&lt;/p&gt;
&lt;p&gt;You can get any facebook profile picture with a simple url call. So you can grab the profile picture from the url, save it on your server and attach it to a mail,video, flash,... . And you've got your personlized holidaycard.&lt;/p&gt;
&lt;p&gt;First we grab the facebook profile picture. Replace 560940 with the facebook profile Id of your choice.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;https://graph.facebook.com/560940/picture&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Now that we've got the picture we can save it locally with Curl. We can use the following code to loop trough an array $img containing the picture_id and the picture_url. The other parameter $fullpath is the path where you want to save the image. We loop the array with foreach, create a filename based on the picture_id and write the file&lt;/p&gt;
&lt;p&gt;&lt;code&gt;function save_picture($img,$fullpath){&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;    foreach($img as $key=&amp;gt;$value ){&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;    /* Generate a filename */&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;    $filename = $fullpath ."/".$key.'.jpg';&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;    /* Save picture in map with Curl */&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;    $ch = curl_init();&lt;br /&gt;
    curl_setopt($ch, CURLOPT_URL,$value);&lt;br /&gt;
    $fp = fopen($filename, 'w');&lt;br /&gt;
    curl_setopt($ch, CURLOPT_FILE, $fp);&lt;br /&gt;
    curl_exec ($ch);&lt;br /&gt;
    curl_close ($ch);&lt;br /&gt;
    fclose($fp);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;We now have all our facebook pictures saved on our server and can use them in various applications.&lt;/p&gt;
&lt;p&gt;Note from facebook private policy:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: 10px;"&gt;&lt;span style="color: rgb(169, 169, 169);"&gt;&lt;em&gt;"Information set to everyone is publicly available information, just like your name, profile picture, and connections.  Such information may, for example, be accessed by everyone on the Internet (including people not logged into Facebook), be indexed by third party search engines, and be imported, exported, distributed, and redistributed by us and others without privacy limitations. Such information may also be associated with you, including your name and profile picture, even outside of Facebook, such as on public search engines and when you visit other sites on the internet.  The default privacy setting for certain types of information you post on Facebook is set to everyone. You can review and change the default settings in your privacy settings.&lt;/em&gt;&lt;/span&gt;&lt;span style="color: rgb(169, 169, 169);"&gt;&lt;em&gt; If you delete everyone content that you posted on Facebook, we will remove it from your Facebook profile, but have no control over its use outside of Facebook."&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 07 Dec 2010 15:14:39 +0000</pubDate>
 <dc:creator>Gert</dc:creator>
 <guid isPermaLink="false">5 at http://labs.boulevart.be</guid>
 <comments>http://labs.boulevart.be/content/save-images-url#comments</comments>
</item>
<item>
 <title>Quick guide to the new Drupal 7 database layer</title>
 <link>http://labs.boulevart.be/content/quick-guide-new-drupal-7-database-layer</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even" property="content:encoded"&gt;&lt;p class="p1"&gt;This post is my effort to guide you through the whole new abstraction layer for accessing the database server, which comes with the Drupal 7 core distribution. In this blog-post I’ll try to explain to you guys (girls) how this new layer works, without diving into all the details, in order to give you a clear view on this new system.&lt;/p&gt;
&lt;p class="p2"&gt;First of all I’ll explain the benefits of this new system to you. Drupal needed a database system that could easily support multiple database servers in a unified way which preserves the syntax power of SQL. The system was also build to enforce the security checks. There are 6 different types of Query’s which can be called in this database system: Insert, Update, Delete, Merge, Static and Dynamic.&lt;/p&gt;
&lt;p class="p2"&gt;Now lets get it started!&lt;/p&gt;
&lt;h3&gt;Settings.php&lt;/h3&gt;
&lt;p class="p2"&gt;We should start by taking a look at the renewed database definition in the settings.php. In most cases you will be using a drupal site with only one database. For this you should use the following structure.&lt;/p&gt;
&lt;p class="p2"&gt; &lt;/p&gt;
&lt;p class="p2" style="text-align: justify;"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$databases['default']['default'] = array(&lt;br /&gt;
‘driver’ =&amp;gt; ‘mysql’,&lt;br /&gt;
‘database’ =&amp;gt; ‘drupaldb’,&lt;br /&gt;
‘username’ =&amp;gt; ‘username’,&lt;br /&gt;
‘password’ =&amp;gt; ’secret’,&lt;br /&gt;
‘host’ =&amp;gt; ‘localhost’,&lt;br /&gt;
);&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2" style="text-align: justify;"&gt; &lt;/p&gt;
&lt;p class="p2"&gt;The first thing you should notice are the values “default” in the $database array. The first one is the CONNECTION KEY, the second one is the TARGET.&lt;/p&gt;
&lt;p class="p2"&gt;The &lt;em&gt;connection key&lt;/em&gt; is a unique identifier for a database connection, there must ALWAYS be a ‘default’ connection key available.&lt;/p&gt;
&lt;p class="p2"&gt;The &lt;em&gt;target&lt;/em&gt; is used to define master/slave database structures. If the master (default) isn’t available, the system will search for the slave database. It is also possible to flag a query to run on the slave database. To make this a bit clearer I’ll give you a more complicated database structure.&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$databases['default']['default'] = array(&lt;br /&gt;
‘driver’ =&amp;gt; ‘mysql’,&lt;br /&gt;
‘database’ =&amp;gt; ‘drupaldb1′,&lt;br /&gt;
‘username’ =&amp;gt; ‘username’,&lt;br /&gt;
‘password’ =&amp;gt; ’secret’,&lt;br /&gt;
‘host’ =&amp;gt; ‘dbserver1′,&lt;br /&gt;
);&lt;br /&gt;
$databases['default']['slave'][] = array(&lt;br /&gt;
‘driver’ =&amp;gt; ‘mysql’,&lt;br /&gt;
‘database’ =&amp;gt; ‘drupaldb2′,&lt;br /&gt;
‘username’ =&amp;gt; ‘username’,&lt;br /&gt;
‘password’ =&amp;gt; ’secret’,&lt;br /&gt;
‘host’ =&amp;gt; ‘dbserver2‘,&lt;br /&gt;
);&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;In this case, the first database is the default database, the second is the slave databases.&lt;/p&gt;
&lt;p class="p2"&gt;It is also possible to define separate database structures.&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$databases['default']['default'] = array(&lt;br /&gt;
‘driver’ =&amp;gt; ‘mysql’,&lt;br /&gt;
‘database’ =&amp;gt; ‘drupaldb1′,&lt;br /&gt;
‘username’ =&amp;gt; ‘username’,&lt;br /&gt;
‘password’ =&amp;gt; ’secret’,&lt;br /&gt;
‘host’ =&amp;gt; ‘dbserver1′,&lt;br /&gt;
);&lt;br /&gt;
$databases['extra']['default'][] = array(&lt;br /&gt;
‘driver’ =&amp;gt; ‘mysql’,&lt;br /&gt;
‘database’ =&amp;gt; ‘drupaldb2′,&lt;br /&gt;
‘username’ =&amp;gt; ‘username’,&lt;br /&gt;
‘password’ =&amp;gt; ’secret’,&lt;br /&gt;
‘host’ =&amp;gt; ‘dbserver2‘,&lt;br /&gt;
);&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'Lucida Grande'; font-weight: normal; line-height: normal; font-size: 13px;"&gt;Note that no matter how many connections are defined in the settings file, These connections will not be used by Drupal until they are actually opened.&lt;/span&gt;&lt;/p&gt;
&lt;p class="p2"&gt; &lt;/p&gt;
&lt;h2&gt;Select Query&lt;/h2&gt;
&lt;p class="p2"&gt;So far for the database connections, now lets take a look at the actual use of query’s in this new database layer. For the regular select query’s not much changes. Here is an example of a select query with a short explanation.&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$result = db_query(“SELECT nid, title FROM {node} WHERE type = :type”, array(&lt;br /&gt;
‘:type’ =&amp;gt; ‘page’,&lt;br /&gt;
));&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;The db_query function uses three arguments, first one is the query string, the second one are the values used to fill up the placeholders. The third one will be explained at the next example.&lt;/p&gt;
&lt;p class="p2"&gt;Note that the placeholder (:type) doesn’t use quotes. Another thing you should take in to account is to put your database names between {}. This is needed for the database system to attache a prefix string if this is defined in your settings.&lt;/p&gt;
&lt;p class="p2"&gt;Now for the third argument of the db_query we will take a look at the following code:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$result = db_query(“SELECT nid, title FROM {node}”, array(), array(&lt;br /&gt;
‘target’ =&amp;gt; ’slave’,&lt;br /&gt;
));&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;The third argument is an array of configuration directives to detect the way the query should run. In this case the query runs on the slave database. I won’t go into the details here, because (as I said before) in this blog post I will only pick up the basics of the new database system.&lt;/p&gt;
&lt;p class="p2"&gt;The following is just a handy guide about the way you can use the database query’s results. (Not 100% relevant to this post, but this might come in handy for some of you. The others should just scroll through the code, as if it doesn’t exist).&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$result = db_query(“SELECT nid, title FROM {node}”);&lt;br /&gt;
foreach ($result as $record) {&lt;br /&gt;
// Do something with each $record&lt;br /&gt;
}&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$record = $result-&amp;gt;fetch(); // Use the default fetch mode.&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$record = $result-&amp;gt;fetchObject(); // Fetch as a stdClass object&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$record = $result-&amp;gt;fetchField($column_index); // Fetch only one field.&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;$number_of_rows = $result-&amp;gt;rowCount(); //Count the results.&lt;/code&gt;&lt;/p&gt;
&lt;p class="p3"&gt; &lt;/p&gt;
&lt;h2&gt;Insert Query&lt;/h2&gt;
&lt;p class="p2"&gt;Now we’ve arrived to he fun part. The INSERT, DELETE and UPDATE query’s require that you use the query builder object in order to behave consistently across all different databases. This is where the new object-oriented query API comes in.&lt;/p&gt;
&lt;p class="p2"&gt;The compact INSERT form is the following:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$nid = db_insert(‘node’)&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘title’ =&amp;gt; ‘Example’,&lt;br /&gt;
‘uid’ =&amp;gt; 1,&lt;br /&gt;
‘created’ =&amp;gt; REQUEST_TIME,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;This will result in the following query:&lt;/p&gt;
&lt;blockquote&gt;&lt;p class="p2"&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;INSERT INTO {node} (title, uid, created) VALUES (’Example’, 1, 1221717405);&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="p2"&gt;Note: If you don’t call the execute() method, the query will not run!&lt;/p&gt;
&lt;p class="p2"&gt;The insert query object can also be used with multiple values. To insert multiple rows you shouldn’t only use fields() but also values(). In this case fields() only defines the fields, but doesn’t put any content into the selected fields. The values() may be called multiple times in order to add more than one line to your database.&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$query = db_insert(’node’)&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;-&amp;gt;fields(array(‘title’, ‘uid’, ‘created’))&lt;br /&gt;
-&amp;gt;values(array(&lt;br /&gt;
array(&lt;br /&gt;
‘title’ =&amp;gt; ‘Example’,&lt;br /&gt;
‘uid’ =&amp;gt; 1,&lt;br /&gt;
‘created’ =&amp;gt; REQUEST_TIME,&lt;br /&gt;
),&lt;br /&gt;
array(&lt;br /&gt;
‘title’ =&amp;gt; ‘Example 2′,&lt;br /&gt;
‘uid’ =&amp;gt; 1,&lt;br /&gt;
‘created’ =&amp;gt; REQUEST_TIME,&lt;br /&gt;
)))&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;Using a ‘foreach’ the code will look like this:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$values = array(&lt;br /&gt;
array(&lt;br /&gt;
‘title’ =&amp;gt; ‘Example’,&lt;br /&gt;
‘uid’ =&amp;gt; 1,&lt;br /&gt;
‘created’ =&amp;gt; REQUEST_TIME,&lt;br /&gt;
),&lt;br /&gt;
array(&lt;br /&gt;
‘title’ =&amp;gt; ‘Example 2′,&lt;br /&gt;
‘uid’ =&amp;gt; 1,&lt;br /&gt;
‘created’ =&amp;gt; REQUEST_TIME,&lt;br /&gt;
),&lt;br /&gt;
);&lt;br /&gt;
$query = db_insert(‘node’)-&amp;gt;fields(array(‘title’, ‘uid’, ‘created’));&lt;br /&gt;
foreach ($values as $record) {&lt;br /&gt;
$query-&amp;gt;values($record);&lt;br /&gt;
}&lt;br /&gt;
$query-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p3"&gt; &lt;/p&gt;
&lt;h2&gt;Update Query&lt;/h2&gt;
&lt;p class="p2"&gt;Next stop is the UPDATE query. The update query is pretty straight forward, if you understand how the insert query’s work, it shouldn’t be a problem to understand the update query. Here it goes:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$num_updated = db_update(‘node’)&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘uid’ =&amp;gt; 5,&lt;br /&gt;
’status’ =&amp;gt; 1,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;condition(‘created’, REQUEST_TIME - 3600, ‘&amp;gt;=’)&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;This will result in the following query:&lt;/p&gt;
&lt;blockquote&gt;&lt;p class="p2"&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;UPDATE {node} SET uid=5, status=1 WHERE created &amp;gt;= 1221717405;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="p2"&gt;Not much to explain here, so let’s go to the DELETE query’s.&lt;/p&gt;
&lt;p class="p2"&gt; &lt;/p&gt;
&lt;h2&gt;Delete Query&lt;/h2&gt;
&lt;p class="p2"&gt;Again the same story here. The DELETE query is probabily the easiest form of the query object:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
$num_deleted = db_delete(‘node’)&lt;br /&gt;
-&amp;gt;condition(‘nid’, 5)&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;This will result in the following query:&lt;/p&gt;
&lt;blockquote&gt;&lt;p class="p2"&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;DELETE FROM {node} WHERE nid=5;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="p2"&gt; &lt;/p&gt;
&lt;h2&gt;Merge Query&lt;/h2&gt;
&lt;p class="p2"&gt;Finally we’ve got to the last one. The MERGE query. This one is a bit more complicated. If you would strip this one down to it’s original form, you will finde that a merge query is actually just the combination of an insert and an update query. In php it would be something like this:&lt;/p&gt;
&lt;blockquote&gt;&lt;p class="p2"&gt;&lt;code&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;?php&lt;br /&gt;
if (db_query(”SELECT COUNT(*) FROM {example} WHERE id=:id”, array(’:id’ =&amp;gt; $id)-&amp;gt;fetchField()) {&lt;br /&gt;
// Run an update using WHERE id = $id&lt;br /&gt;
}&lt;br /&gt;
else {&lt;br /&gt;
// Run an insert, inserting $id for id&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p class="p2"&gt;In the new database API structure the merge query’s are build up like this:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
db_merge(‘example’)&lt;br /&gt;
-&amp;gt;key(array(‘name’ =&amp;gt; $name))&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘field1′ =&amp;gt; $value1,&lt;br /&gt;
‘field2′ =&amp;gt; $value2,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;Here the “example” table is used. The specified key field ‘name’ has the value of $name. Now two things could happen.&lt;/p&gt;
&lt;p class="p2"&gt;First option: If the $name value exists in the database, then fields “field1” and “field2” will get an update with the correspondingvalues.&lt;/p&gt;
&lt;p class="p2"&gt;Second option: If the $name value doesn’t exist in the database, a new row will be created in which “name” gets the value $name, “field1” gets the value $field1 and “field2” gets the value $field2.&lt;/p&gt;
&lt;p class="p2"&gt;In some cases the values you want to set will have to be different, according to the fact that the key field does or doesn’t already exist. This can be handled in two ways.&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
db_merge(‘example’)&lt;br /&gt;
-&amp;gt;key(array(‘name’ =&amp;gt; $name))&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘field1′ =&amp;gt; $value1,&lt;br /&gt;
‘field2′ =&amp;gt; $value2,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;update(array(&lt;br /&gt;
‘field1′ =&amp;gt; $alternate1,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;In this case, if the “name” already exists the value of “field1” will be $alternate1, and the value of “field2” will be $value2. If the “name” doesn’t allready exist, $value1 and $value2 will be used.&lt;/p&gt;
&lt;p class="p2"&gt;It is also possible to use expressions. I’ll give you an example in which, if the ‘name’ already exists, the “value1” field will become the current value +1:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
db_merge(‘example’)&lt;br /&gt;
-&amp;gt;key(array(‘name’ =&amp;gt; $name))&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘field1′ =&amp;gt; $value1,&lt;br /&gt;
‘field2′ =&amp;gt; $value2,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;expression(‘field1′, ‘field1 + :inc’, array(‘:inc’ =&amp;gt; 1))&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;Note that expression() can be used multiple times, 1 time for each field.&lt;/p&gt;
&lt;p class="p2"&gt;Field updates can also be limited, if the row already exists. In this case, if the “name” already exists, only “field2” will be updated, and “field1” will be ignored:&lt;/p&gt;
&lt;p class="p2"&gt;&lt;code&gt;&amp;lt;?php&lt;br /&gt;
db_merge(‘example’)&lt;br /&gt;
-&amp;gt;key(array(‘name’ =&amp;gt; $name))&lt;br /&gt;
-&amp;gt;fields(array(&lt;br /&gt;
‘field1′ =&amp;gt; $value1,&lt;br /&gt;
‘field2′ =&amp;gt; $value2,&lt;br /&gt;
))&lt;br /&gt;
-&amp;gt;updateExcept(‘field1′)&lt;br /&gt;
-&amp;gt;execute();&lt;br /&gt;
?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p class="p2"&gt;This is the end of my quick guide into the new Drupal 7 database layer. Hope you’ve enjoyed it. For more detailed information about this system, I would like to refere to the official drupal database API: &lt;a href="http://drupal.org/developing/api/database"&gt;http://drupal.org/developing/api/database&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-category field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/drupal" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;Drupal&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/projects" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;Projects&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Mon, 29 Nov 2010 14:37:51 +0000</pubDate>
 <dc:creator>Jeroen</dc:creator>
 <guid isPermaLink="false">4 at http://labs.boulevart.be</guid>
 <comments>http://labs.boulevart.be/content/quick-guide-new-drupal-7-database-layer#comments</comments>
</item>
<item>
 <title>The Action View Helper</title>
 <link>http://labs.boulevart.be/content/action-view-helper</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even" property="content:encoded"&gt;&lt;h2&gt;Situation:&lt;/h2&gt;
&lt;p&gt;I use Zend_Layout to create a global view template for my website and the view (.phtml) that corresponds to a controller/action will be loaded into the $this-&amp;gt;layout-&amp;gt;content variable automatically.&lt;/p&gt;
&lt;p&gt;All good but what to do with a form in your global template?&lt;/p&gt;
&lt;p&gt;Building the form (Zend_Form) into your view isn’t really a clean option and what about validation of the form? There is no controller/action from where the form was build and so there is no controller/action where to post the form to.&lt;/p&gt;
&lt;h2&gt;Solution:&lt;/h2&gt;
&lt;p&gt;I read through the &lt;strong&gt;Zend&lt;/strong&gt; manual and I discovered there was a view helper named ‘Action’.&lt;/p&gt;
&lt;p&gt;What does it do?&lt;/p&gt;
&lt;p&gt;It enables you to call a controller/action from within your view script. It will process the controller/action code, render the view and return it into your view script.&lt;/p&gt;
&lt;h2&gt;Example:&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;IndexController.phpclass&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Index Controller extends Zend_Controller_Action {&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;public function indexAction(){}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;public function formAction(){&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//build a form $form = new Zend_Form();&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//create a text element $elementName = new Zend_Form_Element_Text(’name’); $element-&amp;gt;setRequired(true);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//a validation saying our field has to be filled out&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//create a submit button $elementSubmit = new Zend_Form_Element_Submit(’submit’); $elementSubmit-&amp;gt;setLabel(’Send’);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//add elements to the form $form-&amp;gt;addElement($elementName); $form-&amp;gt;addElement($elementSubmit);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//if we click the ‘Send’ button if($this-&amp;gt;getRequest()-&amp;gt;isPost()){ if($form-&amp;gt;isValid()){//form is valid}}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;//add the form to a view variable $this-&amp;gt;view-&amp;gt;form = $form;}}index/form.phtml&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;?php echo this-&amp;gt;form; ?&amp;gt;layout.phtml&amp;lt;?php echo$this-&amp;gt;doctype(Zend_View_Helper_Doctype::XHTML1_STRICT) . “\n”; ?&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;div id=”container”&amp;gt;&amp;lt;?phpecho$this-&amp;gt;layout()-&amp;gt;content.”\n”;?&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div&amp;gt;&lt;strong&gt;&amp;lt;?php echo $this-&amp;gt;action(’form’, ‘index’); ?&amp;gt;&lt;/strong&gt;&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;parameter list of the view action&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;action($action, $controller, $module = null, array $params = array())&lt;/code&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h2&gt;Result:&lt;/h2&gt;
&lt;p&gt;The form action of the index controller is called from the layout.phtml.&lt;/p&gt;
&lt;p&gt;The business logic is executed, the view (index/form.phtml) is rendered and is returned to our layout.phtml.&lt;/p&gt;
&lt;p&gt;When we press the ‘Send’ button in our form. The form will be submitted to index/form and the validation will happen.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-category field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/zend" typeof="skos:Concept" property="rdfs:label skos:prefLabel"&gt;Zend&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Mon, 29 Nov 2010 12:26:56 +0000</pubDate>
 <dc:creator>Yves</dc:creator>
 <guid isPermaLink="false">3 at http://labs.boulevart.be</guid>
 <comments>http://labs.boulevart.be/content/action-view-helper#comments</comments>
</item>
</channel>
</rss>

