0

my value $viena_salis only brings out the last value(in this case united kingdom) when there are 6 other countries and all of them get the same "plan on visiting "united kingdom"?" I've tried moving var contentString to other places in the loop in hopes of the outcome changing but nothing seems to do it. This code places markers on a google map with numbers how many people have registered from each country, and should place different colored markers depending on the type of user roles(still working on the colored marker part).

$indivitualios_salys is an array of country names.

$google maps is an array of all world countries with coordinates.

I cant figure out what's wrong with it, in my eyes it should just loop js and echo the country name since if($viena_salis==$gm_salis[0]) works with no problems.

if anyone has any ideas to what I did wrong, help would be much appreciated.

P.s I'm still new to php and js.

<?php $i=0;
        foreach ($indivitualios_salys as $viena_salis) {
            $i++;
            foreach ($Google_maps as $gm_salis) {
                if ($viena_salis==$gm_salis[0]) {
                    ?>
                    var contentString = '<div id="content" >'+
                            '<div id= "siteNotice">'+
                            '</div>'+
                              '<div id="bodyContent">'+
                              '<p>Plan on visisting "<?php echo $viena_salis; ?>"?</p>'+
                              '<p>Check out our <a href="https://biz4good.co/about/">welcome guide</a> </p>'+
                              '</div>'+
                              '</div>';
                    var infowindow = new google.maps.InfoWindow({
                        content: contentString});

                    var <?php echo 'marker'.$i; ?> = new google.maps.Marker({
                      position: {lat: <?= $gm_salis[1]; ?>, lng: <?= $gm_salis[2]; ?>},
                      map: map,
                      icon: <?php 

                foreach ($remeju_id as $con_id){
                  foreach ($country_user_id as $user_id_country) {
                    if($con_id==$user_id_country){
                      echo 'image_green_blue,';
                    }
                    if($con_id!=$user_id_country){ 
                      echo 'image_green,';
                    }
                    else {
                        echo 'image_blue,';
                    }
                  }
                }; ?>
                      label: { color: '#fff', fontWeight: 'bold', fontSize: '14px', text: '<?php echo $elementCount[$viena_salis]; ?>' }
                    });
                    <?php echo 'marker'.$i; ?>.addListener('click', function() {
                        infowindow.open(map, <?php echo 'marker'.$i; ?>);
                    });
                    <?php
                }
            }
        };?>
      }

Edit: $indivitualios_salys- looks like:

Array
(
    [0] => Aruba
    [1] => Lithuania
    [2] => Bolivia
    [3] => Ukraine
    [4] => Bosnia and Herzegovina
    [5] => Greece
    [9] => United Kingdom
)

$google_maps has all countries but in short it looks like:

Array
(
    [0] => Array
        (
            [0] => Andorra
            [1] => 42.546245
            [2] => 1.601554
        )

    [1] => Array
        (
            [0] => United Arab Emirates
            [1] => 23.424076
            [2] => 53.847818
        )

    [2] => Array
        (
            [0] => Afghanistan
            [1] => 33.93911
            [2] => 67.709953
        )
6
  • 1
    so you need contentString hold multiple values ? Commented Jan 10, 2020 at 7:02
  • @AmitSharma The code makes a clickable popup on markers that are placed on the map, since it shows "planning on visiting (country name here)" and those markers are on different countries it should change, so yeah. Commented Jan 10, 2020 at 7:32
  • @StackSlave Thank you for your tip and yeah, I understand that it's a mess and that there must have been ways to do it more efficiently, before I used django framework for college projects but once I entered internship(since it was the only option because there are no companies that work with python and accept internship students) I had do things with php, js, css, wp ect. So it's different from what I'm used to. Commented Jan 10, 2020 at 7:34
  • 1
    this holds all the values $viena_salis Commented Jan 10, 2020 at 7:41
  • Please provide examples of how your data in $indivitualios_salys and $Google_maps looks like. Also I'd really advise you not to mix PHP blocks within HTML or JS - it makes things unnecessarily hard to read. Commented Jan 10, 2020 at 8:12

1 Answer 1

1

i simplified your code and prepared a short snippet of code , in my code $viena_salis is iterating all matched values of array , you can just check it out by copy below code and see in console it will result you

Andorra

Afghanistan

$indivitualios_salys = array('Andorra','Lithuania','Afghanistan','Ukraine','Bosnia and Herzegovina','Greece','United Kingdom');


$Google_maps = array(
                array('Andorra',42.546245,1.601554),
                array('United Arab Emirates',42.546245,1.601554),
                array('Afghanistan',42.546245,1.601554),

            );


        $i=0;
        foreach ($indivitualios_salys as $viena_salis) {
            $i++;
            foreach ($Google_maps as $gm_salis) {
                if ($viena_salis==$gm_salis[0]) {
                    ?>
                    <script>
                        var viena_salis = '<?php echo $viena_salis ?>';
                        console.log(viena_salis);
                    </script>
                    <?php
                }
            }
        };

?>

Above code is tested and worked,

i gave you above solution as answer of your statement , " my value $viena_salis only brings out the last value(in this case united kingdom) ",

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.