[英]Printing a field value in an array
我正在使用此處找到的教程來顯示基於Drupal中的Google Maps Directions API的路線。 我的節點中有兩個字段-originfield和destinationfield。 它們每個都包含一個城市的名稱。
問題是我無法在$ 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
);
嘗試這個
$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.