I am trying to display data from my flexinode and am having trouble trying to figure out just how to query it.

What I would like to do is display nodes like how they show up when listed in taxonomy, but I would also like to be able to use variables like $node->flexinode_3. I basically want to be able to list certain elements from my flexinode in a loop anywhere on my site.

Any help is always appreciated.

Thanks :).

Comments

slayerment’s picture

Anybody?

ant71’s picture

It may not be exactly what you are after, but it should give you some clues.


//Need to supply $node_type e.g. "flexinode-1" and $lis_no i.e. number of nodes to display e.g. 5

$sql = "SELECT node.title, node.type, node.nid FROM node WHERE node.type = '$node_type' LIMIT $list_no";

$result = db_query($sql);

//This will spit you out a list of nodes of type flexinode-1.

while ($node = db_fetch_object($result)) {

   //this is for 4.7
   $output .= node_view(node_load($node->nid));

   //or for 4.6 use
   $output .= node_view(node_load(array('nid' => $nid)));

}

//You could use $node->flexinode_5 or whatever in there

I hope that gives you a start.

slayerment’s picture

Thank you very much for the reply. I tried using the following code but I think I am doing something wrong. Nothing is appearing for me. Any thoughts?

//Need to supply $node_type e.g. "flexinode-1" and $lis_no i.e. number of nodes to display e.g. 5
$node_type = 'flexinode-1';
$list_no = 10;
$sql = "SELECT node.title, node.type, node.nid FROM node WHERE node.type = '$node_type' LIMIT $list_no";
$result = db_query($sql);
//This will spit you out a list of nodes of type flexinode-1.
while ($node = db_fetch_object($result)) {
   $output .= node_view(node_load(array('nid' => $nid)));
print $node->flexinode_5;
}
joel_guesclin’s picture

I have been wondering about using Flexinode more.... errr, flexibly - myself, so I'm interested in whatever you may find out.

If you are getting nothing at all from the print statement then this is probably because you the property "flexinode_5" does not exist for the $node object.

Have you tried just doing a "print_r($node)" which will print out all the properties of the object and their values?

slayerment’s picture

I tried print_r($node) but that didn't do anything either.

edmund.kwok’s picture

Would this work?

//Need to supply $node_type e.g. "flexinode-1" and $lis_no i.e. number of nodes to display e.g. 5
$node_type = 'flexinode-1';
$list_no = 10;
$sql = "SELECT node.title, node.type, node.nid FROM node WHERE node.type = '$node_type' LIMIT $list_no";
$result = db_query($sql);
//This will spit you out a list of nodes of type flexinode-1.
while ($node = db_fetch_object($result)) {
   $output .= node_view(node_load(array('nid' => $node->nid)));
}

Your initial codes uses the $nid variable which equals to null since it's not initialized. $node->nid would be the nid grabbed from the database with your sql query.

justaguru’s picture

Your flexinode data is not in the node table, so you need to 'JOIN' the node to the flexinode data. Here is a long and complex looking example, but just paste the code into a node that is php code and set ctype_id and run it. This will show a table view of the flexinode fields in a node if they are selected to 'Show in table' for that flexinode type. Uncomment the print $sql line to see the sql code required to include the flexinnode fields in a query.

<?php
  // Set ctype_id to '2' if you want to see flexinode-2 nodes
  $ctype_id = '2';
  $ctype = flexinode_load_content_type($ctype_id);

  // Build the query.
  $fields_to_select = array();  $table_joins = array();  $where_clauses = array();

  foreach ($ctype->fields as $field) {
    $fieldname = 'flexinode_'. $field->field_id;

    $fields_to_select[] = flexinode_invoke('db_select', $field);
    $table_joins[] = 'LEFT JOIN {flexinode_data} '. $fieldname .' ON n.nid = '. $fieldname .'.nid';
    $where_clauses[] = $fieldname .'.field_id = '. $field->field_id;
  }
  $type = 'flexinode-' . db_escape_string($ctype_id);
  $extra_fields = count($fields_to_select) > 0 ? ', ' . implode(', ', $fields_to_select) : '';
  $extra_where = count($where_clauses) > 0 ? ' AND ' . implode(' AND ', $where_clauses) : '';
  $sql = 'SELECT n.nid, n.title '. $extra_fields .' 
    FROM {node} n '. implode(' ', $table_joins) ." 
    WHERE n.status = 1 AND n.type = '$type'". $extra_where;


// Uncomment this line to see the SQL code used to grab the data
//print $sql . '<BR>';
  // Build the column headers
  $header[] = array('data' => t('Title'), 'field' => 'n.title');
  foreach ($ctype->fields as $field) {
    if ($field->show_table) {
      $header[] = array('data' => t($field->label));
    }
  }

  $sql .= tablesort_sql($header);

  // Fill in the rows.
  $rows = array();
  $nodes = pager_query(db_rewrite_sql($sql), 20);
  while ($node = db_fetch_object($nodes)) {
    $row = array(l($node->title, 'node/' . $node->nid));
    foreach ($ctype->fields as $field) {
      if ($field->show_table) {
        $thedata = flexinode_invoke('format', $field, $node, TRUE);
        $row[] = $thedata ? $thedata : '';
      }
    }
    $rows[] = $row;
  }

  $output .= theme('table', $header, $rows);
  $output .= theme('pager', NULL, 20, 0, tablesort_pager());
print $output;

?>
fuerst’s picture

If you want to take full control of the output you may put your data inside an array like

