php - Printing a field value in an array -
i'm using tutorial found here display directions based on google maps directions api in drupal. have 2 fields in node - originfield , destinationfield. each of them contains name of city.
the problem can't print field value inside of $params array. here whole tpl.php code:
<?php /** * @file * returns html node. * * complete documentation file available online. * @see https://drupal.org/node/1728164 */ ?> <?php // print works: print $node->field_originfield['und'][0]['value']; // parameters used request $params = array( 'origin' => 'tokyo, japan', // can't find way output originfield's value here 'destination' => 'kyoto, japan' // can't find way output destinationfield's value here ); // join parameters url string foreach($params $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 json response $directions = json_decode($return); // show total distance print('<p><strong>total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>'); ?> // print works: <?php print $node->field_originfield['und'][0]['value']; print $node->field_destinationfield['und'][0]['value']; ?>
if manually add origin , destination works fine. if use of following combinations, whole page gets either messed , nothing displayed or nothing happens @ 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 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 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 );
Comments
Post a Comment