填充数组失败

时间:2016-06-30 07:44:34

标签: javascript php arrays wordpress google-maps

我在网站上遇到了一个问题。这是一个使用WordPress作为CMS管理的网站。我创建了一个包含纬度和经度值的自定义帖子类型。我正在浏览帖子并正确获取数据。但是,当我尝试根据帖子类别保存每个数组中的帖子时,第二个数组中的数据填充失败。请参阅以下代码:

Dictionary<DateTime, int> dataDictionary = new Dictionary<DateTime, int>()
{
    {new DateTime(2016, 8, 4), 20},
    {new DateTime(2016, 8, 5), 50}, 
    {new DateTime(2016, 8, 6), 70}, 
    {new DateTime(2016, 8, 7), 90},
    {new DateTime(2016, 8, 8), 100},
    {new DateTime(2016, 8, 9), 120}, 
    {new DateTime(2016, 8, 10), 10}, 
    {new DateTime(2016, 8, 11), 40}, 
};

DateTime start = new DateTime(2016, 8, 7);
DateTime stop = new DateTime(2016, 8, 11);

页面模板 - Javascript:

70

我看到名为locationsFundet的数组已填充,但名为locationsTabt的数组未填充。这是结果:

<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>

我不一定要找人的完整答案,尽管那会很好。我至少在寻找在哪里搜索问题的好主意。

3 个答案:

答案 0 :(得分:0)

我会在代码中的第一个循环之后使用json_encode php函数。

我没有测试代码,但看起来应该是这样的:

<?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).';';

?>

答案 1 :(得分:0)

@ R3nPi2和@Andreas这很棒。使用json_encode()绝对是方法。我不知道这个方法。谢谢

我设法实现了json_encode()并用适当的数据填充两个数组。 但是,我的javascript要求数组中的数据格式如下所示:

var locationsFundet = [
                       {
                        latlng:new google.maps.LatLng(56.613931480691875,8.4210205078125),
                        info : document.getElementById('itemFundet1')   
                       },

问题是js_code以下列方式填充数组(注意冒号和引号):

var fundet = [
              {
               "latlng:":"new google.maps.LatLng(56.43820369358164,10.997314453125)",
               "info":"document.getElementById(itemFundet1)"
              },

              {
               "latlng:":"new google.maps.LatLng(55.20395325785898,15.084228515625)",
               "info":"document.getElementById(itemFundet2)"
              },

              {
               "latlng:":"new google.maps.LatLng(57.314657355733274,11.195068359375)",
               "info":"document.getElementById(itemFundet3)"
              },

              {
               "latlng:":"new google.maps.LatLng(56.85375917920851,10.267839431762695)",
               "info":"document.getElementById(itemFundet4)"
              }
];

var tabt = [
            {
             "latlng:":"new google.maps.LatLng(56.613931480691875,8.4210205078125)",
             "info":"document.getElementById(itemTabt1)"
            },

            {
             "latlng:":"new google.maps.LatLng(55.42901345240742,8.382568359375)",
             "info":"document.getElementById(itemTabt2)"
            },

            {
             "latlng:":"new google.maps.LatLng(54.73730756865753,10.777587890625)",
             "info":"document.getElementById(itemTabt3)"
            },

            {
             "latlng:":"new google.maps.LatLng(55.329535012504195,12.457809448242188)",
             "info":"document.getElementById(itemTabt4)"
            }
];

任何人都有一个关于如何使json_encode方法以我需要的格式填充数组的方法。

使用该数组的javascript如下所示:

for (var i = 0; i < locationsFundet.length; i++) 
{  

    var markerFundet = new google.maps.Marker(
    {
        position: locationsFundet[i].latlng,
        icon: fundetMarkerImg,
        animation:google.maps.Animation.DROP,
        map: map
    });

    google.maps.event.addListener(markerFundet, 'click', (function(markerFundet, i) 
    {
        return function() 
        {
            infowindow.setContent(locationsFundet[i].info);
            infowindow.open(map, markerFundet);
        }
    })(markerFundet, i));
}

for (var b = 0; b < locationsTabt.length; b++) 
{  

    var markerTabt = new google.maps.Marker(
    {
        position: locationsTabt[b].latlng,
        icon: fundetMarkerImg,
        animation:google.maps.Animation.DROP,
        map: map
    });

    google.maps.event.addListener(markerTabt, 'click', (function(markerTabt, b) 
    {
        return function() 
        {
            infowindow.setContent(locationsTabt[b].info);
            infowindow.open(map, markerTabt);
        }
    })(markerTabt, b));
}

答案 2 :(得分:0)

非常感谢R3nPi2!

在您的帮助下,我设法替换了不需要的符号和字符。

这是我的解决方案:

var fundetReplaced = <?php echo str_replace('"','', json_encode($locationFundet)); ?>
var tabtReplaced = <?php echo str_replace('"','', json_encode($locationTabt)); ?>

结果如下:

var fundetReplaced = [
                      {latlng:new google.maps.LatLng(56.43820369358164,10.997314453125),
                       info:document.getElementById('itemFundet1')},
                      {latlng:new google.maps.LatLng(55.20395325785898,15.084228515625),
                       info:document.getElementById('itemFundet2')},
                      {latlng:new google.maps.LatLng(57.314657355733274,11.195068359375),
                       info:document.getElementById('itemFundet3')},
                      {latlng:new google.maps.LatLng(56.85375917920851,10.267839431762695),
                       info:document.getElementById('itemFundet4')}];
var tabtReplaced = [
                      {latlng:new google.maps.LatLng(56.613931480691875,8.4210205078125),
                       info:document.getElementById('itemTabt1')},
                      {latlng:new google.maps.LatLng(55.42901345240742,8.382568359375),
                       info:document.getElementById('itemTabt2')},
                      {latlng:new google.maps.LatLng(54.73730756865753,10.777587890625),
                       info:document.getElementById('itemTabt3')},
                      {latlng:new google.maps.LatLng(55.329535012504195,12.457809448242188),
                       info:document.getElementById('itemTabt4')}];