Last updated January 17, 2012. Created on January 17, 2012.
Edited by gnassar. Log in to edit this page.

A very thorough example by alan.pinkert.

------

I'd like to submit a basic example that I use for the documentation page of this project. Please edit it any way you like.

Basic Filter Example

Sometimes, I create my own weight fields, so that users can order nodes themselves.
I've often create views of nodes that have a higher weight or lower weight that the currently viewed node.
This requires a filter that compares the weight of the current node to the weight of all other possibly selected nodes.
For this example, we'll create that filter.

My field in CCK is called "field_report_order" as its machine name.

<?php
$nid
= (int)arg(1); // Gets the node id of the current page

$node = node_load($nid);  // Loads the node with this nid

$order = $node->field_report_order[0]['value'];  // Sets the variable "$order" to the current node's weight field

$nodeids = array();  // Creates the array we'll use to pass back to the filter

$my_result = db_query("SELECT nid FROM content_type_workspace_report WHERE field_report_order_value > $order");  // This is the query from the database.  See notes below for more.

while ($my_row = db_fetch_array($my_result))  // Adds the node ids to our array

{
$nodeids[] = $my_row['nid'];  // The blank [] next to $nodeids will add each 'nid' from the query sequentially in the array
}

return
$nodeids// Passes the filter back
?>

Notes on DB queries: as of CCK 6.x-2.8, fields are stored in different tables/columns based on a few factors. Use this PHP to determine how to phrase your query:

<?php
// Obtain information about a field attached to a node type.
$field = content_fields('field_name', 'node_type');

// Obtain database information about the field.
$db_info = content_database_info($field);

// $db_info['table'] contains the name of the table where the field is stored.

// $db_info['columns'] contains information about the database columns of the field.
?>

For more reference on sql syntax, see http://www.1keydata.com/sql/sql-syntax.html

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.