Skip to content

Instantly share code, notes, and snippets.

View mrsinguyen's full-sized avatar
🎯
Focusing

Si Nguyen mrsinguyen

🎯
Focusing
View GitHub Profile
/**
* Get URI parameter.
*
* @param string $str
* Example to use $str urldecode(drupal_get_destination()).
* @return array
*/
function _more_node_buttons_get_params($str) {
$chunks = explode("?", $str);
$params = explode("&", $chunks[1]);
<?php
/**
* Implements hook_url_outbound_alter() - from URL Alter module
*/
function YOURMODULE_url_outbound_alter(&$path, &$options, $original_path) {
// Rewrite paths for rices4peru.com
if ($_SERVER['HTTP_HOST'] == "www.rices4peru.com") {
$options['absolute'] = TRUE;
@mrsinguyen
mrsinguyen / drupaDynamicQuery.php
Created June 6, 2010 04:44
The Drupal Dynamic Query
<?php
$query = db_select('users', 'u');
$query
->condition('u.uid', 0, '<>')
->fields('u', array('uid', 'name', 'status', 'created', 'access'))
->range(0, 50);
$result = $query->execute();
@mrsinguyen
mrsinguyen / multiInsert.php
Created June 6, 2010 04:49
Drupal's Database API, Multi-insert form
<?php
$values = array(
array(
'title' => 'Example',
'uid' => 1,
'created' => REQUEST_TIME,
),
array(
'title' => 'Example 2',
'uid' => 1,
<?php
$sql = "SELECT n.nid, n.vid, n.title, n.uid, u.name, n.created
FROM {node} n
INNER JOIN {users} u
ON u.uid = n.uid
WHERE n.type = 'quiz'";
if ($uid != 0) {
$sql .= " AND n.uid = %d";
$args[] = $uid;
<?php
db_merge('example')
->key(array('name' => $name))
->fields(array(
'field1' => $value1,
'field2' => $value2,
))
->expression('field1', 'field1 + :inc', array(':inc' => 1))
->execute();
?>
<?php
// Old code.
$result = db_query("SELECT nid FROM {node} n WHERE n.type = '%s' AND n.status = %d", array('page', 1));
// DBTNG. Note that multiple
$result = db_query("SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status", array(
':type' => 'page',
':status' => 1,
));
?>
<?php
// Old code, loop over a result.
while ($row = db_fetch_object($result)) {
}
// DBTNG.
foreach ($result as $row) {
}
// Old code, fetch a single field directly from a query.
<?php
// Old code.
db_query("UPDATE {profile_field} SET weight = %d, category = '%s' WHERE fid = %d", $weight, $category, $fid);
// DBTNG.
db_update('profile_field')
->fields(array(
'weight' => $weight,
'category' => $category,
))
@mrsinguyen
mrsinguyen / reflection.php
Created June 30, 2010 02:02
PHP's reflection api
<?php
// $Id$
function reflection_menu() {
$items = array();
$items['reflection-page'] = array(
'page callback' => 'reflection_page',
'title' => 'Reflection test',
'access arguments' => array('access content'),