0

I have a php variable as such:

$output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'"></div>';

I want the code below inside the "marker" div that's within the above var:

<p><?php echo $location['address']; ?></p>
<p><?php the_field('description'); ?></p>

The <<<EOD method isn't working and going in/out of php tags doesn't seem to work. It seems like it's going to look messy, I wondering what is the syntax I'm missing here?

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
clive
  • 51
  • 6
  • 1
    Is there a reason you cannot include that code inside the variable with the other html? – IncredibleHat Jan 13 '18 at 00:58
  • Change `the_field` to `get_field` and then just concatenate it in, like your already doing. – Lawrence Cherone Jan 13 '18 at 00:59
  • Oh, is `the_field()` some wordpress thing? Then nevermind... I'm out. – IncredibleHat Jan 13 '18 at 01:04
  • It is unclear what you're requesting. Show us the resulting code that you want to have in the end. –  Jan 13 '18 at 01:07
  • Yes @LawrenceCherone that is how I'm doing it. `the_field` is a wordpress thing yes (using Advanced custom fields), but it's kind of redundant as I can't even get the plain `echo` to show up on my site. – clive Jan 13 '18 at 01:11
  • Most probably you have an error which you don't see because it's not showing on screen. Put this two lines before your code in order to see the eventual raised errors: `error_reporting(E_ALL);` followed by `ini_set('display_errors', 1);`. Then give us feedback. –  Jan 13 '18 at 01:14
  • Thanks @aendeerei that did help. I had undefined vars, but ultimately Lawrence's code does work, I had some confusion with the syntax. Thank you. – clive Jan 13 '18 at 01:53
  • You are welcome. Important is that your code it's working now. You should accept the answer of @LawrenceCherone then, so that other users can benefit from it too. –  Jan 13 '18 at 01:55

2 Answers2

0

Try this:

<?php
$output_map[$the_ID]['map'] = '
<div class="marker" data-lat="'.$get_google_map['lat'].'">
    <p>'.$location['address'].'</p>
    <p>'.get_field('description').'</p>
</div>';
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0
  • Activate error reporting for showing eventual errors.
  • Make use of the PHP's sprintf() function to build your final result in an elegant manner.
  • Echo the output as usual, or by using the heredoc syntax: <<<. In the case of the latter I am wondering why would you choose to do it?

Notice the use of the curly brackets { and } inside the heredoc output. See this answer and Example #3 Heredoc string quoting example in the heredoc syntax.

Good luck.

<?php

// Display eventual errors and exceptions.
error_reporting(E_ALL);
ini_set('display_errors', 1); // SET IT TO 0 ON A LIVE SERVER!

// Dummy test function
function the_field($name) {
    return 'Some ' . $name . ' value';
}

// Dummy test values.
$the_ID = 1;
$get_google_map['lat'] = '50.2341234';
$location['address'] = 'Some address';

// Build the map item's content.
$output_map[$the_ID]['map'] = sprintf(
        '<div class="marker" data-lat="%s">
            <p>%s</p>
            <p>%s</p>
        </div>'
        , $get_google_map['lat']
        , $location['address']
        , the_field('description')
);

// Option 1.
//echo $output_map[$the_ID]['map'];

// ... or Option 2.
echo <<<MAP
    {$output_map[$the_ID]['map']}
MAP;

Output (visible with "View page source" in browser, or similar option):

<div class="marker" data-lat="50.2341234">
    <p>Some address</p>
    <p>Some description value</p>
</div>