javascript - 填充数组失败

标签 javascript php arrays wordpress google-maps

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

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

页面模板 - 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; ?>
];

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

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 = [];

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

最佳答案

我会在像代码中的第一个循环一样的循环之后使用 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).';';

?>

关于javascript - 填充数组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116907/

相关文章:

javascript - 如何编写嵌套样式?

javascript - 将两个功能绑定(bind)到同一个按钮

javascript - 服务器端 AJAX post 为空(PHP)

php - PHP 和 MySQL 中的过程问题

php - 为 HTML 表单下拉列表指定默认选定项

javascript - 从 JSON 对象数组中剪切数据

python - 将 Html 页面中的数据获取到 Python 数组中

javascript - SlideToggle jQuery 速度

C++ - 读取后数组值变为1

javascript - 针对未经验证的代码保护 SSJS