简体   繁体   English

在数组中打印字段值

[英]Printing a field value in an array

I'm using the tutorial found here to display the directions based on Google Maps Directions API in Drupal. 我正在使用此处找到的教程来显示基于Drupal中的Google Maps Directions API的路线。 I have two fields in my node - originfield and destinationfield. 我的节点中有两个字段-originfield和destinationfield。 Each of them contains the name of a city. 它们每个都包含一个城市的名称。

The problem is that I can't print the field value inside of the $params array. 问题是我无法在$ params数组内打印字段值。 Here is the whole tpl.php code: 这是完整的tpl.php代码:

<?php
/**
 * @file
 * Returns the HTML for a node.
 *
 * Complete documentation for this file is available online.
 * @see https://drupal.org/node/1728164
 */
?>

<?php
// This print works:
print $node->field_originfield['und'][0]['value'];

    // Parameters used for the request
    $params = array(
        'origin'    => 'Tokyo, Japan', // I can't find a way to output the originfield's value here
        'destination'   => 'Kyoto, Japan' // I can't find a way to output the destinationfield's value here
    );

    // Join parameters into URL string
    foreach($params as $var => $val){
        $params_string .= '&' . $var . '=' . urlencode($val);  
    }

    // Request URL
    $url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');

    // Make our API request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);

    // Parse the JSON response
    $directions = json_decode($return);

    // Show the total distance
    print('<p><strong>Total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>');

?>

// This print works:
<?php
print $node->field_originfield['und'][0]['value'];
print $node->field_destinationfield['und'][0]['value'];
?>

If I manually add origin and destination everything works fine. 如果我手动添加起点和终点,则一切正常。 But if I use any of the following combinations, the whole page gets either messed up and nothing is displayed or nothing happens at all. 但是,如果我使用以下任何一种组合,整个页面要么混乱,要么不显示任何内容,或者什么也没有发生。

'origin'    => "<?php print $node->field_originfield['und'][0]['value']; ?>"
'origin'    => "print $node->field_originfield['und'][0]['value'];"
'origin'    => "$node->field_originfield['und'][0]['value'];"
'origin'    => "field_originfield['und'][0]['value'];"
'origin'    => "field_originfield;"
'origin'    => "<?php print render($content['originfield']); ?>"

I have also tried this, without luck: 我也尝试过这个,没有运气:

$departure = print $node->field_originfield['und'][0]['value'];
$arrival = print $node->field_destinationfield['und'][0]['value'];

'origin' => 'departure',
'destination'   => 'arrival'

What am I doing wrong here? 我在这里做错了什么?

Please try this: 请尝试以下方法:

$params = array(
    'origin'    => $node->field_originfield['und'][0]['value'],
    'departure' => $node->field_destinationfield['und'][0]['value']
);

or 要么

$departure = $node->field_originfield['und'][0]['value'];
$arrival = $node->field_destinationfield['und'][0]['value'];

$params = array(
    'origin' => $departure,
    'destination'   => $arrival
);

Try this 尝试这个

$departure = $node->field_originfield['und'][0]['value'];
$arrival = $node->field_destinationfield['und'][0]['value'];

'origin' => $departure,
'destination'   => $arrival

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM