我正在使用找到的教程 here在 Drupal 中显示基于 Google Maps Directions API 的路线。我的节点中有两个字段 - 起始字段和目的地字段。每个都包含一个城市的名称。
问题是我无法打印 $params 数组内的字段值。这是整个 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'];
?>
如果我手动添加出发地和目的地,一切正常。但是,如果我使用以下任何组合,整个页面就会变得困惑并且不显示任何内容或根本没有任何反应。
'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']); ?>"
我也尝试过这个,但没有运气:
$departure = print $node->field_originfield['und'][0]['value'];
$arrival = print $node->field_destinationfield['und'][0]['value'];
'origin' => 'departure',
'destination' => 'arrival'
我在这里做错了什么?
最佳答案
请尝试这个:
$params = array(
'origin' => $node->field_originfield['und'][0]['value'],
'departure' => $node->field_destinationfield['und'][0]['value']
);
或
$departure = $node->field_originfield['und'][0]['value'];
$arrival = $node->field_destinationfield['und'][0]['value'];
$params = array(
'origin' => $departure,
'destination' => $arrival
);
关于php - 打印数组中的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072721/