Dougless Dougless - 5 months ago 13
PHP Question

Populating array is failing

I am struggeling with an issue on my website. It is a website managed using WordPress as CMS. I created a custom post type containing a latitude and longitude value. I am running through the post and fetching the data right. However, when i try to save the posts in each array depending on the post category the dat population within the second array is failing. Please see the code below:

<div style="display: none;">

<?php $i = 1; ?>
<?php $b = 1; ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php $terms = get_the_terms($loop->ID, 'observationcat'); ?>

<?php foreach( $terms as $term ) {?>

<?php if ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 112)) : ?>

<div id="itemFundet<?php echo $i; ?>">
<a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
<?php echo'<p>Kategori ID: ' . $term->term_id . '</p>'; ?>
<?php echo'<p>Kategori: ' . $term->name . '</p>'; ?>
<?php the_excerpt(); ?>
<?php echo '<p>Tabt udstyr nummer: ' . $i . '</p>'; ?>
<?php echo get_post_meta($post->ID, '_location', true) ?>
</div>

<?php $i++; ?>

<?php elseif ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 113)) : ?>

<div id="itemTabt<?php echo $b; ?>">
<a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
<?php echo'<p>Kategori ID: ' . $term->term_id . '</p>'; ?>
<?php echo'<p>Kategori: ' . $term->name . '</p>'; ?>
<?php the_excerpt(); ?>
<?php echo '<p>Fundet udstyr nummer: ' . $b . '</p>'; ?>
<?php echo get_post_meta($post->ID, '_location', true) ?>
</div>

<?php $b++; ?>

<?php endif; ?>

<?php } ?>

<?php endwhile; ?>

</div>


Page template - Javascript:

var locationsFundet = [

<?php $i = 1; while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php foreach( $terms as $term ) { ?>

<?php if ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 112)) : ?>
{
latlng:new google.maps.LatLng(<?php echo get_post_meta($post->ID, '_location', true) ?>),
info : document.getElementById('itemFundet<?php echo $i; ?>')
},

<?php endif; ?>
<?php } ?>

<?php $i++; ?>

<?php endwhile; ?>
];

var locationsTabt = [

<?php $b = 1; while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php foreach( $terms as $term ) { ?>

<?php if ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 113)) : ?>
{
latlng:new google.maps.LatLng(<?php echo get_post_meta($post->ID, '_location', true) ?>),
info : document.getElementById('itemTabt<?php echo $b; ?>')
},

<?php endif; ?>

<?php } ?>

<?php $b++; ?>

<?php endwhile; ?>
];


I see that the array named locationsFundet are populated, but the array named locationsTabt are not. This is the result:

var locationsFundet = [{
latlng: new google.maps.LatLng(56.69244163539978, 11.612548828125),
info: document.getElementById('itemFundet1')
}, {
latlng: new google.maps.LatLng(55.20395325785898, 8.953857421875),
info: document.getElementById('itemFundet2')
}, {
latlng: new google.maps.LatLng(55.66519318443606, 10.601806640625),
info: document.getElementById('itemFundet3')
}, {
latlng: new google.maps.LatLng(56.292156685076456, 8.162841796875),
info: document.getElementById('itemFundet4')
}, {
latlng: new google.maps.LatLng(55.329535012504195, 12.457809448242188),
info: document.getElementById('itemFundet5')
}, {
latlng: new google.maps.LatLng(56.85375917920851, 10.267839431762695),
info: document.getElementById('itemFundet6')
}, ];
var locationsTabt = [];


I am not necessarily looking for a complete answer from someone, although that would be nice. I am at least looking for a good idea about where to search for the issue.

Answer

I would use json_encode php function after a loop like the first one in your code.

I did not test the code, but it should look something like this:

<?php
$i = 1;
$b = 1;

$locationsFundet = array();
$locationsTabt = array();

while ( $loop->have_posts() ) : $loop->the_post();

  $terms = get_the_terms($loop->ID, 'observationcat');

  foreach( $terms as $term ) {

    if ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 112)) :

      $locationsFundet[] = array('latlng' => 'new google.maps.LatLng'.get_post_meta($post->ID, '_location', true).')', 'info' => 'document.getElementById("itemFundet'.$i.'")' );

      $i++;

    elseif ((get_post_meta($loop->ID, '_location', true) !== '') && (($term->term_id) == 113)) :

      $locationsTabt[] = array('latlng' => 'new google.maps.LatLng'.get_post_meta($post->ID, '_location', true).')', 'info' => 'document.getElementById("itemTabt'.$b.'")' );

      $b++;

    endif;

  }

endwhile;

//Here you have the two javascript arrays:
echo 'var locationsFundet = '.json_encode($locationsFundet).';';
echo 'var locationsTabt = '.json_encode($locationsTabt).';';

?>