[0] =>
     [label name 1] => value,
     [label name 2] => value,
     ...
[1] =>
     [label name 1] => value,
     [label name 2] => value,
     ...
...

Use this as a php snippet as described above

global $user;
$userid=$user->uid; // gets the currently logged in user's id number
$ctype_id = 5; // change this to your flexinode id number
$output = '';
$ctype = flexinode_load_content_type($ctype_id);

// Build the query.
$fields_to_select = array();
$table_joins = array();
$where_clauses = array();

foreach ($ctype->fields as $field) {
  $fieldname = 'flexinode_'. $field->field_id;

  $fields_to_select[] = flexinode_invoke('db_select', $field);
  $table_joins[] = 'LEFT JOIN {flexinode_data} '. $fieldname .' ON n.nid = '. $fieldname .'.nid';
  $where_clauses[] = $fieldname .'.field_id = '. $field->field_id;
}

$type = 'flexinode-' . db_escape_string($ctype_id);
$extra_fields = count($fields_to_select) > 0 ? ', ' . implode(', ', $fields_to_select) : '';
$extra_where = count($where_clauses) > 0 ? ' AND ' . implode(' AND ', $where_clauses) : '';
// the 'AND uid = $userid' restricts results to the current user
$sql = 'SELECT n.nid, n.title '. $extra_fields .' FROM {node} n '. implode(' ', $table_joins) ." WHERE n.status = 1 AND uid = $userid AND n.type = '$type'". $extra_where; 

// Build the columns.
$header[] = array('data' => t('title'), 'field' => 'n.title');
foreach ($ctype->fields as $field) {
  if ($field->show_table) {
    $fieldname = 'flexinode_'. $field->field_id;

    $sort_column = flexinode_invoke('db_sort_column', $field);
    if ($sort_column) {
      $header[] = array('data' => t($field->label), 'field' => $sort_column);
    }
    else {
      $header[] = array('data' => t($field->label));
    }
  }
}

$sql .= tablesort_sql($header);

// Build the rows.
$rows = array();
$nodes = pager_query(db_rewrite_sql($sql), 50);

while ($node = db_fetch_object($nodes)) {
  $row = array();
  $row['link'] = l($node->title, 'node/' . $node->nid);
  $row['title'] = $node->title;
  $row['url'] = 'node/' . $node->nid;


  foreach ($ctype->fields as $field) {
    if ($field->show_table) {
      $data = flexinode_invoke('format', $field, $node, TRUE);
      $row[$field->label] = $data ? $data : '';
    }
  }

  $rows[] = $row;
}

// sort by key (one of your label names)
$GLOBALS['usortCallbackFunction']['sortKey'] = 'label1';
// sort direction: DESC for descendending or ASC for ascendending
$GLOBALS['usortCallbackFunction']['sortDir'] = 'ASC';

// sort the array (uncomment next line if no sorting desired)
usort ($rows, 'usortCallbackFunctionXYZ');

if ($rows) {
  // output row by row
  foreach ($rows as $row) {
	  // build your own output format here
	  $output .= '&#039;.print_r($row, 1).&#039;';
  }
  $output .= theme('pager', NULL, 50);
}

print $output;

// The usort callback function, see http://www.php.net/usort
// don't use a function name that is in use in your Drupal installation
function usortCallbackFunctionXYZ ($a, $b) {
	// do nothing if key $GLOBALS['usortCallbackFunction']['sortKey'] is not set in array $a or $b
	if ( !isset ($a[$GLOBALS['usortCallbackFunction']['sortKey']]) OR !isset ($b[$GLOBALS['usortCallbackFunction']['sortKey']]) ) {
		return 0;
	}

	// sort descendending
	if ( isset ($GLOBALS['usortCallbackFunction']['sortDir']) and $GLOBALS['usortCallbackFunction']['sortDir'] == 'DESC' ) {
		return strcmp($b[$GLOBALS['usortCallbackFunction']['sortKey']], $a[$GLOBALS['usortCallbackFunction']['sortKey']]);
	}

	// or sort ascendending
	return strcmp($a[$GLOBALS['usortCallbackFunction']['sortKey']], $b[$GLOBALS['usortCallbackFunction']['sortKey']]);
}

PEpe’s picture

it look very nice, but can you send example of part "//build your own output format here" ? ... I have flexinode with only one label - "name" ... And I would like to make list of them ... I would like to use it for list of gags of my friends ...
I try this:
$output .= '<div>'.print($row[title]).'</div>';
but output is:

content of label
content of label
1
1

why are there "1" ?

fuerst’s picture

Are you setting the correct flexinode id number in $ctype_id? Besides not using quotes around your string literal array index 'title' I can't see something wrong. May be you actually have this 4 rows? Try printing the whole $rows array to get an idea whats going on.

BTW, this one in my snippet above is wrong because it does not check for the count of items in $rows:

if ($rows) {
  // output row by row

Replace it by

if ( count( $rows ) > 0 ) {
  // output row by row
PEpe’s picture

I play with it again, and I use your function "print_r" ... $output .= '<div>'.print_r($row['title'], 1).'</div>'; and it works fine.

(I changed "print_r" on "print" at the start of my experimentation, and output appended char "1" for each one node to the end of the output. It's my stupidity, I actually don't know why I changed it...(I'm really not php expert:) ) )

So sorry, your code is correct and really very useful! THX

astra’s picture

This script looks very useful to customize the output of flexinode types. I'll try it later on.