Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ryann Green
Ryann Green
11,010 Points

Including PHP Array within Javascript

Right now I have a series of object in an include PHP file like this:

$shoots[01] = array(
    "name" => "Rustic Farm Shoot",
    "id" => "rustic",
    "img" => array("img/slider.jpg", "img/testbw2.jpg")
); 
$shoots[02] = array(
    "name" => "Bridesmaids' Party",
    "id" => "bridesmaids",
    "img" => array("img/testcolor.jpg")
);

What I am trying to do is include the img array items as a javascript variable array. What is making this difficult is that I would like the javascript array to change dynamically depending on what page it is. I'm doing something similar to display an image like this:

<?php foreach($shoots as $shootID => $shoot) { 
        if($_GET["id"] == $shootID) { 
          foreach($shoot['img'] as $img) break; ?>
              <img src="<?php echo $img; ?>" alt="<?php echo $shoot['name']; ?>">
      <?php }}; ?>

The javascript is below that section. So basically, I would like something similar that if the $_GET["id"] == $shootID, then it should use that object's "img" array. Hopefully this makes sense. Any help is appreciated.

3 Answers

Maybe if you post your Javascript it might make more sense.

I would just use php and forget javascript all together, unless you are trying to make slider dynamic, then just insert you foreach loop into javascript.

<?php
$shoots[01] = array(
    "name" => "Rustic Farm Shoot",
    "id" => "rustic",
    "img" => "images/Facebook.png"
); 
$shoots[02] = array(
    "name" => "Bridesmaids' Party",
    "id" => "bridesmaids",
    "img" => "images/Twitter.png"
);



              if(isset($_GET["id"])) {
                  $id = $_GET['id'];
              }else{
                  $id = "";
              }
  ?>      <?php foreach($shoots as $shootID => $shoot) { ?>
        <?php echo "<a href=\"test.php?id=".$shootID." \">{$shoot['name']}</a>"; ?>
       <?php                                 
              if($id == $shootID){
                  ?>
              <img src="<?php echo $shoot['img']; ?>" alt="<?php echo $shoot['name']; ?>">

            <?php }}?>
            ```
Ryann Green
Ryann Green
11,010 Points

Sorry for the lack of responses. I figured out what I was doing wrong and just wanted to post it in case anyone had the same question. This is the javascript I used in addition to the code listed above.

<script>
images = [<?php foreach($shoots as $shootID => $shoot) { 
        if($_GET["id"] == $shootID) { 
          foreach($shoot['img'] as $img) {
            echo json_encode($img, JSON_UNESCAPED_SLASHES) . ",";}}} ?>];

</script>

Thanks for all the help. If anyone knows a better or different way to do the same thing, I would be interested to see it. I'm sure this isn't the most efficient way.