首页 > 解决方案 > 需要帮助将 php 变量插入 URL 字符串

问题描述

这是我的 php 文件:

<div id="map_wrap">
    <div id="map"></div>
</div><!-- #map_wrap -->

<?php
$states_list = get_state_list();
$states_to_disable = get_field( 'states_to_disable' );
?>


<ul class="state-list">
    <?php
    foreach( $states_list as $key => $value ){
        $anchor = $key;
        $class = '';
        foreach( $states_to_disable as $state ){
            if( $state['value'] === $key ){
                $class = 'disabled';
                $anchor = 'no-service';
            }
        }
        $anchor = str_replace("_","-",$anchor);
        ?>
        <li><a class="<?= $class; ?>" href="<?php echo site_url(); ?>/<?= $anchor; ?>-quick-online-installment-loans"><?= $value; ?></a></li>
    <?php
    }
    ?>
</ul>


<?php

?>


<script src='<?php echo get_stylesheet_directory_uri(); ?>/js/raphael-min.js'></script>
<script src='<?php echo get_stylesheet_directory_uri(); ?>/js/usmap.js'></script>
<script>
$(document).ready(function() {
    var clickGo = true;
    $('#map').usmap({
        stateStyles: {fill: '#009a4d','stroke': '#ffffff'},
        stateSpecificStyles: {
            <?php
            foreach( $states_to_disable as $state ){
                echo $state['value'] . ": {fill: '#C5C5C5'},";
            }
            ?>
        },
        stateSpecificLabelBackingHoverStyles: {
            <?php
            foreach( $states_to_disable as $state ){
                echo $state['value'] . ": {fill: '#888'},";
            }
            ?>
        },

        clickState: {
            <?php
            foreach( $states_to_disable as $state ){
            ?>
                '<?= $state['value']; ?>' : function(event, data) {
                    var state_name = data.name;
                    console.log('1');
                    clickGo = false;
                    state_name = state_name.replace("_","-")+'';
                    location.href = "<?php echo site_url(); ?>/state-terms-and-rates/no-service";
                },
            <?php
            }
            ?>
        },

        click: function(event, data) {
            var state_name = data.name;
            console.log('2');
            state_name = state_name.replace("_","-")+'';
            if( clickGo === true ){
                location.href = "<?php echo site_url(); ?>/state-terms-and-rates/"+state_name;
            }

        },

            });
});
</script>




如您所见,在最后一段代码中,地图中任何被点击的活动状态都转到/state-terms-and-rates/+state_name。但是,我需要它去 /+state_name-quick-online-installment-loans。

我可以很接近它,但 URL 要么显示空白状态名称、单词数组、零或加号,具体取决于我一直在尝试的不同事情。

任何人都可以给我正确的方向吗?

标签: phpvariables

解决方案


推荐阅读