0

I'm extremely new to javascript so I've no idea if I'm doing this correctly.

I've got some php that is filling a javascript variable array:

<?php

$pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
$data = array();
foreach($pages as $post){
  setup_postdata($post);
  $fields = get_fields(); 
  $data[] = '<p>'.$fields->company_name.'</p>';
}
wp_reset_query();

// the js array
echo 'var marker_data = ["'.implode('","', $data).'"];';
?>

This then feeds this javascript:

infowindow.setContent(marker_data[i]);

The problem is that it's not incrementing. If I change the "i" to "0" or "1" then it works. But obviously I need it to increment through.

12
  • 2
    and where's javascript ?"i see only one line Commented Sep 23, 2011 at 8:37
  • you have to use loop if want to iterate through JS array Commented Sep 23, 2011 at 8:38
  • Keep in mind they run on different levels. The JS runs after all the PHP has executed. So if you expect them to work together, you'll have problems. Commented Sep 23, 2011 at 8:39
  • @SergeS It's being echoed out in the first code box, then the one line below. Commented Sep 23, 2011 at 8:39
  • 1
    Could you check to see what the JS you generate looks like? Also remember JSON is basicly javascript, you could consider using json_encode to echo your array. Commented Sep 23, 2011 at 8:40

3 Answers 3

1

You need to increment i in JavaScript, not in php, like this:

<script type="application/javascript">
<?php
$pages =get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
$data = array();
foreach($pages as $post){
  setup_postdata($post);
  $fields = get_fields(); 
  $data[] = '<p>'.htmlspecialchars($fields->company_name).'</p>';
}
wp_reset_query();

echo 'var marker_data = ' . json_encode($data) . ';'; // Instead of implode
?>
for (var i = 0;i < marker_data.length;i++) {
    infowindow.setContent(marker_data[i]);
}
</script>
Sign up to request clarification or add additional context in comments.

10 Comments

@Rob Oops, didn't see that part. I also confused data and marker_data - sorry! Fixed in this new version. If it doesn't work, try two things: 1. Give us a link to a live example page 2. Go to your browser's developer tools or JavaScript console and look for any errors.
From looking at the source (line 90) it's showing the right data "var marker_data = ["<p>MediWales<\/p>","<p>Teamworks Design & Marketing<\/p>"];" (not sure what the extra slash in </p> has come from). So just a case of looping through that. At the moment it's only showing the "teamworks" name.
@Rob Sorry, I may be misunderstanding something, but your example looks like it works exactly as intended: Upon clicking one of the marker, the HTML content is displayed. What is not working there?
@Rob Oh, I see. The inserted backslash is correct and does no harm. It looks like you're setting infowindow in an anonymous function that already gets i. Have you tried just infowindow.setContent(marker_data[i]); (without the for loop)?
Got it, just fixed it from your source. I can't say thanks enough, literally 10 or so people have attempted and failed. Thank you thank you thank you!!
|
0

Once your PHP runs and returns the page, have a look at the source and look at your JS. It should look something like this

var marker_data = ['<p>Comp 1</p>', '<p>Comp 2</p>', '<p>Comp 3</p>'];

This means you've built your array of strings. Now you'll need to use a javascript loop to iterate through it, something like this.

var length = marker_data.length;
for (var i = 0; i < length; i++) {
   infowindow.setContent(marker_data[i]);
}

Remember that all of this iteration should happen in your JS code and not your PHP code. They are not to be mixed like that.

1 Comment

Phihag has got the json working for the marker_data, it's just not doing the looping part.
0

Good answears given above. This is a general old school instructional way how you mix php and javascript arrays, it can also be adjusted to your needs but I also suggest you go beyond it and look for new schoold things too.

<html>
<head>
<script type="text/javascript">
var myRandoms = new Array();
<?php
$dbq="\"";
for($i=0;$i<9;$i++)
    echo 'myRandoms[',$i,']=',$dbq,rand(1,999),$dbq,';';
?>
var len=myRandoms.length;
var i;
for(i=0;i<len;i++)
    alert(myRandoms[i]);

</script>
</head>
<body>
</body>
</html>

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.