Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP adding in a line break to echo statement

Hi,

I'm building a Google Map for a WordPress site and I need to allow the client to alter the markers and info windows that are placed onto the map dynamically within WordPress. Hard-coded everything was working fine, but the PHP I wrote to drag out the locations from WordPress and output them into a JavaScript Variable in a script tag isn't working. Having looked into it I can see that a line break is being added at the end of the 'description' field before the final closing quotation marks.

Anyone got any ideas why this might happen?

I've tried taking out all whitespace from the statements but that doesn't seem to work.

The page.php file:

<script type="text/javascript">
var markers = [
    <?php $args = array('post_type' => 'markers');
                    $the_query = new WP_Query( $args );?>
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php get_template_part('content', 'markers'); ?>
<?php endwhile; endif; ?>
];

</script>

Then the content-markers.php file:

<?php echo 
'{"title": "';?><?php the_field('title');?>
<?php echo '","lat": ';?><?php the_field('lat');?>
<?php echo ',"lng": ';?><?php the_field('lng');?>
<?php echo ',"description": "';?><?php trim(the_field('description'));?>
<?php echo '"},';

Your help is appreciated!

3 Answers

Brian Herbert
Brian Herbert
3,834 Points

As an alternative, and better practice, you may want to look at using the json_encode function. It will take an associative array and make the JSON for you. It's much less error prone that way. You would so something like this:

<?php
$data = array('title'=>the_field('title'),
    'lat'=>the_field('lat'),
    'lng'=>the_field('lng'),
    'description'=>the_field('description'));
echo json_encode($data);

Nice, I like that a lot. I started off trying to use the JSON API plugin but it was outputting the LatLng coordinates as strings so I couldn't use it. I'm still getting the same problem as above atm, but this looks like a way better method. Thanks!

Brian Herbert
Brian Herbert
3,834 Points

When you put that last line in your content-markers.php file on a newline after the ?> tag, it will print the newline. Basically, anything outside the <?php ?> tags will print as you write it. For better readability, and to fix the problem, you might want to do something like this:

<?php
echo '{"title": "'.the_field('title').'",
"lat": '.the_field('lat').',
"lng": '.the_field('lng').',
"description": "'.trim(the_field('description')).'"},';

Thanks for the quick response, Brian.

I tried something like that earlier but abandoned the idea because the output I get is really strange - it prints the contents of the fields first and then the concatenated strings afterwards like this:

<script type="text/javascript">
var markers = [
                        Bondi Beach-33.890542151.274856<p>Bondi Beach is a popular beach and the name of the surrounding suburb in Sydney, New South Wales, Australia. Bondi Beach is located 7 km (4 mi) east of the Sydney central business district, in the local government area of Waverley Council, in the Eastern Suburbs. Bondi, North Bondi and Bondi Junction are neighbouring suburbs.</p>

{"title": "","lat": ,"lng": ,"description": ""},

];

</script>

I never encountered this before, do you know why it would do that?