phparraysdrupal-6fieldnodereference

search array of field values for one of them in drupal-6


So, I have got a node reference field, which can have multiple values and I'm attempting to compose a php code, which will check all values of that field for a certain value i want and print a text if it finds it. I want to do it in views php field and this is the code i have came up with so far:

<?php
$node = node_load($data->nid);
print $data->nid . '<br>';
if(in_array('Yog [nid:1315]', $node->field_ref_oznajomenieni)) {
print 'yoog';
}
else {
print 'niemanic';
}
?>

I'm somewhat bad in PHP, but: The node_load() works fine and $data->nid returs node id for every row of my view. This is confirmed by print $data->nid . '|';

Now, the if function is supposed to do what i want, search the array of values for something in the node reference field and print something too. I'm not sure if i should search for the nid of the referenced node, its title or how i put it in the code, whatever i try, i get the print 'niemanic'; of the else{}

How should i do it exactly, please?


Solution

  • To make it clear: The code is to search for a value in a node reference field and print something only if it is found.

    one way:

    // less RAM usage
    $node = node_load($data->nid);
    $napis = '';
    foreach ($node->field_ref_oznajomenieni as $value) {
    if ($value[nid] == '1315') {
    $napis = 'Yog';
    }
    // if (!($napis)) $napis = 'niemanic'; // for testing purpose
    }
    print $napis;
    

    another way: (untested, may lack some brackets)

    // flattened matrix way
    $plaskatablica = array();
    foreach ($node->field_ref_oznajomenieni as $value) {
    $plaskatablica[] = $value[nid];
    
    if(in_array('1315', $plaskatablica)) {
    print 'Yog';
    }
    }
    

    Here is what i have figured by myself:

    $node = node_load($data->nid);
    $ii = 0;
    foreach ($node->field_ref_oznajomenieni as $value) {
    if(in_array('1315', array_values($node->field_ref_oznajomenieni[$ii]))) {
    print 'Yog';
    }
    ++$ii;
    }
    

    This is print_r($node->field_ref_oznajomenieni); output of an example field:

    Array ( 
    [0] => Array ( [nid] => 1320 ) 
    [1] => Array ( [nid] => 1315 ) 
    [2] => Array ( [nid] => 1518 ) 
    [3] => Array ( [nid] => 1519 ) 
    [4] => Array ( [nid] => 1525 ) 
    )